ansible配置mysql主从复制

配置主机
1、下载安装所需安装包

[root@server1 ansible]# ls
ansible-2.7.8-1.el7.noarch.rpm
ansible-tower-setup-bundle-3.4.2-1.el7.tar.gz
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm
[root@server1 ansible]# yum install -y *

ansible配置mysql主从复制_第1张图片
2、配置
[root@server1 ansible]# cd /etc/ansible/
[root@server1 ansible]# ls
ansible.cfg hosts roles
[root@server1 ansible]# vim hosts
ansible配置mysql主从复制_第2张图片
测试ping模块:先ping——>输入yes,再加-k参数

[root@server1 ansible]# ansible server1 -m ping
The authenticity of host 'server1 (172.25.60.1)' can't be established.
ECDSA key fingerprint is 4a:62:22:df:00:1f:c1:37:c6:ea:b4:8c:ad:75:b3:07.
Are you sure you want to continue connecting (yes/no)? yes
server1 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'server1,172.25.60.1' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}

[root@server1 ansible]# ansible server1 -m ping -k
SSH password: 
server1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

ansible配置mysql主从复制_第3张图片

[root@server1 ansible]# ansible server2 -m ping
The authenticity of host 'server2 (172.25.60.2)' can't be established.
ECDSA key fingerprint is 91:66:3b:f0:dd:37:39:62:2d:2c:7a:05:bc:e1:e6:d4.
Are you sure you want to continue connecting (yes/no)? yes
server2 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'server2,172.25.60.2' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).", 
    "unreachable": true
}
[root@server1 ansible]# ansible server2 -m ping -k
SSH password: 
server2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

ansible配置mysql主从复制_第4张图片
设置免密

[root@server1 ansible]# ssh-keygen

ansible配置mysql主从复制_第5张图片

[root@server1 ansible]# ssh-copy-id server1
[root@server1 ansible]# ssh-copy-id server2

ansible配置mysql主从复制_第6张图片
免密连接测试

[root@server1 ansible]# ansible server1 -m ping
[root@server1 ansible]# ansible server2 -m ping

ansible配置mysql主从复制_第7张图片
建立文件目录

 [root@server1 ansible]# mkdir -p /etc/ansible/roles/mysql_repl/files
[root@server1 ansible]# mkdir -p /etc/ansible/roles/mysql_repl2/files
[root@server1 ansible]# cd /etc/ansible/
[root@server1 ansible]# ls
ansible.cfg  hosts  roles
[root@server1 ansible]# vim test.yml

ansible配置mysql主从复制_第8张图片
server2建立目录

[root@server2 ~]# mkdir -p /etc/ansible/roles/mysql_repl/files
[root@server2 ~]# mkdir -p /etc/ansible/roles/mysql_repl2/files

建立mysql_repl执行文件目录和变量目录

[root@server1 ansible]# cd roles/mysql_repl
[root@server1 mysql_repl]# mkdir tasks
[root@server1 mysql_repl]# mkdir vars
[root@server1 vars]# vim main.yml
mysql_version: mysql-5.7.24-1.el7.x86_64.rpm-bundle
mysql_dir: /etc/ansible/roles/mysql_repl/files

ansible配置mysql主从复制_第9张图片
ansible配置mysql主从复制_第10张图片

[root@server1 mysql_repl]# cd /etc/ansible/roles/mysql_repl/files/
[root@server1 files]# cd /etc/
[root@server1 etc]# cat my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@server1 etc]# cd -
/etc/ansible/roles/mysql_repl/files
[root@server1 files]# ls
[root@server1 files]# vim my.cnf

ansible配置mysql主从复制_第11张图片
配置执行脚本

[root@server1 files]# cd /etc/ansible/roles/mysql_repl/tasks/
[root@server1 tasks]# vim main.yml

ansible配置mysql主从复制_第12张图片
配置mysql_repl

[root@server1 mysql_repl]# cd /etc/ansible/roles/mysql_repl2
[root@server1 mysql_repl2]# mkdir tasks
[root@server1 mysql_repl2]# mkdir vars
[root@server1 mysql_repl2]# cd files/
[root@server1 files]# vim my.cnf

ansible配置mysql主从复制_第13张图片
[root@server1 files]# cd …
[root@server1 mysql_repl2]# cd vars/
[root@server1 vars]# vim main.yml

-name: copy package
  copy: src={{mysql_dir}}/{{mysql_version}}.tar dest={{mysql_dir}}/{{mysql_version}}.tar -C {{ mysql_dir }}/
- name: tar mysql.tar
  shell: tar -xvf {{mysql_dir}}/{{mysql_version}}.tar -C {{ mysql_dir }}/
- name: install mysql
  yum: name={{mysql_dir}}/mysql-community-client-5.7.24-1.el7.x86_64.rpm,{{mysql_dir}}/mysql-community-common-5.7.24-1.el7.x86_64.rpm,{{mysql_dir}}/mysql-community-libs-5.7.24-1.el7.x86_64.rpm,{{mysql_dir}}/mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm,{{mysql_dir}}/mysql-community-server-5.7.24-1.el7.x86_64.rpm,MySQL-python
- name: cp my.cnf
  copy: src={{mysql_dir}}/my.cnf dest=/etc/my.cnf
- name: start mysqld
  service:
        name: mysqld
        state: started
- name: init mysql
  shell: mysql -uroot -p`cat /var/log/mysqld.log | grep 'password is generated' | awk '{print $NF}'` --connect-expired-password -e "alter user root@localhost identified by 'Xuhuan@301415';"
- name: repl1
  shell: mysql -uroot -pXiaojiujiu@666 --connect-expired-password -e "stop slave;"
- name: repl2
  shell: mysql -uroot -pXiaojiujiu@666 --connect-expired-password -e "change master to master_host='172.25.60.1',master_user='rep',master_password='Xuhuan@301415',master_auto_position=1;"
_position=1;"
- name: repl3
        shell: mysql -uroot -pXuhuan@301415 --connect-expired-password -e "start slave;"

ansible配置mysql主从复制_第14张图片

[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# tree
.
├── ansible.cfg
├── hosts
├── roles
│   ├── mysql_repl
│   │   ├── files
│   │   │   ├── my.cnf
│   │   │   └── mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
│   │   ├── main.yml
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── vars
│   │       └── main.yml
│   └── mysql_repl2
│       ├── files
│       │   ├── my.cnf
│       │   └── mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
│       ├── tasks
│       │   └── main.yml
│       └── vars
│           └── main.yml
└── test.yml





  [root@server1 ansible]# ansible-playbook test.yml

ansible配置mysql主从复制_第15张图片

[root@server1 ansible]# mysql -uroot -pXuhuan@301415

ansible配置mysql主从复制_第16张图片

[root@server2 ansible]# mysql -uroot -pXuhuan@301415

ansible配置mysql主从复制_第17张图片

你可能感兴趣的:(ansible配置mysql主从复制)