ansible批量安装初始化MySQL并启用主从同步

1.前提配置好ansible,如图,这里不做介绍了。
ansible批量安装初始化MySQL并启用主从同步_第1张图片
2.定义主机群组

vim host.ini
#############################################################
#                                                           #
# writed by fangzhipeng 20200521                            #
# 注意:对于从库迁移至新集群环境                            #
# (1)所有字符集与原主库一致                               #
# (2)sql_mode与原主库一致                                 #
# (3)wait_timeout = 1800(或和原主库一致)                #
# (4)innodb_thread_concurrency = 0                        #
# (5)innodb_concurrency_tickets = 5000                    #
#                                                           #
#############################################################

[master]
172.21.203.52 backup=0

[slave]
172.21.203.53 backup=1


[all:children]
master
slave


[all:vars]
#必填选项,认真仔细核对
pro_group=yunwei
cluster_name=mysql_3307
vip=172.21.203.55
#mysql_version=5.6.26
mysql_version=5.7.22
mysql_port=3306

3.定义yaml文件

vim init_mysql_cluster.yml

- hosts: all
#  gather_facts: no
  remote_user: root
  roles:
    - init_mysql_cluster
#    - record_mysql_cluster

4.执行ansible命令

ansible-playbook -i host.ini init_mysql_cluster.yml
PLAY [all] *******************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [172.21.203.53]
ok: [172.21.203.52]

TASK [init_mysql_cluster : check mysqld process] *****************************************************************************************************************
changed: [172.21.203.53]
changed: [172.21.203.52]

TASK [init_mysql_cluster : if stdout != 0 ,Interrupt execution] **************************************************************************************************
fatal: [172.21.203.52]: FAILED! => {"changed": false, "msg": "mysqld process is exist"}
fatal: [172.21.203.53]: FAILED! => {"changed": false, "msg": "mysqld process is exist"}

PLAY RECAP *******************************************************************************************************************************************************
172.21.203.52              : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
172.21.203.53              : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

[root@zb-yunwei-test-203-51 mysql_initial]# ansible-playbook -i host.ini init_mysql_cluster.yml

PLAY [all] *******************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : check mysqld process] *****************************************************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : if stdout != 0 ,Interrupt execution] **************************************************************************************************
fatal: [172.21.203.52]: FAILED! => {"changed": false, "msg": "mysqld process is exist"}
fatal: [172.21.203.53]: FAILED! => {"changed": false, "msg": "mysqld process is exist"}

PLAY RECAP *******************************************************************************************************************************************************
172.21.203.52              : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
172.21.203.53              : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

[root@zb-yunwei-test-203-51 mysql_initial]# ansible-playbook -i host.ini init_mysql_cluster.yml

PLAY [all] *******************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : check mysqld process] *****************************************************************************************************************
changed: [172.21.203.53]
changed: [172.21.203.52]

TASK [init_mysql_cluster : if stdout != 0 ,Interrupt execution] **************************************************************************************************
skipping: [172.21.203.52]
skipping: [172.21.203.53]

TASK [init_mysql_cluster : pro_group not allow null] *************************************************************************************************************
skipping: [172.21.203.52]
skipping: [172.21.203.53]

TASK [init_mysql_cluster : create mysql user] ********************************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : config /etc/my.cnf] *******************************************************************************************************************
changed: [172.21.203.53]
changed: [172.21.203.52]

TASK [init_mysql_cluster : install yum dependents] ***************************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and 
specifying `name: "{{ item }}"`, please use `name: ['libaio-devel', 'numactl-devel', 'perl-Data-Dumper', 'perl-DBI', 'perl-DBD-MySQL', 'perl-Time-HiRes', 'perl-
Digest-MD5', 'bison', 'pigz', 'fuse-libs', 'MySQL-python', 'nfs-utils']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and 
specifying `name: "{{ item }}"`, please use `name: ['libaio-devel', 'numactl-devel', 'perl-Data-Dumper', 'perl-DBI', 'perl-DBD-MySQL', 'perl-Time-HiRes', 'perl-
Digest-MD5', 'bison', 'pigz', 'fuse-libs', 'MySQL-python', 'nfs-utils']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [172.21.203.52] => (item=[u'libaio-devel', u'numactl-devel', u'perl-Data-Dumper', u'perl-DBI', u'perl-DBD-MySQL', u'perl-Time-HiRes', u'perl-Digest-MD5', u'bison', u'pigz', u'fuse-libs', u'MySQL-python', u'nfs-utils'])
ok: [172.21.203.53] => (item=[u'libaio-devel', u'numactl-devel', u'perl-Data-Dumper', u'perl-DBI', u'perl-DBD-MySQL', u'perl-Time-HiRes', u'perl-Digest-MD5', u'bison', u'pigz', u'fuse-libs', u'MySQL-python', u'nfs-utils'])

TASK [init_mysql_cluster : transfer mysql install package to remote host and unarchive to /usr/local/] ***********************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : export path env variable] *************************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : create datadir] ***********************************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : copy mysqldata /data/mysql/] **********************************************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : cp mysql.server to /etc/init.d/mysql.server] ******************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : cp mysql.service to mysql.service] ****************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : config mysql.service start up on boot] ************************************************************************************************
ok: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : start mysql server (systemctl)] *******************************************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : cp mysql.server to /etc/init.d/mysql.server] ******************************************************************************************
skipping: [172.21.203.52]
skipping: [172.21.203.53]

TASK [init_mysql_cluster : config mysql.server start up on boot] *************************************************************************************************
skipping: [172.21.203.52]
skipping: [172.21.203.53]

TASK [init_mysql_cluster : start mysql server (service)] *********************************************************************************************************
skipping: [172.21.203.52]
skipping: [172.21.203.53]

TASK [init_mysql_cluster : transfer sql to remote host] **********************************************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : create replication user on master / start slave on slave] *****************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : clear temp file tmp/master_slaves.sql] ************************************************************************************************
changed: [172.21.203.52]
changed: [172.21.203.53]

TASK [init_mysql_cluster : transfer mysql script to remote host and unarchive to /opt/] **************************************************************************
ok: [172.21.203.53]
ok: [172.21.203.52]

TASK [init_mysql_cluster : mysql backup config crontab] **********************************************************************************************************
skipping: [172.21.203.52]
ok: [172.21.203.53]

TASK [init_mysql_cluster : mysql add vip config crontab] *********************************************************************************************************
changed: [172.21.203.53]
changed: [172.21.203.52]

TASK [init_mysql_cluster : config vip] ***************************************************************************************************************************
skipping: [172.21.203.53]
changed: [172.21.203.52]

TASK [init_mysql_cluster : arping vip] ***************************************************************************************************************************
skipping: [172.21.203.53]
changed: [172.21.203.52]

PLAY RECAP *******************************************************************************************************************************************************
172.21.203.52              : ok=20   changed=11   unreachable=0    failed=0    skipped=6    rescued=0    ignored=0   
172.21.203.53              : ok=19   changed=9    unreachable=0    failed=0    skipped=7    rescued=0    ignored=0   


6.登录托管主机验证MySQL的安装和主从同步是否启动

ansible批量安装初始化MySQL并启用主从同步_第2张图片
ansible批量安装初始化MySQL并启用主从同步_第3张图片
托管主机 52和53MySQL均已安装完毕,且主从同步正常。

你可能感兴趣的:(数据库)