当前位置:网站首页>Docker container implements MySQL master-slave replication
Docker container implements MySQL master-slave replication
2022-07-28 15:44:00 【A cat that can't modulate and demodulate】
Catalog
Create a new master server container instance 3307
Get into /mydata/mysql-master/conf newly build my.cnf
Restart after modifying the configuration master example
Get into mysql-master Containers
master Create a data synchronization user in the container instance
Create a new instance from the server container 3308
Get into /mydata/mysql-slave/conf newly build my.cnf
Restart after modifying the configuration slave example
View the master-slave synchronization status in the master database
Get into mysql-slave Containers
Configure master slave replication in the slave database
View the master-slave synchronization status in the slave database
Start master-slave synchronization in the slave database
Check the status of the slave database again
Use Docker install MySQL5.7 Detailed article :Linux Use in Docker install MySQL5.7(CentOS7)_ Blog of cat who can't modulate and demodulate -CSDN Blog
Create a new master server container instance 3307
Even if there is no pull here mysql5.7 The image of can also be run directly , If no image is found, it will be pulled directly and automatically .
there -v If you don't understand , You can also view the previous container data volume details :Docker Container data volume _ Blog of cat who can't modulate and demodulate -CSDN Blog
docker run -p 3307:3306 --name mysql-master -v /mydata/mysql-master/log:/var/log/mysql -v /mydata/mysql-master/data:/var/lib/mysql -v /mydata/mysql-master/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
ps Check it out. , Make sure it's up
docker ps
Get into /mydata/mysql-master/conf newly build my.cnf
vim my.cnfDirectly copy and paste all the following contents , Then save to exit .
[mysqld]
## Set up server_id, You need to be unique in the same LAN
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 (mixed,statement,row)
binlog_format=mixed
## Binary log expiration cleanup time . The default value is 0, Does not automatically clean up .
expire_logs_days=7
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave End copy interrupt .
## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database data is inconsistent
slave_skip_errors=1062Restart after modifying the configuration master example
Modify the configuration through the container data volume on the host , Conduction to docker Inside , It needs to be restarted
docker restart mysql-master 
Again ps Check it out. , Make sure it has up up
docker ps
Get into mysql-master Containers
docker exec -it mysql-master /bin/bashmysql -uroot -proot
Container entered successfully , Move on to the next step
master Create a data synchronization user in the container instance
Safety reinforcement , Add a user , Then authorize
create user 'slave'@'%' identified by '123456';grant replication slave,replication client on *.* to 'slave'@'%';
Create a new instance from the server container 3308
docker run -p 3308:3306 --name mysql-slave -v /mydata/mysql-slave/log:/var/log/mysql -v /mydata/mysql-slave/data:/var/lib/mysql -v /mydata/mysql-slave/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7ps To confirm that up

Get into /mydata/mysql-slave/conf newly build my.cnf
Paste directly, save and exit
[mysqld]
## Set up server_id, You need to be unique in the same LAN
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 , in preparation for Slave As an instance of another database Master When using
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 (mixed,statement,row)
binlog_format=mixed
## Binary log expiration cleanup time . The default value is 0, Does not automatically clean up .
expire_logs_days=7
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave End copy interrupt .
## Such as :1062 An error is a duplicate of some primary keys ,1032 The error is because the master-slave database data is inconsistent
slave_skip_errors=1062
## relay_log Configure relay logs
relay_log=mall-mysql-relay-bin
## log_slave_updates Express slave Write the copy event to your own binary log
log_slave_updates=1
## slave Set to read only ( have super Except for users with permission )
read_only=1Restart after modifying the configuration slave example
docker restart mysql-slave
View the master-slave synchronization status in the master database
show master status;
Get into mysql-slave Containers
docker exec -it mysql-slave /bin/bash
mysql -uroot -proot

Configure master slave replication in the slave database
Is configuring from the database , It's not the Lord !
change master to master_host=' The host machine ip', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
change master to master_host='192.168.150.30', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;Parameter description :
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 .

View the master-slave synchronization status in the slave database
show slave status \G;\G It's standing up to check , It is similar to key value pairs
there Slave_IO_Running and Slave_SQL_Running Are still NO, Has not started

Start master-slave synchronization in the slave database
start slave;

Check the status of the slave database again
show slave status \G;

success .
Master slave replication test
Master database
The main database is newly built , new table , insert data
create database dbtest1;
use dbtest1;
create table test1 (id int,name varchar(20));
insert into test1 values(1,'van');
select * from test1;

From database
Search from the database
show databases;
use dbtest1;
select * from test1;
Test success !
边栏推荐
- Vs usage skills
- try...except异常处理语句(6)
- Tencent interview -- please design a thread pool to implement sequential execution
- Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储
- Easyexcel complex header export (one to many)
- How to obtain and embed go binary execution package information
- Samba Server Setup Guide
- 1200 times faster! MIT develops a new generation of drug research and development AI, and suspends the old model
- Qt信号与槽的五种连接方式
- 如何搭建openGrok代码服务器
猜你喜欢

How to obtain and embed go binary execution package information

2022年最火的十大测试工具,你掌握了几个

关于Simulink如何生成模型覆盖率报告

使用Mock技术帮助提升测试效率的小tips,你知道几个?

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

堆操作

有奖活动分享:使用WordPress搭建一个专属自己的博客后最高可领取iPhone13

800V high voltage system

程序员的自我修养

Endnote 与word关联
随机推荐
Matlab导出高清图片、且Word中压缩不失真、转换PDF不失真
Explain the difference set, intersection set and union set of complex type set in detail.Net
20. Channel allocation task implementation
8. Realization of real-time data backup and real-time clock function
10. Implementation of related data accumulation task
7. Definitions of real-time data backup and real-time clock
【删除指定数字——leetcode]
融云实时社区解决方案
一次失败的破解经历
Monkey stress test
Editor in ArcGIS Pro
21. Definition of message processing task
EasyExcel复杂表头导出(一对多)
生命的感悟
Getting started with crawlers (1) -- requests (1)
22. Realization of message processing task
PXE网络装机
Vs dynamic library debugging
字符数组和字符串的区别
Try... Except exception handling statement (6)