当前位置:网站首页>Implementing MySQL master-slave replication in docker

Implementing MySQL master-slave replication in docker

2022-06-26 11:02:00 Jaffy

Preface

We know , Use Docker It can be carried out conveniently MySQL Pull , start-up . Compared with the traditional installation method , stay Docker Use in MySQL It's so convenient , The content of this article is to learn how to Docker In the middle of MySQL Master-slave replication of , Because in a real production environment , At least one master and one slave mode is needed . If my friends have free time , You can still follow the steps , Give the operation a try . If you haven't tried Docker Install in MySQL My friends can quickly follow the following articles to install the previous steps .

Docker install MySQL Detailed steps of mirror image ( Suitable for beginners )

install

Mirror image

What we use here is MySQL:5.7 The mirror version of
 Insert picture description here

Preparation container

First of all, we have to think about preparing several containers . Here we are for convenience , Just one master and one follower , To be created later MySQl The name of the container is mysql_master and mysql_slave, The port is specified as 3306 and 3307, It is stipulated that the attached directories are placed in /mydata/mysql Under .

Create a new master server
 docker run -d -p 3306:3306 --name mysql_master -e MYSQL_ROOT_PASSWORD=123456 -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql  mysql:5.7

 Insert picture description here

 Insert picture description here

modify my.cnf

stay /mydata/mysql-master/conf Create a new folder my.cnf file , Then paste the following content into it .

[mysqld]
##  Set up serverid, The same LAN should be unique 
server_id=101
## Specify the name of the database that does not need to be synchronized 
binlog-ignore-db=mysql
## Turn on binary log function 
log-bin=mall-mysql-bin
## Set the memory size of binary log ( Business )
binlog_cache_size=1M
## Set the binary log format to use 
binlog_format=mixed
## Binary log expiration cleanup time 
expire_logs_days=7
## Skip all errors in master-slave replication or errors of the specified type , avoid slave End copy interrupt 
###1062 Duplicate primary key ,1032 Main weight data is inconsistent 
slave_skip_errors=1062
Restart the master server
docker restart mysql_master
Enter the main server
  • Enter the container

    docker exec -it mysql_master /bin/bash
    
  • Log in to mysql

    mysql -uroot -p123456
    
  • master Create a data synchronization user in the container instance

    CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
    GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
    
View the master-slave synchronization status in the master database
show master status \G;

 Insert picture description here

The parameters here are critical , When the database is finally configured, you need to obtain the corresponding value from here

Create a new slave server
docker run -d -p 3307:3306 --name mysql_slave -e MYSQL_ROOT_PASSWORD=123456 -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql  mysql:5.7
modify my.cnf
[mysqld]
##  Set up serverid, The same LAN should be unique 
server_id=102
## Specify the name of the database that does not need to be synchronized 
binlog-ignore-db=mysql
## Turn on binary log function 
log-bin=mall-mysql-slave1-bin
## Set the memory size of binary log ( Business )
binlog_cache_size=1M
## Set the binary log format to use 
binlog_format=mixed
## Binary log expiration cleanup time 
expire_logs_days=7
## Skip all errors in master-slave replication or errors of the specified type , avoid slave End copy interrupt 
###1062 Duplicate primary key ,1032 Main weight data is inconsistent 
slave_skip_errors=1062
## Configure relay logs 
relay_log=mall-mysql-relay-bin
## Express slave Write the copy event to your own binary log 
log_slave_updates=1
##slave Set to read only 
read_only=1
Restart the slave server
docker restart mysql_slave
Enter from the server
  • Enter the container

    docker exec -it mysql_slave /bin/bash
    
  • Sign in MySQL

    mysql -uroot -p123456
    
  • Configure the master-slave configuration in the slave database

    change master to master_host='192.168.40.128', master_user='slave', master_password='123456', master_port=3306, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
    

    master_host: The main database IP Address ;
    master_port: The running port of the primary database ;
    master_user: A user account created in the master database to synchronize data ;
    master_password: The user password created in the master database for synchronizing data ;
    master_log_file: Specify the log file to copy data from the database , By looking at the status of the master data , obtain File Parameters ;
    master_log_pos: Specify where to start copying data from the database , By looking at the status of the master data , obtain Position Parameters ;
    master_connect_retry: The connection failed and the time interval between retries , The unit is in seconds .

    The parameters here are defined by show master status; obtain

  • View the master-slave synchronization status in the slave database

    show slave status \G;
    

     Insert picture description here

  • Turn on master-slave synchronization

    start slave;
    

 Insert picture description here

test

use Navicat Premium The connection tool connects to the master database and the slave database respectively .

 Insert picture description here

Then create a new database in the main database called mydata

 Insert picture description here

At this time, it is found that the synchronization from the database is successful .

 Insert picture description here

原网站

版权声明
本文为[Jaffy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260953142201.html