Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ansible to create mysql Master and Slave

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use ansible to create mysql master and slave. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

I. each software version

1. Docker version

Client version: 1.3.2 Client API version: 1.15 Go version (client): go1.3.3 Git commit (client): 39fa2fa/1.3.2 OS/Arch (client): linux/amd64 Server version: 1.3.2 Server API version: 1.15 Go version (server): go1.3.3 Git commit (server): 39fa2fa/1.3.2

2. Docker storage

Containers: 2 Images: 4 Storage Driver: devicemapper Pool Name: docker-253:1-1430610-pool Pool Blocksize: 65.54 kB Data file: / var/lib/docker/devicemapper/devicemapper/data Metadata file: / var/lib/docker/devicemapper/devicemapper/metadata Data Space Used: 9.855 GB Data Space Total: 107.4 GB Metadata Space Used: 6.095 MB Metadata Space Total: 2.147 GB Library Version: 1.02.84-RHEL7 (2014-03-26) Execution Driver: native-0.2 Kernel Version : 3.18.3-1.el7.elrepo.x86_64 Operating System: CentOS Linux 7 (Core)

II. Docker

1. Create 2 new containers test1 (mysql master) and test2 (mysql slave)

[root@docker-test3 ~] # sh create_docker_container_use_static_ip.sh test1 docker.ops-chukong.com:5000/centos6-http:new / usr/bin/supervisord 10 {'Physics_ip':'10.10.17.3','Container_name':'test1','Container_ip':'172.16.1.2/24','Container_vlan':'10','Container_vlan_gateway':'172.16.1.1/24' 'Container_create':'2015-03-05 14 sh create_docker_container_use_static_ip.sh test2 docker.ops-chukong.com:5000/centos6-http:new 49 usr/bin/supervisord 19 May 14 sh create_docker_container_use_static_ip.sh test2 docker.ops-chukong.com:5000/centos6-http:new / usr/bin/supervisord 10 {' Physics_ip':'10.10.17.3','Container_name':'test2','Container_ip':'172.16.1.3/24','Container_vlan':'10' 'Container_vlan_gateway':'172.16.1.1/24','Container_create':'2015-03-05 14 purl 49 purl 39' 'Container_status':'running'} [root@docker-test3 ~] # docker ps- a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36b3b3643097 docker.ops-chukong.com:5000/centos6-http:new "/ usr/bin/supervisor About a minute ago Up About a minute Test2 90f8ffc29d41 docker.ops-chukong.com:5000/centos6-http:new "/ usr/bin/supervisor About a minute ago Up About a minute test1

Two instances have been created, and the ip of test1 is 172.16.1.2 and the ip of Test2 is 172.16.1.3

The reason for using this creation is to use persistent fixed ip. You can also create it in other ways.

The ip of the host is 10.10.17.3

III. Ansible

1. Add rout

Add a route to the host in the ansible to facilitate the direct ansible deployment of mysql to the container's ip

[root@puppet ansible] # route add-net 172.16.0.0 netmask 255.255.0.0 gw 10.10.17.3 ping the two containers ip [root@puppet ansible] # ping-c 2 172.16.1.2 PING 172.16.1.2 (172.16.1.2) 56 (84) bytes of data. 64 bytes from 172.16.1.2: icmp_seq=1 ttl=63 time=0.846 ms 64 bytes from 172.16.1.2: icmp_seq=2 ttl=63 time=0.121 ms-172.16.1.2 ping statistics-2 packets transmitted, 2 received, 0 packet loss Time 1001ms rtt min/avg/max/mdev = 0.363 ms You have mail in/ var/spool/mail/root [root@puppet ansible] # ping-c 2 172.16.1.3 PING 172.16.1.3 (172.16.1.3) 56 (84) bytes of data. 64 bytes from 172.16.1.3: icmp_seq=1 ttl=63 time=0.672 ms 64 bytes from 172.16.1.3: icmp_seq=2 ttl=63 time=0.111 ms-- 172.16.1.3 ping statistics-2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.111 ms 0.391 ms

You can see the connection. Put these two ip in the hosts of ansible.

2. Add hosts

[root@puppet ansible] # tail-n3 / etc/ansible/hosts [container_mysql] 172.16.1.2 etc/ansible/hosts 22 172.16.1.3

3. Deploy mysql using ansible

A, deploy mysql master, deploy in test1 node

Time ansible-playbook mysql_master_install.yml-extra-vars "host=172.16.1.2 user=root mysql_slave_ip=172.16.1.3" k

Mysql_slave_ip is from the ip of the library, and this is to authorize the slave account in master.

Let's start the deployment.

Root@puppet ansible] # time ansible-playbook mysql_master_install.yml-- extra-vars "host=172.16.1.2 user=root mysql_slave_ip=172.16.1.3"-k SSH password: PLAY [172.16.1.2] * * * * GATHERING FACTS * * ok: [172.16.1.2] TASK: [common | Install initializtion require software] * changed: [ 172.16.1.2] TASK: [mysql_master_install | Copy Mysql Software To Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Create Mysql User In Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Copy Mysql Start Script To Redhat Client] * * changed: [172.16.1.2] TASK: [mysql_master_install | Copy Install Mysql Script To Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Copy Mysql Config To Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Copy Mysql Security Script To Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Create Mysql Install Dir] * ok: [172.16.1.2] TASK: [mysql_master_install | Uncompression Mysql Software To Redhat Client] * * changed: [172.16.1.2] TASK: [mysql_master_install | Modify Mysql Dir Permission In Redhat Client] * ok: [172.16.1.2] = > (item=/data/mysql/datadir) ok: [172.16.1.2] = > (item=/data/mysql/basedir) TASK: [mysql_master_install | Install Mysql Script In Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Wait Untils Mysql Service Avaiable In Redhat Client] * * changed: [172.16.1.2] TASK: [mysql_master_install | Start Myql Security Script In Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Add Boot Start Mysql Service In Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Copy Mysql Create Slave Script To Redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Create Mysql Master And Slave In redhat Client] * * changed: [172.16.1.2] TASK: [mysql_master_install | Delete Create Mysql Master And Slave Script In redhat Client] * changed: [172.16.1.2] TASK: [mysql_master_install | Delete Mysql compression Software In Redhat Client] * changed: [172.16.1.2] PLAY RECAP * * * 172.16.1.2: ok=19 changed=16 unreachable=0 failed=0 real 3m2.646s user 0m14.250s sys 0m0.854s

You can see that the deployment is completed in 3 minutes, and the most wasteful time is for yum to install the basic library.

Now check it in test1.

[root@puppet ansible] # ssh 172.16.1.2 root@172.16.1.2's password: Last login: Mon Nov 17 14:10:39 2014 from 172.17.42.1 root@b8d17d0f3941:~ 15:02:39 # netstat-tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 172.16.1.2 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0.0.0.0LISTEN 8/sshd tcp * LISTEN-tcp 0 0 0.0.0.0 LISTEN 8/sshd tcp 22 0.0.0.0 LISTEN 8/sshd tcp 0 0:: 80:: * LISTEN 14 / Httpd tcp 00: 22: * LISTEN 8/sshd root@b8d17d0f3941:~ root@b8d17d0f3941:~ 15:03:12 # mysql-h 172.16.1.2-u root-p Enter password: Welcome to the MySQL monitor. Commands end with; or\ g. Your MySQL connection id is 12 Server version: 5.5.21-log Source distribution Copyright (c) 2000, 2011, 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 databases +-+ | Database | +-+ | information_schema | | mysql | | performance_schema | +-+ 3 rows in set (0.00 sec) mysql > use mysql Reading table information for completion of table and column names You can turn Off this feature to get a quicker startup with-A Database changed mysql > select host User,password from user +-- + | host | user | password | +-- -- + | localhost | root | * BE78618CBAFFF409CE17D81579C1678B94439BE1 | | 172.16.1.3 | root | * BE78618CBAFFF409CE17D81579C1678B94439BE1 | | 172.16.1.2 | root | * BE78618CBAFFF409CE17D81579C1678B94439BE1 | |% | zabbix | * DEEF4D7D88CD046ECA02A80393B7780A63E7E789 | + -- + 4 rows in set (0.00 sec) mysql >

You can see that the database was successfully deployed and permissions have been granted to slave.

B, deploy the slave library

Deploy on the test2 node

Time ansible-playbook mysql_slave_install.yml-extra-vars "host=172.16.1.3 user=root mysql_master_ip=172.16.1.2 mysql_master_port=3306 mysql_master_user=root mysql_master_passwd=E4yR3WnoluSFTCBAI" k

Mysql_master_ip is the ip of mysql master

Mysql_master_port is the mysql port of mysql master

Mysql_master_user is a mysql user of mysql master

Mysql_master_passwd is the password of mysql

If you use my module to deploy the master of mysql, just run the command directly. If not, you just want to create a slave library separately, not the master library, then you don't need to create a mysql master step. You just need to grant the permission to slave in mater, and then run the mysql slave module.

Here's how it works.

[root@puppet ansible] # time ansible-playbook mysql_slave_install.yml-- extra-vars "host=172.16.1.3 user=root mysql_master_ip=172.16.1.2 mysql_master_port=3306 mysql_master_user=root mysql_master_passwd=E4yR3WnoluSFTCBAI"-k SSH password: PLAY [172.16.1.3] * * * GATHERING FACTS * * ok: [172.16.1.3] TASK: [common | Install initializtion require software] * * changed: [172.16.1.3] TASK: [mysql_slave_install | Copy Mysql Software To Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Create Mysql User In Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave _ install | Copy Mysql Start Script To Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Copy Install Mysql Script To Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Copy Mysql Config To Redhat Client] * changed: [172.16.1.3] TASK: [mysql _ slave_install | Copy Mysql Security Script To Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Create Mysql Install Dir] * ok: [172.16.1.3] TASK: [mysql_slave_install | Uncompression Mysql Software To Redhat Client] * changed: [172.16.1.3] TASK : [mysql_slave_install | Modify Mysql Dir Permission In Redhat Client] * ok: [172.16.1.3] = > (item=/data/mysql/datadir) ok: [172.16.1.3] = > (item=/data/mysql/basedir) TASK: [mysql_slave_install | Install Mysql Script In Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Wait Untils Mysql Service Avaiable In Redhat Client] * * changed: [172.16.1.3] TASK: [mysql_slave_install | Start Myql Security Script In Redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Add Boot Start Mysql Service In Redhat Client] * * changed: [172.16.1.3] TASK: [mysql_slave_install | Copy Mysql Create Slave Script To Redhat Client] * * * changed: [172.16.1.3] TASK: [mysql_slave_install | Create Mysql Master And Slave In redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Delete Create Mysql Master And Slave Script In redhat Client] * changed: [172.16.1.3] TASK: [mysql_slave_install | Delete Mysql compression Software In Redhat Client] * * changed: [172.16.1. 3] PLAY RECAP * 172.16.1.3: ok=19 changed=16 unreachable=0 failed=0 real 2m59.966s user 0m14.413s sys 0m0.987s

Deployment is complete, let's test it.

15:26:22 # netstat-tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 172.16.1.3 only servers 3306 0.0.0.0 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp * LISTEN-tcp 0 0 0.0.0.0 only servers 22 0 .0.0.0: * LISTEN 8/sshd tcp 0 0: 80:: * LISTEN 14/httpd tcp 0 0: 22:: * LISTEN 8/sshd root@4ac2891ba3fd:~ 15: 26:27 # mysql-h 172.16.1.3-u root-p Enter password: Welcome to the MySQL monitor. Commands end with; or\ g. Your MySQL connection id is 16 Server version: 5.5.21-log Source distribution Copyright (c) 2000, 2011, 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 databases; +-+ | Database | +-+ | information_schema | | mysql | | performance_schema | +-+ 3 rows in set (0.01 sec) mysql > show processlist +-+ -+ | Id | User | Host | db | Command | Time | State | | Info | Rows_sent | Rows_examined | Rows_read | +-+-- | -- + | | 11 | system user | | NULL | Connect | 192 | Waiting for master to send event | NULL | 0 | 0 | 1 | | 12 | system user | | NULL | Connect | 192 | Slave has read all relay log | Waiting for the slave O thread to update it | NULL | 0 | 0 | 1 | | 14 | root | 172.16.1.3 NULL 53445 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 4 | +-+ -- + 3 rows in set (0.00 sec) mysql > show slave status\ G * * 1. Row * * Slave_IO_State: Waiting for master to send event Master_Host: 172.16.1.2 Master_User: mysql_sync Master_Port: 3306 Connect_ Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1663 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 253 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: 1663 Relay_Log_Space: 409 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: 21 row in set (0.00 sec)

You can see that the slave database is created successfully, and the master and slave (sql and I states are also yes) are created successfully.

C, test master-slave status

Create a database mysql_master in master and create a table test to insert data

[root@puppet ansible] # ssh 172.16.1.2 root@172.16.1.2's password: Last login: Thu Mar 5 15:02:39 2015 from 10.10.11.125 root@b8d17d0f3941:~ 15:27:20 # mysql-h 172.16.1.2-u root-p Enter password: Welcome to the MySQL monitor Commands end with; or\ g. Your MySQL connection id is 17 Server version: 5.5.21-log Source distribution Copyright (c) 2000, 2011, 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 > create database mysql_master; Query OK, 1 row affected (0.00 sec) mysql > use mysql_master Database changed mysql > create table test (id int,name varchar (4)); Query OK, 0 rows affected (0.03 sec) mysql > desc test +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | id | int (11) | YES | | NULL | name | varchar (4) | YES | | NULL | | +-+-+ 2 rows in set (0.00 sec) mysql > insert into test value (1) 'test1') Query OK, 1 row affected, 1 warning (0.00 sec) mysql > insert into test value; Query OK, 1 row affected, 1 warning (0.00 sec) mysql > select * from test; +-+ | id | name | +-+ | 1 | test | 2 | test | +-+-+ 2 rows in set (0.00 sec)

And then check it from the library.

[root@puppet ansible] # ssh 172.16.1.3 root@172.16.1.3's password: Last login: Thu Mar 5 15:25:06 2015 from 10.10.11.125 root@4ac2891ba3fd:~ 15:31:05 # mysql-h 172.16.1.3-u root-p Enter password: Welcome to the MySQL monitor Commands end with; or\ g. Your MySQL connection id is 17 Server version: 5.5.21-log Source distribution Copyright (c) 2000, 2011, 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 databases +-+ | Database | +-+ | information_schema | | mysql | | mysql_master | | performance_schema | +-+ 4 rows in set (0.00 sec) mysql > use mysql_master Reading table information For completion of table and column names You can turn off this feature to get a quicker startup with-A Database changed mysql > show tables +-+ | Tables_in_mysql_master | +-+ | test | +-+ 1 row in set (0.00 sec) mysql > select * from test +-+-+ | id | name | +-+-+ | 1 | test | | 2 | test | +-+-+ 2 rows in set (0.00 sec)

You can see that the slave database also synchronizes the data, and the mysql master and slave is created successfully.

4. Module address

Mysql master address

Https://github.com/dl528888/ansible-examples/tree/master/mysql_master_install

Mysql slave address

Https://github.com/dl528888/ansible-examples/tree/master/mysql_slave_install on "how to use ansible to create mysql master and slave" this article to share here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report