環境:
roles | IP |
---|---|
主 | 10.1.1.20 |
從 | 10.1.1.21 |
1、修改MASTER的/etc/my.cnf,在[mysqld]段的最後一行加入:
server-id=1 #主數據庫的id
log-bin=master-bin #日誌路徑,作用是從數據庫是根據這個日誌來複制主數據庫的數據。
重啓數據庫,新建用戶rep,設置密碼123456,並授權允許在Slave服務器上登陸:
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> use mysql;
Query OK, 1 row affected, 4 warnings (0.00 sec)
mysql > grant all on *.* to 'rep'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql > flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql > show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000014 | 245 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
2、修改SLAVE的/etc/my.cnf,在[mysqld]段的最後一行加入:
server-id=2#這個id必須不能和主數據庫相同
read-only=on#設置該數據庫是隻讀狀態
relay-log=relay-bin #日誌
重啓MySQL,然後使用root登陸,建立主從同步相關信息:
mysql>change master to \
master_host='10.1.1.20',master_user='rep',master_password='123456',master_log_file='master-bin.000005',master_log_pos=245;
啓動SLAVE,並檢查SLAVE狀態:
mysql> start slave; #啓動SLAVE
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;# 檢查SLAVE狀態
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.20
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 406
Relay_Log_File: relay-bin.000004
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
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: 406
Relay_Log_Space: 615
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: a0a896e0-803d-11e9-a717-000c29c380a1
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 the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
上面的“Slave_IO_Running: Yes”和“Slave_SQL_Running: Yes”說明主從同步已經通了,如果不是yes,則需要排查原因了。
3、測試驗證:
在主庫上刪除自帶的test庫,則可以看到從庫上的test庫也消失了。在主庫新建111庫,則從庫也出現了111庫。
4、報錯案例:
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: 993
Relay_Log_Space: 120
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.
上述出現“Slave_IO_Running: No”和“Last_IO_Error”兩處錯誤,從第二個錯誤可以看出MySQL的UUID錯誤,提示:主從庫的UUID是一樣的。因爲我們的虛擬機是克隆過來的,克隆之前MySQL已經安裝好,所以克隆過的MySQL的UUID和之前的UUID一致,產生衝突。所以,解決此問題,需要修改從庫的UUID,UUID記錄在/usr/local/mysql/data/auto.cnf中,將UUID的值任意修改一位就可以:
vim /usr/local/mysql/data/auto.cnf
[auto]
server-uuid=d85da196-7d0c-11e9-924a-000c29c380a2
修改完成後,重啓MySQL,登陸再次查看slave 狀態,這次已經恢復正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes