注意:
1、主庫:10.1.131.75,從庫:10.1.131.76
2、server-id必須是純數字,並且主從兩個server-id在區域網內要唯一。
【主節點】
vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=2019001
重啟服務
service mysqld restart
【從節點】
vi /etc/my.cnf
[mysqld]
server-id=2019002
重啟服務
service mysqld restart
【主節點】
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 | | | |
+------------------+----------+--------------+------------------+-------------------+
【從節點】
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;
開啟主從同步
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
注意看Slave_IO_Running和Slave_SQL_Running,必須都是YES,如果Slave_IO_Running是No,通常都是server-id配置有問題。