Master-slave replication mode
- Based on the log (bin log)
- Based GTID (global transaction identifier)
using the first method this article
Prerequisites
- Master and slave server's operating system version and a bit needs to be consistent
- Main mysql version and from mysql version needs to be consistent
- From the master database in the data needs to be consistent
- Master database open binary logs , from the master database and the database server_id in must be unique within the local area network .
Process
1 . First pull mirroring
docker pull mysql:5.7.26
2 . Create a container
Master (main)
docker run -p 3340:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=
123456 -d mysql:5.7.26
Slave (from)
docker run -p 3341:3306 --name mysql-slave -e MYSQL_ROOT_PASSWORD=
123456 -d mysql:5.7.26
-Name specify a name for the container, and this is mysql-master-Slave MySQL
-p specified container port mapping to the specified port of the host, where the container 3341 is mapped to the host port 3342 and port 3306
-e environment provided variable here is the designated root account password is 123456
-d background container and return the container ID
MySQL: MySQL version 5.7.26 running specified
Use docker ps
the command to view the containers are running:
3 . Configuration Master (Main)
by first docker exec -it mysql-master /bin/bash
entering the interior of the container
then first apt-get update
, after completion apt-get install vim
immediately vim /etc/mysql/my.cnf
add the following configuration
[mysqld]
## 同一局域网内注意要唯一,最好用ip段后缀来区分
server-id= 2
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
After configuration, Mysql need to restart the service, service mysql restart
restart the service will lead to docker container stop, as long as the docker start mysql-master
start of the container to
the next, the new slave in the Master user and grant permissions
CREATE USER 'slave'@'%'IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
- Configuring the Slave (from)
entering the same mysql-slave above step, then add follows my.cnf:
[mysqld]
## 设置server_id,注意要唯一
server-id=3
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=mysql-relay-bin
You configure the same restart.
5 . Link Master (primary) and Slave (from)
entering the Master mysql, the implementation of show master status;
which the File and Position will be used, etc. , can make use of these two find the corresponding binary read information from the library
6 . Get the address of the Master , refers to the independent ip container by docker inspect --format='{{.NetworkSettings.IPAddress}}'
container name | ip container id query container, we need to use.
7 . Continued enter mysql in Slave, do the following:
change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos=1299, master_connect_retry=30;
Command Description:
MASTER_PORT: Master port number, port number refers to a container
master_user: user data for synchronization
master_password: used to synchronize the user's password
master_log_file: Specifies Slave start copying data from which log file, i.e., File field value in the above-mentioned
Position from which to start reading the value, i.e., the above-mentioned fields Position: master_log_pos
master_connect_retry: If the connection fails, retry interval, in seconds, default is 60 seconds
Slave executed in the show slave status \G;
view from the master replication status
Do not worry, there are circles under normal circumstances show NO, because no open master-slave replication, I have opened here only display YES. Now just start slave
, again you can see two queries are a YES. Then you can test whether the data synchronization is successful, there is not demonstrated.
8. error investigation
if your Slave_IO_RUNNGING shows connecting, then you can slip below view Last_IO_Error
, under normal circumstances because of error:
- Port or IP address error, you could not turn on the firewall, or if you are also Ali cloud server, then remember to open security group in advance.
- The password is wrong, confirm the master-slave synchronization of user and password are correct.
- FILE POS and wrong, then this check
- The master-slave
my.cnf
configuration file may be configured wrong, check again.
9. writing shell scripts
with nagios
surveillance monitor YES slave of two (Slave_IO and Slave_SQL process), if one of them is no, indicates that the main problem from there, then send a text message alert!