当前位置:网站首页>Using docker for MySQL 8.0 master-slave configuration
Using docker for MySQL 8.0 master-slave configuration
2022-07-05 12:36:00 【just4you】
MySQL Single machine configuration
Pull & Start the container
docker run -d \
--name mysql \
--privileged=true \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/conf:/etc/mysql/ \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/mysql-files:/var/lib/mysql-files/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
mysql
The latest version pulled by default is mysql8
docker Mirror image :https://hub.docker.com/_/mysql?tab=tags
To configure mysql
stay /usr/local/mysq/conf Add files to directory :my.cnf
[client]
default-character-set=utf8mb4
[mysqld]
# Specify the authentication plug-in
authentication_policy=mysql_native_password
- mysql8 The default encoding format of the server :utf8mb4, So the character encoding can not be set .
- Authentication plug-in : stay mysql5.7 The plug-in in is "mysql_navtive_password", And in the mysql8 The plug-in used is " caching_sha2_password", To avoid problems when the client connects , So you need to set it up .
Restart Mysql
docker restart mysql
Into the container
docker exec -it mysql bin/bash
Entering the container MySQL
mysql -uroot -p123456
MySQL Master slave configuration
MySQL Master
Docker Set up
Specify the host port as :13306
docker run -d \
--name mysql-master-13306 \
--privileged=true \
-v /usr/local/mysql-master/data:/var/lib/mysql \
-v /usr/local/mysql-master/conf:/etc/mysql/ \
-v /usr/local/mysql-master/log:/var/log/mysql \
-v /usr/local/mysql-master/mysql-files:/var/lib/mysql-files/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 13306:3306 \
mysql
The configuration file
stay /usr/local/mysql-master/conf Add profile to :my.cnf
[client]
default-character-set=utf8mb4
[mysqld]
## serverId, It's the only one in the LAN
server_id=01
## Turn on binary log function
log-bin=ms-mysql-bin
## Specify the name of the database that does not need to be synchronized , More than one... Can be specified
binlog-ignore-db=mysql
binlog-ignore-db=sys
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
## Specify a synchronized database , More than one... Can be specified
binlog-do-db=db01
binlog-do-db=db02
## Binary log uses memory size ( Business )
binlog_cache_size=1M
## Binary log format (mixed, statement,row)
# statement: Add a write operation statement to binlog, And then in slave The machine is running . shortcoming : There are now() when , It will cause data inconsistency
# row: Record the changes in each line , And then in slave Machine update . shortcoming : When updating in batch , Will be in slave The machine performs a lot update
# mixed: It's similar now() This kind of function will cause data inconsistency , Use row Pattern , Otherwise, use statement Pattern .
binlog_format=mixed
## Binary log expiration cleanup time , Default 2592000 second (30 God ),604800 second (7 God )
binlog_expire_logs_seconds=604800
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave Replication interrupt
replica_skip_errors=1062
# Specify the authentication plug-in
authentication_policy=mysql_native_password
restart Master
docker restart mysql-master-13306
Add backup users
Get into MySQL
CREATE USER 'slave'@'%' identified by 'backup';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
FLUSH PRIVILEGES;
stay mysql5.7 in , You can use a statement to create users and assign :GRANT REPLICATION SLAVE ON . TO ‘slave’@’%’ IDENTIFIED BY ‘backup’;
stay mysql8 It seems that there will be an error in , Separate the user creation and empowerment statements .
see & Record Master state
mysql> show master status \G;
*************************** 1. row ***************************
File: ms-mysql-bin.000003
Position: 1535
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.01 sec)
a key :
- File: ms-mysql-bin.000003
- Position: 1535
MySQL Slave
Docker Set up
Specify the host port as :13307
docker run -d \
--name mysql-slave-13307 \
--privileged=true \
-v /usr/local/mysql-slave/data:/var/lib/mysql \
-v /usr/local/mysql-slave/conf:/etc/mysql/ \
-v /usr/local/mysql-slave/log:/var/log/mysql \
-v /usr/local/mysql-slave/mysql-files:/var/lib/mysql-files/ \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 13307:3306 \
mysql
The configuration file
stay /usr/local/mysql-slave/conf Add profile to :my.cnf
[client]
default-character-set=utf8mb4
[mysqld]
## serverId, It's the only one in the LAN
server_id=11
## Specify the name of the database that does not need to be synchronized
binlog-ignore-db=mysql
## Turn on binary log function
log-bin=ms-mysql-slave11-bin
## Binary log uses memory size ( Business )
binlog_cache_size=1M
## Binary log format (mixed, statement,row)
binlog_format=mixed
## Binary log expiration cleanup time , Default 2592000 second (30 God ),604800 second (7 God )
binlog_expire_logs_seconds=604800
## Skip all errors encountered in master-slave replication or errors of the specified type , avoid slave Replication interrupt
replica_skip_errors=1062
## relay_log To configure slave The trunk log of
relay_log=ms-mysql-relay-bin
## Write the copy event to your own binary log
log_slave_updates=1
## Set to read only (super Except for authorized users )
read_only=1
# Specify the authentication plug-in
authentication_policy=mysql_native_password
restart slave Containers
docker restart mysql-slave-13307
see slave state
Get into slave
show slave status;
here , Usually there is no information .
take slave Connect to master
# stop it slave
stop slave;
# If you have done master-slave replication , You may also need to execute the following commands
## reset master;
# Connect to master
# master_log_file and master_log_pos The value of reference master Results of machine status query :
change master to master_host='192.168.10.121', master_user='slave', master_password='backup', master_port=13306,master_log_file='ms-mysql-bin.000003',master_log_pos=1535,master_connect_retry=30;
# start-up slave
start slave;
Parameter description :
- master_host: master machine IP, That's the host IP
- master_port: master port ,docker When the configuration ,master External port
- master_user: Account used for synchronization
- master_password: Password for synchronization
- master_log_file: Appoint slave To copy master Data log file
- master_log_pos: Specify where to start copying
- master_connect_retry: When the connection fails , Retry interval , Company : second
see slave state
show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.10.121
Master_User: slave
Master_Port: 13306
Connect_Retry: 30
Master_Log_File: ms-mysql-bin.000003
Read_Master_Log_Pos: 828
Relay_Log_File: ms-mysql-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: ms-mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
..........
a key :
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
When both values are “Yes” when , Description connection successful , Can be found in Master Data operation on the machine , And then, in slave The machine sees the effect of data synchronization .
other
If you inquire slave In the state of ,Slave_IO_Running The value of has always been Connecting, It may be the problem of account password or permission , Generally, you can see the prompt below , for example :
Last_IO_Error: error connecting to master '[email protected]:13306' - retry-time: 30 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
Adjust according to the prompt .
边栏推荐
- Kotlin流程控制、循环
- Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
- Implementing Yang Hui triangle with cyclic queue C language
- MySQL trigger
- Principle of universal gbase high availability synchronization tool in Nanjing University
- Solve the problem of cache and database double write data consistency
- ZABBIX customized monitoring disk IO performance
- Redis's memory elimination mechanism, read this article is enough.
- Learn memory management of JVM 01 - first memory
- Embedded software architecture design - message interaction
猜你喜欢

What is digital existence? Digital transformation starts with digital existence
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"

Keras implements verification code identification

Migrate data from Mysql to neo4j database
[email protected] (using password"/>Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password

Understand kotlin from the perspective of an architect

MySQL transaction

Resnet18 actual battle Baoke dream spirit

Understand redis persistence mechanism in one article

mysql拆分字符串做条件查询
随机推荐
Get all stock data of big a
Introduction to relational model theory
什么是数字化存在?数字化转型要先从数字化存在开始
Semantic segmentation experiment: UNET network /msrc2 dataset
Redis highly available sentinel cluster
MySQL data table operation DDL & data type
July Huaqing learning-1
Hexadecimal conversion summary
Correct opening method of redis distributed lock
7月华清学习-1
Image hyperspectral experiment: srcnn/fsrcnn
Distributed solution - distributed session consistency problem
Read and understand the rendering mechanism and principle of flutter's three trees
Four operations and derivative operations of MATLAB polynomials
Pytoch implements tf Functions of the gather() function
SENT协议译码的深入探讨
只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
byte2String、string2Byte
语义分割实验:Unet网络/MSRC2数据集
Instance + source code = see through 128 traps