Mysql master-slave configuration based on Docker

 Master-slave architecture background

  • Data security, data backup can be performed;
  • Separation of reading and writing, for most business systems, more reading and less writing. If the access pressure is too high, the request can be distributed to the slave server, thereby alleviating the pressure of database access;
  • Failover (high availability), when the primary node goes down, it will switch from the service to the primary node to continue providing services.

Synchronization principle

Mysql's master-slave architecture is generally implemented through binlog logs, which record every operation of the master library. After the slave library and the main library establish a TCP connection, the main library is requested to transfer the binlog. At this time, another dump thread of the main library transfers the binlog to the main library. The binlog log read from the library is written into its own relaylog, and another thread reads the content in the relaylog for replay.

First, the benefits of Docker construction

  • Resources are limited and more hosts are required;
  • Virtual machine construction requires machine configuration, and the steps to install mysql are cumbersome;
  • Multiple Docker containers can be installed and run on one machine;
  • Docker containers are independent of each other, have their own IP, and do not affect each other;
  • Docker is easy to use and starts a container in seconds;

2. Docker construction steps

2.1 Install mysql

docker pull mysql

Default latest version

2.2 Start the master-slave container

Use the msyql image to start the container, and start the master and slave containers respectively;

  •  -p: map port
  • --name: Indicates the name of the container
  • -e MYSQL_ROOT_PASSWORD: Set the mysql password, a parameter that must be set;
  • -d mysql Tag: Tag represents the mirror id;
  • Master
docker run -p 3339:3306 --name mysql-master -e MYSQL_ROOT_PASSWORD=123456 
-d mysql:latest
  • Slave (from)
docker run -p 3340:3306 --name mysql-slave1 -e MYSQL_ROOT_PASSWORD=123456 
-d mysql:latest
  • The externally mapped port of the Master is 3339, and the externally mapped port of the Slave is 3340. Because docker containers are independent of each other, each container has an independent IP, so different containers using the same port will not conflict.

Navicat Test Tool: Connection succeeded! 

 2.3 Configure Master

1) Enter the container

  • Enter through the docker exec -it mysql-master /bin/bash command;
  • mysql-master: container name or id also works;

2) switch directory

  • cd /etc/mysql

3) Edit the my.cnf file

  • vim my.cnf

 If an error is reported,

bash: vim: command not found

Execute the following command,

  • apt-get install vim

If it still reports an error, you need to update apt-get and execute the following command

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim
  • apt-get update, and then perform the previous step to complete the installation;

4 ) Add the following configuration in my.cnf

[mysqld]

## 同一局域网内要唯一
server-id=100

## 开启二进制日志功能,可以随时取消
log-bin=mysql-bin

5) Restart the mysql-master container

  • docker restart mysql-master
  • docker exec -it mysql-master /bin/bash:运行mysql

6) Configure the slave library and synchronize data

  • Enter mysql: mysql -u root -p, enter the password;
  • create user 'slave'@'%' identified by '123456';
  • grant replication slave,replication client on *.* to 'slave'@'%';
  • flush privileges; refresh

success!

Syntax format: CREATE USER <user name> [ IDENTIFIED ] BY [ PASSWORD ] <password>

<username>

Specifies to create a user account in the format 'user_name'@'host_name'. Here user_nameis the user name, which host_nameis the host name, that is, the name of the host where the user connects to MySQL. If during the creation process, only the user name of the account is given, but the host name is not specified, the host name defaults to "%", indicating a group of hosts.

PASSWORD Password, optional, is used to specify the hash password, that is, if the password is set in clear text, the PASSWORDkeyword needs to be ignored.

   IDENTIFIED             BY

It is used to specify the password corresponding to the user account. If the user account has no password, this clause can be omitted.
<password> Specifies the password for the user account, after the IDENTIFIED BYkeyword or PASSWOEDkeywords. The given password value can be either plaintext consisting of only letters and numbers, or a hash value obtained by the PASSWORD() function.

Syntax format: grant replication

For the newly created MySQL user, it must be authorized, and the GRANT statement can be used to realize the authorization of the newly created user.

  • replication slave: With this permission, you can view the slave server and read the binary log from the master server; only by granting this permission can replication really work;
  • Replication client: With this permission, you can query the status of the master server and slave server; only by granting this permission can replication really work;

2.4 Configure Slave (slave)

Same as Maser (main) configuration, omitted before!

1) Add in the Slave configuration file my.cnf

[mysqld]

## 同一局域网内要唯一
server-id=101

## 开启二进制日志功能,以备份Slave作为其他Slave的Master时使用
log-bin=mysql-slave-bin

## 配置中继日志
relay_log=edu-mysql-relay-bin

2) Restart mysql-slave1

2.5 Connect Master and Slave

1) Enter mysql-master

Excuting an order:

show master status;

The values ​​of the File and Position fields will be used later;

2) Enter mysql-slave1

Excuting an order:

change master to master_host='172.17.0.3',master_user='slave',
master_password='123456',master_port=3306,
master_log_file='mysql-bin.000005',
master_log_pos=0,master_connect_retry=10,
get_master_public_key=1;
  • master_host: The address of the Master, which is the opposite IP of the container, which can be viewed with the following command
docker inspect --format='{
    
    {.NetworkSettings.IPAddress}}' 容器名称|容器id

  • master_port: The port number of the Master, which is the port number of the container;
  • master_user: the user used for data synchronization;
  • master_password: the password used to synchronize the user;
  • master_log_file: Specifies which log file the Slave starts to copy data from, that is, the value of the File field mentioned above;
  • master_log_pos: Which Position to start reading from, that is, the value of the Position field mentioned above;
  • master_connect_retry: If the connection fails, the retry interval, the unit is seconds, the default is 60 seconds;

Remember to add get_master_public_key=1, because RSA encryption is required to transmit passwords above mysql8.0.

Notice:

If you use the container's IP ( master_host ) and PORT ( master_port ), it should be replaced with the host's IP and mapped to an external port, so as to avoid errors. for example

 3) View the master-slave synchronization status

  • Enter the mysql terminal in the slave to execute the command:
show slave status \G;

  • Under normal circumstances, both SlaveIORunning and SlaveSQLRunning are No, because the master-slave replication process has not been started. Use the following command to start master-slave replication:
start slave

# 此外 若想关闭则 stop slave;即可
  • Query the master-slave replication status again show slave status \G;

Has become Yes!

2.6 Master-slave replication troubleshooting

After using start salve to enable master-slave replication, if SlaveIORunning is always Connecting, it means that master-slave replication has always been connected. This situation is generally caused by the following reasons, which can be ruled out according to the Last_IO_Error prompt.

1) Network failure 

  • Check ip, port

2) The password is incorrect

  • Check that the user to be created for synchronization and the user password are correct

3) pos is not right

  • Check the Master's Position

4) The problem of mysql version

  • mysql8.0 or above password encryption, you need to add it when granting,
因为mysql8.0以上需要使用RSA加密来传输密码

2.7 Test master-slave replication

Create a database on the Master, and then check whether the database exists on the Slave

  • Master

  • Slave

Guess you like

Origin blog.csdn.net/Jiangtagong/article/details/123134137
Recommended