当前位置:网站首页>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

roleipport Copy accounts password
master172.1.2.23306Slaveslave
slave172.1.2.33307--

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 :

  1. Modify the master-slave mysql.cnf The configuration file
  2. Master-slave mysql Service to restart
  3. The master and slave firewalls are closed
  4. Establish authorized on the host slave Login account
  5. 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
原网站

版权声明
本文为[Wang Daochang's way of programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241719271857.html