Construction of dual master mysql using docker

  Sometimes we need to double main mysql, so no matter which database there is a problem, then can continue to use the database. The two databases linked to a top agency, which services such a problem, the other can continue the service. Of course, this article is not to talk about the main agency, so this is not elaborate.

  Because of the limited resources of our computer, we continue to use all forms of docker for processing.

  My first computer was installed mac and docker, if some students are not installed, then you need to install the advance. Coupled with domestic accelerate mirror sites, so when we pull the image of the service will be completed soon, otherwise the mirror needs to pull from abroad is very slow. I encountered this problem before, then put docker placed for a long time, the country is now configured to accelerate the mirror sites, pull the mirror very quickly.

  Prior to the basics docker in my blog it has been said, is not clear can be "ticket" in the past.

  Well, let's get to the point, everyone's ready docker environment. We pull mirroring mysql 5.7 version, you can look after themselves what pulls complete mirror.

docker pull mysql:5.7
docker images

  Next we need to create a network environment, let the Lord from the machine in the same environment. It can be ordered: docker network ls to see what created their own network. Here we create a network with the name mysql.

docker network create --driver=bridge mysql

  Let's get started master-slave machine.

docker run -p 3366:3306 --network=mysql -h "mysql_master"  --name mysql_master -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
docker run -p 3367:3306 --network=mysql -h "mysql_slave"  --name mysql_slave -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7

  Parameters on top of probably explain -p is exposed out of the port inside the machine, this can be connected from the outside mysql machine. -h host name, --name name specified for the start of the container, this can be used to restart the mirror in the follow-up, such as: docker restart mysql_master. -E time specified for creating an environment variable, because the containers must specify the root mysql password, so set by -e, the password is set to root. -d deamon this is the meaning of that daemon is running in the background. Finally, specify the version number is used to start and mysql.

  Next we need to enter into mysql_master machine. docker exec is used to enter the machine is running, -it is an interactive way, and finally specify the script type bash.

docker exec -it mysql_master /bin/bash

  After entering into the machine, because mysql performed very clean, not inside the tools we need. The kernel is mirrored debian, we need vim and ping tools installed. We need to change the apt sources, first of all the source files to back up, then set the source 163 as our apt source. Of course, you can search for other cloud apt sources are possible.

cp /etc/apt/sources.list /etc/apt/sources_init.list

echo "deb http://mirrors.163.com/debian/ buster main non-free contrib
deb http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

deb-src http://mirrors.163.com/debian/ buster main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib" > /etc/apt/sources.list

  We update the look apt-get, and then install the vim and ping, perform finished tool will be installed.

apt-get update
apt-get install vim
apt-get install iputils-ping

  These updates installation tool that is apt methods also need to slave in perform again, I'm not made a single step. We can test the ping command, we execute the "ping mysql_slave" has a return value, indicating that the network is through significant. We continue to edit the mysql configuration file.

vim /etc/mysql/mysql.conf.d/mysqld.cnf

  We configured the server-id and log-bin, mysql is synchronized, this is open log-bin by bin log. Each server-id of the machine is not the same for different signs machine.

server-id=1
log-bin=mysql-bin

  After saving we exit mirror, and then restart the mirror so that it takes effect.

docker restart mysql_master

  We also need to slave machine files change it. Here enter the machine's command: docker exec -it mysql_slave / bin / bash. Remember to restart the slave after performing at the top of updates and installation configuration, good distribution: docker restart mysql_slave.

server-id=2
log-bin=mysql-bin
binlog_format=mixed

  We enter in mysql master inside the machine, the command is as follows. Enter the root password to enter.

mysql -uroot -p

  We need the authorization of replication and slave root user privileges. If not authorized, then the library is not connected to the main library.

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'root'@'%';

  Look at the master log file that is location. We see the master machine's file name and location.

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

  We also need to open a slave of the machine and into the mysql inside. Because you may need to switch back and forth. We execute the following command from the library inside. Configuration settings inside the master's hostname, port number of the host, the host user name, password, host's file name and location.

change master to master_host='mysql_master',master_port=3306, master_user='root',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=154;

  Next we start the slave, and the slave thread to see orders.

start slave;
show slave status \G;

  

 

    yes Description inside has opened successfully and in communication, if there is no communication with the word is NO, this time need to check the host user name and password configured on top of the host, which needs to be examined again.

  We built a database on the master machine test - create database test; then we see from inside the library, already have. Description from the library has been created.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| slavetest          |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

  Next we we mysql_slave machine libraries to user root authorization.

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'root'@'%';

  Then look at the log file name and location mysql_slave machine.

show master status \G;

  We find the file name and location corresponding to the following command to look at the two values ​​are correct, incorrect words can be replaced, then enter the correct implementation of the mysql_master machine.

change master to master_host='mysql_slave',master_port=3306, master_user='root',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=154;

  After performing on the execution finished mysql_master.

start slave;

  Then look at the slave state. Note If you are just as words like Yes on chart success.

show slave status \G;

  Next you create a table on mysql_slave machine. create database slavetest; this time we look into the mysql_master machine.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| slavetest          |
| sys                |
| test               |
+--------------------+

  This time has also been described double main mysql completed structures.

 

  

 

  

  

Guess you like

Origin www.cnblogs.com/huangqingshi/p/12593395.html