运维需求见表:
主机IP | 部署服务 | 说明 |
192.168.21.66 | Ansible | basedir:/opt/mysql datadir:/opt/mysql/data binlog:/opt/mysql/log/ errorlog:/opt/mysql/log/error.log socket:/opt/mysql/mysqld.sock tmp:/opt/mysql/tmp mysql运行用户:jiangwei 端口:3306 操作系统:Ubuntu 16.04 MySQL版本:5.7 |
192.168.21.70 | MySQL |
关联文档:
- https://www.cnblogs.com/WSQL/p/9963342.html
- https://www.cnblogs.com/WSQL/p/9963243.html
1.前置条件:
- 安装依赖包:apt-get install numactl gcc make libaio libaio-dev
- 创建jiangwei运行用户
- 192.168.21.66能够用root免密登录到192.168.21.70主机上
2./opt/ansible/mysql目录结构如下:
mysql
├── files
│ ├── my.cnf
│ ├── mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
│ └── setpassword.sh
└── tasks
└── main.yml
其中 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz为安装包,可到MySQL官网下载。
1.1)main.yml文件:
- name: unarchive Mysql
unarchive: src=mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz dest=/usr/local/ owner=jiangwei group=jiangwei
- name: mkdir mysql datadir
file: path=/opt/mysql/data owner=jiangwei group=jiangwei state=directory
- name: mkdir mysql log dir
file: path=/opt/mysql/log owner=jiangwei group=jiangwei state=directory
- name: mkdir mysql tmp dir
file: path=/opt/mysql/tmp owner=jiangwei group=jiangwei state=directory
- name: change directory
command: mv /usr/local/mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
#- name: init.d mysql file
# command: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
- name: cp my.cnf
copy: src=my.cnf dest=/etc/my.cnf
- name: initid mysql-server
command: /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=jiangwei
- name: start mysql
command: /usr/local/mysql/support-files/mysql.server start
- name: set password
copy: src=setpassword.sh dest=/opt/mysql/ mode=755
- name: sh setpassword
shell: sh /opt/mysql/setpassword.sh
- name: mysql binary command
command: cp /usr/local/mysql/bin/* /usr/bin/
1.2) my.cnf文件内容:
[mysqld_safe]
socket = /opt/mysql/mysqld.sock
nice = 0
[mysqld]
server-id = 70
event_scheduler = 1
character-set-server = utf8
bind-address = 0.0.0.0
symbolic-links = 0
max_allowed_packet = 40M
user = rocky
pid-file = /opt/mysql/mysqld.pid
socket = /opt/mysql/mysqld.sock
port = 3306
basedir = /usr/local/mysql
datadir = /opt/mysql/data
tmpdir = /opt/mysql/tmp
skip-external-locking
key_buffer_size = 16M
thread_stack = 192K
thread_cache_size = 8
max_connections = 1000
query_cache_limit = 1M
query_cache_size = 16M
log_error = /opt/mysql/log/error.log
log-bin = /opt/mysql/log/mysql-bin
expire_logs_days = 10
max_binlog_size = 100M
[client]
default-character-set = utf8
1.3) setpassword.sh的内容如下:
#该脚本仅限第一次修改初始密码使用
mysqlinitpasswd=`grep "password is generated" /opt/mysql/log/error.log | awk '{print $NF}'`
mysql -uroot -p${mysqlinitpasswd} -S /opt/mysql/mysqld.sock -e "alter user 'root'@'localhost' identified by 'jiangwei';grant all on *.* to 'root'@'%'identified by 'jiangwei';flush privileges" --connect-expired-password
3.配置所要安装mysql的主机列表,vi /etc/ansible/hosts,内容如下:
[mysql]
192.168.21.70
[mysql:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_user=root
4.mysqlinstall.yml文件内容如下:
- hosts: mysql
roles:
- mysql
5.ansible-playbook执行安装,命令:ansible-playbook /opt/ansible/mysqlinstall.yml
6.验证mysql服务,可登陆认证:mysql -uroot -pjiangwei -S /opt/mysql/mysqld.sock