MySQL双主是一种高可用性和容错性的数据库架构,有两个主数据库(Master)。这种架构允许在其中一个主数据库出现故障时,系统仍然能够正常运行,并且在故障恢复后能够继续正常工作。
工作原理:
优点:
缺点:
应用场景:
常见实现方式:
配置完成后重启
vim /etc/my.cnf
log_bin
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
binlog_format=row
log_bin:
此配置项启用二进制日志,它是 MySQL 复制所必需的。
server-id:
此配置项用于为 MySQL 服务器分配唯一的标识符。在复制设置中,每个服务器都应该有一个唯一的 server-id。在您的配置中,服务器的ID被设置为1。确保每个服务器都有一个唯一的ID。
gtid_mode:
此配置项启用 GTID 模式。GTID 是用于在不同 MySQL 实例之间唯一标识事务的机制。启用 GTID 有助于简化复制配置和处理。
enforce_gtid_consistency:
此配置项强制执行 GTID 一致性。这确保在执行复制时事务的一致性。
binlog_format=row:
此配置项指定二进制日志的格式。在您的配置中,设置为row,表示以行为基础记录二进制日志。这是推荐的设置,因为它提供更好的灵活性和一致性。
grant replication slave on *.* to 'rep'@'192.168.180.%' identified by 'Sunshao-123';
rep是用户名称
@后边跟上服务器网段
配置完成后重启
log_bin
server-id=2
#GTID:
gtid_mode=on #开启gtid模式
enforce_gtid_consistency=on
binlog_format=row
1.检测创建账户是否可用 mysql -h 目标服务器 -u创建用户 -p'密码' master2 访问 master1
4.***设置主服务器** 指向master1
mysql> change master to
-> master_host='另外一个主服务器的IP',
-> master_user='rep',
-> master_password='Sunshao-123',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave; 开启复制 Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.180.180
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master1-bin.000002
Read_Master_Log_Pos: 1720
Relay_Log_File: master2-relay-bin.000004
Relay_Log_Pos: 966
Relay_Master_Log_File: master1-bin.000002
Slave_IO_Running: Yes
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: 1720
Relay_Log_Space: 2452
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 0a562cb8-bf46-11ee-b233-000c2950269e
Master_Info_File: /var/lib/mysql/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:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9
Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-9,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
都为yes表示成功
接下来返回master1继续配置
.***设置主服务器** 指向master2
mysql> change master to
-> master_host='另外一个主服务器的IP',
-> master_user='rep',
-> master_password='Sunshao-123',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.180.181
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master2-bin.000002
Read_Master_Log_Pos: 194
Relay_Log_File: master1-relay-bin.000003
Relay_Log_Pos: 411
Relay_Master_Log_File: master2-bin.000002
Slave_IO_Running: Yes
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: 194
Relay_Log_Space: 1233
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: 235616ef-b8fc-11ee-86c1-000c2952be42
Master_Info_File: /var/lib/mysql/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:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 235616ef-b8fc-11ee-86c1-000c2952be42:1-2
Executed_Gtid_Set: 0a562cb8-bf46-11ee-b233-000c2950269e:1-10,
235616ef-b8fc-11ee-86c1-000c2952be42:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
master2同步master1
master1上
mysql> insert into t1 values(666666);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1;
+--------+
| id |
+--------+
| 11 |
| 22 |
| 22 |
| 22 |
| 666666 |
+--------+
5 rows in set (0.00 sec)
mysql>
master2上
mysql> select * from test.t1;
+--------+
| id |
+--------+
| 11 |
| 22 |
| 22 |
| 22 |
| 666666 |
+--------+
5 rows in set (0.01 sec)
master1同步master2
master2上
mysql> insert into test.t1 values(77777);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1;
+--------+
| id |
+--------+
| 11 |
| 22 |
| 22 |
| 22 |
| 666666 |
| 77777 |
+--------+
6 rows in set (0.00 sec)
mysql>
master1上
mysql> select * from test.t1;
+--------+
| id |
+--------+
| 11 |
| 22 |
| 22 |
| 22 |
| 666666 |
| 77777 |
+--------+
6 rows in set (0.00 sec)