当前位置:网站首页>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 !
边栏推荐
猜你喜欢

Among the three "difficult and miscellaneous diseases" of machine learning, causal learning is the breakthrough | Liu Li, Chongqing University

AS如何不区分大小写去进行智能提示

Matlab exports high-definition pictures without distortion in word compression and PDF conversion

vs动态库调试

PXE网络装机

爆肝整理 JVM 十大模块知识点总结,不信你还不懂

堆操作

Self cultivation of programmers

Docker容器实现MySQL主从复制

关于Simulink如何生成模型覆盖率报告
随机推荐
Easyexcel complex header export (one to many)
如何压缩与解压缩ramdisk.img
Late 2021 year-end summary
关闭独立窗口对其他窗口同时关闭的问题
22. Realization of message processing task
Five connection modes of QT signal and slot
10. Implementation of related data accumulation task
软件架构与设计(八)-----分布式架构
Perception of life
How to turn on and off flight mode through ADB
VS使用技巧
Matlab does not overwrite importing Excel
Sharing of award-winning activities: you can get up to iphone13 after using WordPress to build your own blog
Framework customization series (VI) -- shield fallbackhome mobile phone from pop-up during startup and directly enter the launcher
Camera continuous shooting automatic test shell script
EasyExcel复杂表头导出(一对多)
软件架构与设计(九)-----基于组件的架构
Endnote 与word关联
Software architecture and design (VI) -- hierarchy
Vs usage skills