The MySQL master-master replication structure is different from the master-slave replication structure. In the master-master replication structure, changes in the data inventory on either server will be synchronized to the other server, so that the two servers are master-slave to each other and can provide services to the outside world.
Configuration steps:
1. Modify the configuration file
(1) Server A (192.168.115.119) is configured as follows ( this configuration needs to be written in the [mysqld] area )
server-id=10
log-bin=master-bin
log-slave-updates=true
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 1
(2) Server B (192.168.115.120) is configured as follows ( this configuration needs to be written in the [mysqld] area )
server-id=20
log-bin=master-bin
log-slave-updates=true
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog_format=STATEMENT
auto-increment-increment = 2
auto-increment-offset = 2
Note: Only server-id and auto-increment-offset are different between the two.
auto-increment-offset is used to set the starting point of automatic growth in the database. An automatic growth value of 2 is set for both servers, so their starting points must be different to avoid data synchronization between the two servers. A primary key conflict occurs when
replicate-do-db can specify the database to be synchronized
Also: the value of auto-increment-increment should be set to the total number of servers in the entire structure. In this case, two servers are used, so the value is set to 2.
(3) Restart both servers
systemctl restart mysqld
2. Mutually authorize users (authorize a user on server A to allow access to server B, and vice versa)
(1) On server A (192.168.115.119)
grant replication slave on *.* to 'slave'@'192.168.115.%' identified by '123.com';
flush privileges;
(2) On server B (192.168.115.120)
grant replication slave on *.* to 'slave'@'192.168.115.%' identified by '123.com';
flush privileges;
3. Report bin-log information to each other
(1) On server A (192.168.115.119)
show master status;
(2) On server B (192.168.115.120)
show master status;
(3) Specify master on server A (192.168.115.119)
change master to master_host='192.168.115.120',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=612;
(4) Specify master on server B (192.168.115.120)
change master to master_host='192.168.115.119',master_user='slave',master_password='123.com',master_log_file='master-bin.000001',master_log_pos=612;
4. Execute the following commands on both servers
start slave;
5. Check status
show slave status\G;
(1) Displayed on server A (192.168.115.119)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.115.120
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 612
Relay_Log_File: bogon-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 612
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
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).
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 230727 10:23:39
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
(2) Displayed on server B (192.168.115.120)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.115.120
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 612
Relay_Log_File: bogon-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 612
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
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).
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 230727 10:23:35
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
(3) Error analysis
ERROR:
No query specified
At this time, I saw that the two servers displayed the same error, error analysis
(4) Solving problems
1. Run the following command on the main server to obtain the current server ID:
SHOW VARIABLES LIKE 'server_id';
2. Run the following command on the slave server to obtain the current server ID:
SHOW VARIABLES LIKE 'server_id';
At this time, you can see that the IDs of the two servers are the same. Therefore, change the MySQL configuration files /etc/my.cnf of the two servers, find the `server-id` parameter, and set it to a different one from the main server. Unique value. (Comment out the last `server-id` in the two configuration files respectively)
3. Restart both servers to ensure that the settings take effect.
systemctl restart mysqld
4. The two servers enter mysql again and use the following commands to start the replication and replication threads:
stop slave;
start slave;
(5) After executing this command
Displayed on server A (192.168.115.119)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.115.120
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 612
Relay_Log_File: bogon-relay-bin.000002
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 612
Relay_Log_Space: 154
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 20
Master_UUID:
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 230727 10:45:35
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
Error number 1593 is a problem caused by the master and slave servers having the same MySQL server UUID. The error message "Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work." states that in order for replication to work properly, the master and slave MySQL server UUIDs must be different.
The MySQL server UUID is a value that uniquely identifies the server and is used for authentication and identification during replication. Since the UUIDs of the two servers are the same, the IO thread of the slave server stops working and replication cannot continue.
(6) Problem description:
When the cluster is set up, cloning the image of the main service causes the service UUIDs of all nodes to be consistent. In this case, a UUID conflict error will be prompted when adding nodes to the cluster.
(7) Solution
1. Use the uuid function to generate a new uuid
mysql> select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| 18093dc8-2c8f-11ee-bf5e-000c2973c1d9 |
+--------------------------------------+
1 row in set (0.00 sec)
2. View the configuration file directory
mysql> show variables like 'datadir';
+---------------+------------------------+
| Variable_name | Value |
+---------------+------------------------+
| datadir | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.01 sec)
3. Edit the configuration file directory
vim /usr/local/mysql/data/auto.cnf
4. Modify the newly generated uuid by uuid
server-uuid=18093dc8-2c8f-11ee-bf5e-000c2973c1d9
5. Restart the service
service mysqld restart
Then enter mysql again, execute show master status on the two servers respectively; find that the data shown in the figure below has changed, then specify the master again on the two servers, turn on the slave, and check the slave status
Until I saw two yes, namely:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Indicates that the configuration has been successful
6. Test whether it is synchronized
(1) Operate on server A (192.168.115.119)
create database jx;
show databases;
(2) Operate on server B (192.168.115.120)
show databases;
(3) Operate on server B (192.168.115.120)
create database jx1;
show databases;
(4) Operate on server A (192.168.115.119)
show databases;
During the test, I also encountered some problems, mainly the communication between the two machines.
Please note that you must keep the mysql ports of both servers open to each other, otherwise synchronization will not be successful.