Ansible部署Mysql 5.7.28主从

Lassie ·
更新时间:2024-09-21
· 721 次阅读

准备环境

Ansible 主机: 10.4.0.57  

Mysql Master: 10.4.0.17

Mysql Slave: 10.4.0.22

所有操作都在Ansible主机上进行

执行步骤:

1. 安装ansible 2.9 

pip install ansible==2.9

2. 配置 Ansible到Master和Slave主机免密

ssh-copy-id 10.4.0.17 #需要输入root的密码 ssh-copy-id 10.4.0.22 #需要输入root的密码

 3. 下载ansible 源码

https://gitee.com/kevinliu_CQ/ansible/tree/master/MySQL-5.7.28-Master-slave-Playbook

4. 下载mysql 5.7.28 bin 文件,因为git上能上传这么大的文件,放到百度盘里面了,下载以后把mysql.zip放如到roles/common/files/mysql.zip

   下载地址

链接:https://pan.baidu.com/s/1WCgOQRQFnz7rh4UdBI4i7g 提取码:it8k [root@vm10-4-0-57 MySQL-5.7.28-Master-slave-Playbook]# ls -ltr roles/common/f iles/mysql.zip -rw-r--r-- 1 root root 824374085 Feb 15 19:55 roles/common/files/mysql.zip

5. 编辑目录中的vars变量和host文件,改成符合自己的。

#vi vars.yml

### 基础环境 user: mysql mysql_port: 3306 pack_home: /data basedir: "{{pack_home}}/mysql" datadir: "{{pack_home}}/mysql-data" logdir: "{{ basedir }}/logs" rundir: "{{basedir}}/run" ### MySQL root user password root_password: Passw0rd^123 ### MySQL Socket mysql_socket: "{{ basedir }}/run/mysql.sock" ### MySQL rep user password rep_user: replication rep_password: Replication^123 ### MySQL remote user wduser password myuser_user: mysqlremoteuser myuser_password: mysqlremoteuser_password

 #vi hosts

[master] 10.4.0.17 [slave] 10.4.0.22 [slave:vars] master_host_ip=10.4.0.17

6. 运行playbook ,执行安装主从

ansible-playbook -i hosts mysql_cluster.yaml -e"@vars.yml" [root@vm10-4-0-57 MySQL-5.7.28-Master-slave-Playbook]# ansible-playbook -i hosts mysql_cluster.yaml -e"@vars.yml" PLAY [master,slave] ************************************************************************************************************************ TASK [Gathering Facts] ********************************************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : yum python-pip] ************************************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Create mysql group] ********************************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Create mysql user] ********************************************************************************************************** ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Copy MySQL Package] ********************************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Unpacking package] ********************************************************************************************************** changed: [10.4.0.22] changed: [10.4.0.17] TASK [common : Create data dir] ************************************************************************************************************ ok: [10.4.0.17] ok: [10.4.0.22] TASK [common : Create logs dir] ************************************************************************************************************ ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Create error file] ********************************************************************************************************** changed: [10.4.0.17] changed: [10.4.0.22] TASK [common : delete data dir] ************************************************************************************************************ changed: [10.4.0.17] changed: [10.4.0.22] TASK [common : create data dir] ************************************************************************************************************ changed: [10.4.0.17] changed: [10.4.0.22] TASK [common : Copy MySQL configure file] ************************************************************************************************** ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Initialize the MySQL] ******************************************************************************************************* changed: [10.4.0.17] changed: [10.4.0.22] TASK [common : Execution environment variable] ********************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Copy MySQL start file] ****************************************************************************************************** ok: [10.4.0.17] ok: [10.4.0.22] TASK [common : create run dir] ************************************************************************************************************* ok: [10.4.0.22] ok: [10.4.0.17] TASK [common : Stop MySQL if it is running] ************************************************************************************************ changed: [10.4.0.22] changed: [10.4.0.17] TASK [common : start MySQL] **************************************************************************************************************** changed: [10.4.0.22] changed: [10.4.0.17] TASK [common : Get Intial Password] ******************************************************************************************************** changed: [10.4.0.17] changed: [10.4.0.22] TASK [common : Change Password] ************************************************************************************************************ changed: [10.4.0.22] changed: [10.4.0.17] PLAY [master,] ***************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************* ok: [10.4.0.17] TASK [master : Create account for replication] ********************************************************************************************* changed: [10.4.0.17] TASK [master : create business user:{{myuser}} which also have admin privileges] *********************************************************** changed: [10.4.0.17] TASK [master : Get Mysql-master Status] **************************************************************************************************** ok: [10.4.0.17] PLAY [slave] ******************************************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************************* ok: [10.4.0.22] TASK [slave : Get Master IP] *************************************************************************************************************** ok: [10.4.0.22] TASK [slave : SET 2] *********************************************************************************************************************** ok: [10.4.0.22] TASK [slave : show] ************************************************************************************************************************ ok: [10.4.0.22] => { "info_temp['Position']": "1479" } TASK [slave : Configure replication on the slave.] ***************************************************************************************** changed: [10.4.0.22] TASK [slave : Start slave] ***************************************************************************************************************** changed: [10.4.0.22] TASK [slave : Get Slave] ******************************************************************************************************************* ok: [10.4.0.22] TASK [slave : Slave_IO_Running] ************************************************************************************************************ ok: [10.4.0.22] => { "msg": "Slave_IO_Running Yes" } TASK [slave : Slave_SQL_Running] *********************************************************************************************************** ok: [10.4.0.22] => { "msg": "Slave_SQL_Running Yes" } PLAY RECAP ********************************************************************************************************************************* 10.4.0.17 : ok=24 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.4.0.22 : ok=29 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

7. 验证主从状态,登陆到slave主机,可以看到主从同步成功了。

mysql > show slave status \G; [root@vm10-4-0-22 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 5.7.28-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.4.0.17 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1788 Relay_Log_File: vm10-4-0-22-relay-bin.000002 Relay_Log_Pos: 629 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1788 Relay_Log_Space: 842 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 17 Master_UUID: cadee0fa-500c-11ea-9416-fa163e47d46c Master_Info_File: /data/mysql-data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) ERROR: No query specified mysql>
作者:一直学下去



ansible Mysql

需要 登录 后方可回复, 如果你还没有账号请 注册新账号