The mysql database is Shared among master and slave configuration methods

  • 2020-05-13 03:37:39
  • OfStack

mysql master and slave configuration notes:
There are four machines: A(10.1.10.28),B(10.1.10.29),C(10.1.10.30),D(10.1.10.31).
The result after configuration: A-C is the master and slave of each other, B is slave of A, D is slave of C.

0) preparation
Once mysql is installed on all four machines, create the account for synchronization.
Add account:
 
INSERT INTO user (Host,User, Password,Select_priv,Insert_priv,Update_priv,Delete_priv, Create_priv,Drop_priv) VALUES('%','test',password('test'),'Y','Y','Y','Y','Y','Y'); 

Refresh the database:
 
FLUSH PRIVILEGES; 

1) configure A-C as master and slave
Modify the A configuration file to:
 
server-id = 1 
replicate-do-db=test 
replicate-do-db=test_admin 
log-bin=mysql-bin 
log-slave-updates 
replicate-wild-do-table=test.% 
replicate-wild-do-table=test_admin.% 
binlog-ignore-db=mysql 
slave-skip-errors=all 

Modify the C configuration file to:
 
server-id = 3 
binlog-do-db=test 
binlog-do-db=test_admin 
log-bin=mysql-bin 
log-slave-updates 
replicate-wild-do-table=test.% 
replicate-wild-do-table=test_admin.% 
binlog-ignore-db=mysql 
slave-skip-errors=all 

Restarting mysql is when the configuration takes effect
Set A as main:
Stop synchronization:
 
slave stop; 

Clean the server master log:
 
reset master; 

Authorized synchronous account:
 
GRANT REPLICATION SLAVE ON *.* TO 'test'@'%' IDENTIFIED BY 'test' ;  

Refresh authorization:
 
flush privileges; 

Lock database:
 
flush tables with read lock; 

Set C from:
Stop synchronization:
 
slave stop; 

Configuration synchronization information:
 
FLUSH PRIVILEGES; 
0
Set C as main:
Stop synchronization:
 
slave stop; 

Clean the server master log:
 
reset master; 

Authorized synchronous account:
 
GRANT REPLICATION SLAVE ON *.* TO 'test'@'%' IDENTIFIED BY 'test' ;  

Refresh authorization:
 
flush privileges; 

Lock database:
 
flush tables with read lock; 

Set A to:
Stop synchronization:
 
slave stop; 

Configuration synchronization information:
 
FLUSH PRIVILEGES; 
7
2) set B as the slave of A
 
FLUSH PRIVILEGES; 
8

Restart the mysql service
Stop synchronization:
 
slave stop; 

Configuration synchronization information:
 
FLUSH PRIVILEGES; 
0
Start synchronization:
 
slave start; 

3) set D as the slave of C
 
server-id = 4 
replicate-do-db=test 
replicate-do-db=test_admin 
log-bin=mysql-bin 
log-slave-updates 
replicate-wild-do-table=test.% 
replicate-wild-do-table=test_admin.% 
binlog-ignore-db=mysql 
slave-skip-errors=all 

Restart the mysql service
Stop synchronization:
 
slave stop; 

Configuration synchronization information:
 
FLUSH PRIVILEGES; 
7
Start synchronization:
 
slave start; 

Once you're done, you can create add data to test whether it works or not.
The following are common error handling:

1)
change master causes:
Last_IO_Error: error connecting to master 'repl1@IP:3306' - retry-time: 60 retries
2)
Stop the slave process without unlocking:
mysql > stop slave;
ERROR 1192 (HY000): Can't execute the given command because you have active locked tables or an active transaction
3)
change master syntax error, missing comma
mysql > change master to
- > master_host='IP'
- > master_user='USER',
- > master_password='PASSWD',
- > master_log_file='mysql-bin.000002',
- > master_log_pos=106;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_user='USER',
master_password='PASSWD',
master_log_file='mysql-bin.000002' at line 3

4)
change master without stopping the slave process
mysql > change master to master_host= 'IP', master_user='USER', master_password=' USER',
_password ='USER', master_log_file=' mysql-bin.000001 ',master_log_pos=106;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

5)
A B server-id same:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids;
these ids must be different for replication to work (or the --replicate-same-server-id option must be used on
slave but this does not always make sense; please check the manual before using it).
View the server - id
mysql > show variables like 'server_id';
Manually modify server-id
mysql > set global server_id = 2; The value here is the same as the one set in my.cnf
mysql > slave start;
6) after change master, check the state of slave and find that slave_IO_running is NO

Related articles: