centos7上配置mysql8的雙主互寫

卡卡西村長發表於2019-07-11

注意:
1、主庫1:10.1.131.75,主庫2:10.1.131.76
2、server-id必須是純數字,並且主從兩個server-id在區域網內要唯一。

【主節點1】
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2019001
log_slave_updates=1

重啟服務
service mysqld restart

【主節點2】
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2019002
log_slave_updates=1

重啟服務
service mysqld restart

【主節點1】
mysql> CREATE USER 'repl'@'10.1.131.76' IDENTIFIED WITH mysql_native_password BY 'XXXXXX';

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.1.131.76';
mysql> flush privileges;

獲取主節點當前binary log檔名和位置(position)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 155 | | | |
+------------------+----------+--------------+------------------+-------------------+

【主節點2】
mysql> CHANGE MASTER TO MASTER_HOST='10.1.131.75',MASTER_USER='repl',MASTER_PASSWORD='XXXXXX',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=155;


【主節點2】
mysql> CREATE USER 'repl'@'10.1.131.75' IDENTIFIED WITH mysql_native_password BY 'XXXXXX';

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.1.131.75';
mysql> flush privileges;

獲取主節點當前binary log檔名和位置(position)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 178 | | | |
+------------------+----------+--------------+------------------+-------------------+

【主節點1】
mysql> CHANGE MASTER TO MASTER_HOST='10.1.131.76',MASTER_USER='repl',MASTER_PASSWORD='XXXXXX',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=178;

【主節點2】
開啟主從同步
mysql> start slave;

再檢視主從同步狀態
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.131.75
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 155
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

【主節點1】
開啟主從同步
mysql> start slave;

再檢視主從同步狀態
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.131.76
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 178
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 322
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

注意看Slave_IO_Running和Slave_SQL_Running,必須都是YES,如果Slave_IO_Running是No,通常都是server-id配置有問題。

相關文章