当前位置:网站首页>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 .
边栏推荐
- How can labels/legends be added for all chart types in chart. js (chartjs.org)?
- How does MySQL execute an SQL statement?
- Pytoch implements tf Functions of the gather() function
- 语义分割实验:Unet网络/MSRC2数据集
- MySQL regular expression
- ZABBIX monitors mongodb templates and configuration operations
- JDBC exercise - query data encapsulated into object return & simple login demo
- 什么是数字化存在?数字化转型要先从数字化存在开始
- How to design an interface?
- 强化学习-学习笔记3 | 策略学习
猜你喜欢
Hexadecimal conversion summary
Pytoch loads the initialization V3 pre training model and reports an error
Simple production of wechat applet cloud development authorization login
One article tells the latest and complete learning materials of flutter
什么是数字化存在?数字化转型要先从数字化存在开始
Migrate data from Mysql to neo4j database
JDBC exercise - query data encapsulated into object return & simple login demo
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
Pytorch two-layer loop to realize the segmentation of large pictures
Principle of universal gbase high availability synchronization tool in Nanjing University
随机推荐
ZABBIX customized monitoring disk IO performance
Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
MySQL view
[HDU 2096] 小明A+B
Knowledge representation (KR)
Simple production of wechat applet cloud development authorization login
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
End to end neural network
Learn the memory management of JVM 03 - Method area and meta space of JVM
Handwriting blocking queue: condition + lock
ZABBIX ODBC database monitoring
Get data from the database when using JMeter for database assertion
PXE startup configuration and principle
How does MySQL execute an SQL statement?
Select drop-down box realizes three-level linkage of provinces and cities in China
GPS数据格式转换[通俗易懂]
Pytoch counts the number of the same elements in the tensor
ZABBIX agent2 monitors mongodb templates and configuration operations
MySQL multi table operation
Learning JVM garbage collection 06 - memory set and card table (hotspot)