6. 安装MySQL
6.1. 说明
安装在192.168.0.250
这台机里,使用ansible进行安装和启动。因为包比较大,下载速度太慢,这里使用上传rpm
直接进行本地安装。我们使用的版本是5.7.33
,安装包的下载地址https://downloads.mysql.com/archives/community/
打包下载的rpm比较多,但不需要全部安装,因此解压后选择了以下几个包进行安装
- mysql-community-client-5.7.33-1.el7.x86_64.rpm(MySQL客户端)
- mysql-community-common-5.7.33-1.el7.x86_64.rpm(服务端和客户端常用库文件)
- mysql-community-devel-5.7.33-1.el7.x86_64.rpm(客户端开发用的头文件及库)
- mysql-community-libs-5.7.33-1.el7.x86_64.rpm(MySQL数据库客户端共享库)
- mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm(兼容老版本的共享库)
- mysql-community-server-5.7.33-1.el7.x86_64.rpm(数据库服务端与相关工具)
- mysql-community-test-5.7.33-1.el7.x86_64.rpm(MySQL测试套件)
6.2. yml脚本
---
- hosts: mysqlservers
remote_user: root
vars_files:
- ../vars.yml
tasks:
- name: Copy rpm
copy:
src: "{{ PLAYBOOK_DIR }}/files/mysql-rpm"
dest: /home
owner: root
group: root
mode: 0755
- name: install rpm
shell: "yum localinstall -y /home/mysql-rpm/*"
- name: firewarld add 3306
firewalld:
port: 3306/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: start mysql
service:
name: mysqld
state: started
tags:
- start mysql
6.3. 运行ansible-playbook
ansible-playbook -i /etc/ansible/hosts mysql/main.yml
6.4. 安装成功查看启动状态
image.png6.5. 获取MySQL root用户密码
grep tempo /var/log/mysqld.log
image.png
6.6. 修改root用户密码
根据刚找到的密码进行登陆
mysql -uroot -p
修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxxx';
注意:mysql 5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误。查看 MySQL官网密码详细策略
6.7. 添加远程登录用户
mysql> CREATE USER 'anson'@'%' IDENTIFIED BY 'xxxxxxx';
mysql> GRANT ALTER, CREATE, DELETE, INSERT, SELECT, UPDATE ON *.* TO 'anson'@'%';
mysql> FLUSH PRIVILEGES;
6.8. 修改默认编码
mysql 安装后默认不支持中文,需要修改编码。需修改 /etc/my.cnf
配置文件,加入以下内容,client
和mysql
加到最后,否则会报错。
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启服务即可看到效果
shell> systemctl restart mysqld
shell> mysql -uroot -p
mysql> show variables like 'character%';
image.png修改前
image.png修改后
网友评论