当前位置:网站首页>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 .
边栏推荐
- ZABBIX monitors mongodb (template and deployment operations)
- MySQL log module of InnoDB engine
- Get data from the database when using JMeter for database assertion
- Pytoch loads the initialization V3 pre training model and reports an error
- Deep discussion on the decoding of sent protocol
- POJ-2499 Binary Tree
- C language structure is initialized as a function parameter
- What is digital existence? Digital transformation starts with digital existence
- ZABBIX 5.0 - LNMP environment compilation and installation
- 嵌入式软件架构设计-消息交互
猜你喜欢

Making and using the cutting tool of TTF font library
Why do you always fail in automated tests?

The evolution of mobile cross platform technology

Select drop-down box realizes three-level linkage of provinces and cities in China

Ecplise development environment configuration and simple web project construction

Master the new features of fluent 2.10

One article tells the latest and complete learning materials of flutter
Automated test lifecycle

嵌入式软件架构设计-消息交互

Solve the problem of cache and database double write data consistency
随机推荐
Redis's memory elimination mechanism, read this article is enough.
Image hyperspectral experiment: srcnn/fsrcnn
How does MySQL execute an SQL statement?
ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
byte2String、string2Byte
Redis clean cache
SENT协议译码的深入探讨
Get all stock data of big a
Kotlin变量
Learn JVM garbage collection 05 - root node enumeration, security points, and security zones (hotspot)
Interviewer: is acid fully guaranteed for redis transactions?
Principle of universal gbase high availability synchronization tool in Nanjing University
Why do you always fail in automated tests?
PIP command reports an error pip is configured with locations that requires tls/ssl problems
Redis highly available slice cluster
MySQL trigger
PXE startup configuration and principle
Constructing expression binary tree with prefix expression
II. Data type
The evolution of mobile cross platform technology