The same server emulates the master/slave synchronization Settings of Mysql Settings using Docker.
1, run two Mysql: masterMysql (master library), slaveMysql (slave library), database version 5.7.
Docker run -p 13306:3306 --name slavemysQL -e MYSQL_ROOT_PASSWORD=123456 -d Mysql :5.7 docker run -p 13307:3306 --name mastermysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7Copy the code
Note The service ports mapped to the host are 13306 and 13307 respectively. Ensure that the firewall and gateway Settings are enabled for external connection.
2. Check the used IP address of the container (the IP address of the primary and secondary containers must be on the same Intranet)
# to check the main library's IP docker inspect - format = '{{. NetworkSettings. IPAddress}}' masterMysql # 172.18.0.2Copy the code
3. Configure the primary and secondary libraries.
(1) Enter the main library
Docker exec -it masterMysql bash #masterMysqlCopy the code
(2) Configure the configuration file
cd /etc/mysql&&ls
Copy the code
You can see several *.cnf files, including conf.d and mysql.conf.d folders. This has a load order, so the configuration is done on my.cnf.
(3) Configure the configuration file
A:
Normal operation is to enter the folder to edit, as follows:
Bash: vi: command not found bash: vi: command not foundCopy the code
Install the VI first.
Apt-get install vimCopy the code
Method 2 (Suggestion) :
Edit it outside the container and copy it inside the container.
Copy the host to the container. Docker cp [host address] [container ID or container name]:[container file address] docker cp /home/mysql.my.cnf mysql:/etc/ mysql.my.cnfCopy the code
(4) Enable Binlog configuration.
Same configuration: go to the master and slave libraries and set up the my.cnf configuration file. Notice that the server ids must be inconsistent.
Character_set_server =utf8 init_connect='SET NAMES UTF8 '# character_set_server= UTf8 init_connect='SET NAMES UTF8' Rely on this to distinguish between SQL statements that execute Binlog) # can fetch the last Ip. The primary library is 2 and the secondary library is 3. Mysql > set master-bin = master-bin; mysql > set master = master-bin; The slave library is slave-bin log-bin=master-bin log_bin_index = master-bin.indexCopy the code
The different configurations are as follows:
Here is the collation of configurable content, for testing, first comment. # to synchronize the library to the slave machine (if not, #binlog-ignore-db=information_schema #binlog-ignore-db=information_schema Expire_logs_days =15 #binlog_format=row # SQL to set the actual operation of Binlog records. #replicate_ignore_table=igoreTest # ignore the replicated table Replicate_wild_ignore_db =test #replicate_wild_ignore_db=igoreTest Wildcard characters are allowedCopy the code
From the library:
Set to prevent replication errors from master/slave libraries due to delayed update or restart. Read_only = 1 master_info_repository=TABLE relay_log_info_repository=TABLE relay-log = slave relay-bin relay-log-index = slave-relay-bin.indexCopy the code
The configuration takes effect after the container is restarted.
docker restart masterMysql
docker restart slaveMysql
Copy the code
View the container logs:
docker logs masterMysql
Copy the code
If the restart fails, perform this operation to check whether any error occurs in the configuration file. Use method 2 (3) to modify the configuration file and start the container.
(5) Check whether the Binlog configuration is successful.
Mysql -uroot -p123456 Mysql -uroot -p123456 show variables like '%log_bin%'; /var/lib/ mysql.master-bin = /var/lib/ mysql.master-bin = /var/lib/ mysql.master-bin = /var/lib/ mysql.master-bin = /var/lib/ mysql.master-bin [file](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/61e52585e4aa4200a8c27204ef690234~tplv-k3u1fbpfcp-zoom-1.image)Copy the code
(6) Check the Binlog status of the Matser node of the master library and get the position value configured here.
show master status;
Copy the code
If the previously enabled Binlog is not in use, it needs to be generated or reset to clear.
Create a new Binlog file flush logs; Reset master;Copy the code
The log configuration file is master-bin.000001 and Position is 334.
4. Set an account for the master library to synchronize data with the slave library. The user name is slaveMysql.
CREATE USER 'slaveMysql'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slaveMysql'@'%';
Copy the code
5, login from Mysql library execution, it is recommended to connect to Navicat to run and view, the server view format is very messy.
(1) Perform synchronized master library configuration. Notice that this is the closing sign.
Change master to master_host='172.18.0.2', master_user='slaveMysql', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 334, master_connect_retry=30;Copy the code
Parameter description:
Change master to master_host=${IP address used by master_host} Master_port Indicates the Master port number. It is the port number of the container's running database, not the port number mapped to the host. Master_user sets the account to be used for synchronization, master_password sets the account password to be used for synchronization, and master_log_file specifies which configuration file to read Binlog logs from the master library. The Position from which master_log_pos (3 (6) starts reading, in seconds, is master_connect_retry if the connection fails. The default is 60 secondsCopy the code
(2) Open the slave library configuration.
start slave; # Stop slave. #stop slave;Copy the code
(3) View the master/slave synchronization status. Check the status of the master/slave synchronization.
show slave status ;
Copy the code
(4) Check for errors.
You can view the connection error by viewing Last_Io_Error. Check the following for possible errors. 2. The password of the account used for synchronization is normal. 3.
6. Test whether the master/slave is normal.
Master database create database, check whether the slave database also create a success, and then end.
This article is published by OpenWrite!