当前位置:网站首页>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/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;
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;
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 .
边栏推荐
- Is it safe to use flush mobile phones to speculate in stocks? How to fry stocks with flush
- (Typora图床)阿里云oss搭建图床+Picgo上传图片详细教程
- ceph运维常用指令
- JWT (SSO scheme) + three ways of identity authentication
- OpenCV图像处理-灰度处理
- SQL Server foundation introduction collation
- Cereals Mall - Distributed Advanced
- MySQL Performance Monitoring and SQL statements
- Is it safe for compass software to buy stocks for trading? How to open an account to buy shares
- MySQL 9th job - connection Query & sub query
猜你喜欢
MySQL 8th job
哪些PHP开源作品值得关注
Postman入门教程
MySQL模糊查询详解
Qixia housing and Urban Rural Development Bureau and fire rescue brigade carried out fire safety training
Concise course of probability theory and statistics in engineering mathematics second edition review outline
Using reflection to export entity data to excel
[Beiyou orchard microprocessor design] 10 serial communication serial communication notes
Linux下安装Mysql【详细】
[echart] II. User manual and configuration item reading notes
随机推荐
Which PHP open source works deserve attention
sysbench基础介绍
Getting started with postman
[difficult and miscellaneous diseases] @transitional failure summary
VS或Qt编译链接过程中出现“无法解析的外部符号”的原因:
wangEditor 上传本地视频修改
The sixth MySQL job - query data - multiple conditions
Is it safe for compass software to buy stocks for trading? How to open an account to buy shares
磁带库简单记录1
The difference between NPM and yarn
AIX基本操作记录
02 linked list of redis data structure
CentOS安装Redis多主多从集群
Using reflection to export entity data to excel
CentOS installs redis multi master multi slave cluster
Svn installation configuration
MySQL Performance Monitoring and SQL statements
Grain Mall - High Availability Cluster
2、 Linear table
Installer MySQL sous Linux [détails]