Article Directory
MySQL database cluster (dual master and dual slave)
Dual master and dual slave (Experiment 1)
Double master
The single node setting of the master server will affect the global write event if the master server fails, so it should be set to dual master. In the previous experiment, master1 has been set as the master server of master2, here only master2 needs to be set as the master server of master1.
Set master2 as the master server of master1
- Log in to the mysql database of master2 to authorize and refresh
grant replication slave,replication client on *.* to 'rep'@'192.168.83.%' identified by 'Mysql@123';
flush privileges;# 刷新
- Log in to the master1 database and operate:
change master to master_host='master2',master_user='rep',master_password='Mysql@123',master_auto_position=1;
start slave;# 开启奴隶模式
show slave status\G;
At this time, master1 is also someone else's master server, as well as someone else's slave server, master1 and master2 are each other's master and slave servers.
- test
Insert data on master1 and observe whether the data is synchronized
on master2. Insert data on master2 and observe whether the data is synchronized on master1.
If there is a problem, you can check it in /var/log/mysqld.log, and the two parties will synchronize with each other.
Double slave
There are two other mysql databases as servers
Synchronize existing databases
Use mysqldump to backup master1 data
mysqldump -p'Mysql@123' --all-databases --single-transaction --master-data=2 --flush-logs > mmss-mysql-all.sql
After the backup is complete, send the sql file to slave1 and slave2
scp -r master1:/mmss-mysql-all.sql slave1:/tmp/
scp -r master1:/mmss-mysql-all.sql slave2:/tmp/
Log in to slave1 and slave2 respectively and manually synchronize data
slave1 and slave2
mysql -uroot -p'Mysql@123' < /tmp/mmss-mysql-all.sql
Start the ID and GTID of the slave server
Enable the binary log function of slave1 and slave2 and
the database of server ID and GTID slave1:
vi /etc/my.cnf
Write in another few lines
server-id=3
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
Save and exit and restart slave1's database
systemctl restart mysqld
Slave2's database:
vi /etc/my.cnf
server-id=4
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
relay-log-info-repository=TABLE
Save and exit and restart the slave2 database
systemctl restart mysqld
Set up the main server
Log in to slave1's database
change master to master_host='master1',master_user='rep',master_password='Mysql@123',master_auto_position=1 for channel 'master1';
change master to master_host='master2',master_user='rep',master_password='Mysql@123',master_auto_position=1 for channel 'master2';
start slave;#开启奴隶模式
Check running status
show slave status\G;
Log in to slave2's database and operate
change master to master_host='master1',master_user='rep',master_password='Mysql@123',master_auto_position=1 for channel 'master1';
change master to master_host='master2',master_user='rep',master_password='Mysql@123',master_auto_position=1 for channel 'master2';
start slave;#开启奴隶模式
Check running status
show slave status\G;
The data of both parties have been synchronized.
If there is a problem with the cluster, you can /var/lib/mysql/
delete all the files in the database. After restarting the database, it will be a brand new database.
carry out testing
You can insert data into master1 and master2 respectively, and you can view it in slave1 and slave2 to verify the success of the experiment