当前位置:网站首页>Implementing MySQL master-slave replication in docker
Implementing MySQL master-slave replication in docker
2022-06-26 11:02:00 【Jaffy】
List of articles
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 
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


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/bashLog in to mysql
mysql -uroot -p123456master 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;

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/bashSign in MySQL
mysql -uroot -p123456Configure 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;
Turn on master-slave synchronization
start slave;

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

Then create a new database in the main database called mydata

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

边栏推荐
- Notes - simple but adequate series_ KVM quick start
- Origin of b+ tree index
- Oracle11g reports an error when starting the database ora-27154: post/wait create failed
- 【深度学习理论】(6) 循环神经网络 RNN
- Redis knowledge mind map
- MySQL 9th job - connection Query & sub query
- AIX basic operation record
- Reasons for "unresolved external symbols" during vs or QT compilation link:
- 使用‘百家饭’自动生成API调用:JS部分进展(二)
- 用同花顺手机炒股是安全的吗?如何用同花顺炒股
猜你喜欢

Linux下安裝Mysql【詳細】

sliding window

How does unity prevent other camera positions and rotations from being controlled by steamvrplugin when using steamvrplugin

MySQL 12th job - Application of stored procedure

QT connection MySQL data query failed

DataBinding使用与原理分析

Jasperreports - print PDF (project tool)

MySQL 10th job - View
![[echart] i. how to learn echart and its characteristic document reading notes](/img/21/5405ae302df77d2ba07d9f5e5537f3.jpg)
[echart] i. how to learn echart and its characteristic document reading notes

即构「畅直播」上线!提供全链路升级的一站式直播服务
随机推荐
互联网对抗神器之漏洞扫描与反渗透
Query online users and forced withdrawal users based on oauth2
MySQL performance monitoring and SQL statements
Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d
wangEditor 上传本地视频修改
[deep learning theory] (6) recurrent neural network RNN
Swiftui development experience: data layer of application design for offline priority
最强swarm集群一键部署+氢弹级容器管理工具介绍
laravel 使用 OSS时拼接 完整路径 及 laravel-admin 的多图片上传
Server single and two-way adjustable one key mutual trust script!
2、 Linear table
SQL Server 基础介绍整理
Jasperreports - print PDF (project tool)
Code specification & explain in detail the functions and uses of husky, prettier, eslint and lint staged
Redis (basic) - learning notes
[echart] i. how to learn echart and its characteristic document reading notes
mysql性能监控和sql语句
Getting started with postman
【软件项目管理】期末复习知识点整理
Common regular expressions - tool classes (mobile number, email, QQ, fax)