当前位置:网站首页>Docker-mysql8-master-slave
Docker-mysql8-master-slave
2022-06-24 23:12:00 【Wang Daochang's way of programming】
One 、 Prepare official image
official docker Mirror image https://hub.docker.com/_/mysql
docker pull mysql:latest
# establish mysql The Internet
docker network create mysql-net --subnet 172.1.2.0/24
docker volume rm $(docker volume list |awk '{if(NR>0) print $2}')
Master slave planning
| role | ip | port | Copy accounts | password |
|---|---|---|---|---|
| master | 172.1.2.2 | 3306 | Slave | slave |
| slave | 172.1.2.3 | 3307 | - | - |
Two 、 Prepare the configuration file
cd ~
mkdir mysql mysql/master mysql/slave
cd mysql
# master The configuration file
echo "[mysqld] server-id=1 log-bin=mysql-bin" >> master/mysql.cnf
# slave The configuration file
echo "[mysqld] server-id=2 relay_log = /usr/local/mysql/data/mysql-relay-bin relay_log-index = /usr/local/mysql/data/mysql-relay-bin.index log_slave_updates = 1 read_only=1" >> slave/mysql.cnf
3、 ... and 、 Master slave configuration
step :
- Modify the master-slave mysql.cnf The configuration file
- Master-slave mysql Service to restart
- The master and slave firewalls are closed
- Establish authorized on the host slave Login account
- Configure from the machine master Information about
3.1 maste To configure
docker run --network mysql-net --name master -p 3306:3306 --ip 172.1.2.3 -e MYSQL_ROOT_PASSWORD=mysecret -d mysql:latest
# establish slave user , And configure permissions
CREATE USER 'slave'@'172.1.2.%' IDENTIFIED WITH mysql_native_password BY 'slave';
select user,host,plugin from mysql.user;
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.1.2.%';
# mysql master To configure mysql.cnf
docker cp master/mysql.cnf master:/etc/mysql/conf.d/mysql.cnf
# After modifying the configuration , Make sure you reboot
docker restart master
# verification binlog Open file or not
show global variables like '%log_bin%';
remarks :
[mysqld]
server-id= 1 -- Unique to the primary server id
log-bin=mysql-bin -- Start binary log file
binlog-ignore-db=mysql -- Set ignored database ( Multiple can be set )
binlog-do-db=dbname -- Name of the primary database to be replicated
binlog_format=STATEMENT -- Set up binlog The log format of
establish slave The login account permissions of the node
-- Host computer MySQL To carry out the authorization order in the library
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123123';
-- Inquire about master The state of
show master status;
-- Record file and position value , And stop master Our external services , Prevent the master server status value from changing
3.2 slave To configure
docker run --network mysql-net --name slave -p 3307:3306 --ip 172.1.2.4 -e MYSQL_ROOT_PASSWORD=mysecret -d mysql:latest
# mysql slave To configure mysql.cnf
docker cp slave/mysql.cnf slave:/etc/mysql/conf.d/mysql.cnf
# After modifying the configuration , Make sure you reboot
docker restart slave
# Specify master ip, port , user , And configure master and slave
change master to master_host='172.1.2.3',master_port=3306,master_user='slave',master_password='slave';
# Or partially copied
change master to master_host='192.168.81.132',master_port=3306,master_user='slave',master_password='slave',master_log_file='mysql- bin.000002',master_log_pos=155;
start slave;
# testing
show slave status;
remarks :
[mysqld]
server-id=2 -- Unique from the server id
relay-log=mysql-replay -- Enable relay logging
The slave configures the master node information
change master to master_host=" host ip Address “;
master_user="slave"
master_password="123123"
master_log_file="mysql-bin. Specific figures "
master_log_pos=" Specific value “;
-- Start the replication function from the server
start slave;
-- View the status of the slave server
show slave status\G;
-- Only slave_io_running and slave_sql_running A fellow yes The configuration is successful
Additional information :
-- Stop the copy function from the server
stop slave;
-- Reconfigure master-slave
reset master;
Four 、 Dual master configuration
4.1 master To configure
4.1.1 master1 To configure
#vim /etc/my.cnf
server-id=1 # The primary server is unique ID
log-bin=mysql-bin # Enable binary logging
binlog-ignore-db=mysql # Set up the database not to be copied ( Multiple... Can be set )
binlog-ignore-db=information_schema
binlog-do-db= The name of the database to be copied # Set up the database to be copied
binlog_format=STATEMENT # Set up logbin Format
# As a slave database , Write operations also need to update binary log files
log-slave-updates # The amount of each increment of the table self growth field , It refers to the starting value of the auto increment field , The default value is 1, The value range is 1..65535
auto-increment-increment=2
# The table starts from which number the growth field starts , Refers to how many fields are incremented at a time , The value range is 1..65535
auto-increment-offset=1
4.1.2 master2 To configure
#vim /etc/my.cnf
server-id=3 # The primary server is unique ID
log-bin=mysql-bin # Enable binary logging
# Set up the database not to be copied ( Multiple can be set )
binlog-ignore-db=mysql
binlog-ignore-db=information_schema # Set up the database to be copied
binlog-do-db= The name of the master database to be copied # Set up logbin Format
binlog_format=STATEMENT
# When acting as a slave database , Update binary log files when there are write operations
log-slave-updates # The amount of each increment of the table auto increment field , It refers to the starting value of the auto increment field , Default 1, The value range is 1..65535
auto-increment-increment=2
# The table starts from which number the growth field starts , Refers to how many fields are incremented at a time , The value range is 1..65535
auto-increment-offset=2
4.2 Dual slave configuration
4.2.1 slave1 To configure
#vim /etc/my.cnf
# From the server only ID
server-id=2
# Enable relay logging
relay-log=mysql-relay
Dual host 、 Double slave restart mysql service
The host and slave turn off the firewall
Set up accounts on both hosts and authorize slave
4.3 Master-slave authorization
4.3.1 Dual master authorization
-- Host computer MySQL To carry out the authorization order in the library
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123123'; -- Inquire about Master1 The state of
show master status;
-- Inquire about Master2 The state of show master status;
-- Record separately File and Position Value
-- Do not operate the master server after this step MYSQL, Prevent the state value of the primary server from changing
4.3.2 Dual slave authorization
Slava1 Copy Master1,Slava2 Copy Master2
-- slave1、slave2
CHANGE MASTER TO MASTER_HOST=' The host IP', MASTER_USER='slave',
MASTER_PASSWORD='123123', MASTER_LOG_FILE='mysql-bin. Specific figures ',MASTER_LOG_POS= Specific value ;
-- Start the replication function of two slaves
start slave;
-- View the status of the slave server
show slave status\G;
#Slava1 Copy Master1
# The following two parameters are Yes, The master-slave configuration is successful !
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
4.4 Master master copy
-- master1、master2
CHANGE MASTER TO MASTER_HOST=' host 2 Of IP',
MASTER_USER='slave',
MASTER_PASSWORD='123123',
MASTER_LOG_FILE='mysql-bin. Specific figures ',MASTER_LOG_POS= Specific value ;
-- master2
CHANGE MASTER TO MASTER_HOST=' host 1 Of IP',
MASTER_USER='slave',
MASTER_PASSWORD='123123',
MASTER_LOG_FILE='mysql-bin. Specific figures ',
MASTER_LOG_POS= Specific value ;
fen Start the replication function of two primary servers respectively
start slave;
-- View the status of the standby database
show slave status \G;
# The following two parameters are Yes, The master-slave configuration is successful !
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
边栏推荐
- Learn more about redis' eight data types and application scenario analysis
- 【武汉大学】考研初试复试资料分享
- Parental delegation mechanism
- Market trend report, technical innovation and market forecast of solar roof system in China
- Building Survey [1]
- China smallpox vaccine market trend report, technical innovation and market forecast
- laravel 消息队列
- Common sense of resolution
- 【nvm】
- Sword finger offer 42 Maximum sum of successive subarrays
猜你喜欢

Spark 离线开发框架设计与实现

Talk about GC mechanism often asked in interview

并发之共享模型管程

Dig deep into MySQL - resolve the clustered index / secondary index / federated index of InnoDB storage engine

推送Markdown格式信息到钉钉机器人

加分利器 不负所托 | 知道创宇获攻防演练防守方感谢信!

Tech talk activity review kubernetes skills of cloud native Devops

Servlet

花房集团二次IPO:成于花椒,困于花椒

【基础知识】~ 半加器 & 全加器
随机推荐
[Wuhan University] information sharing of the first and second postgraduate entrance examinations
【文本数据挖掘】中文命名实体识别:HMM模型+BiLSTM_CRF模型(Pytorch)【调研与实验分析】
是否需要提高代码阅读能力?这有技巧
Research Report on research and investment prospects of China's container coating industry (2022 Edition)
China Sky Lantern market trend report, technical dynamic innovation and market forecast
【Mongodb】READ_ ME_ TO_ RECOVER_ YOUR_ Data, the database is deleted maliciously
How to integrate Huawei cloud function services in fluent
What kind of processor architecture is ARM architecture?
EPICS记录参考4--所有输入记录都有的字段和所有输出记录都有的字段
China smallpox vaccine market trend report, technical innovation and market forecast
Tech talk activity review kubernetes skills of cloud native Devops
. Net 7 Preview 1 has been officially released
[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
Development specification - parameter verification exception, exception return prompt section
The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!
Financial management [6]
Financial management [5]
Solution to the login error of tangdou people
对抗训练理论分析:自适应步长快速对抗训练
China solar thermal market trend report, technical dynamic innovation and market forecast