MySQL主從資料庫配置攻略以及錯誤處理
MySQL主從複製配置
1. 要求
1.1 系統平台一致
1.2 資料庫版本一致
2. 修改my.cnf檔案,主伺服器和備伺服器要求server-id不能一樣
3. 啟動兩側的資料庫
4. 在主伺服器上建立帳號,並且授權slave,從伺服器可訪問
GRANT REPLICATION SLAVE ON *.* to 'mysql_sync'@'10.10.88.101' identified by '123456';
5. 查看主伺服器狀態
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| dbtest.000005 | 414 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.15 sec)
注意:執行完此步驟後不要再操作主伺服器MYSQL,防止主伺服器狀態值變化
6. 配置從伺服器Slave
mysql> change master to master_host='10.10.88.100',master_user='mysql_sync',master_password='123456',master_log_file='dbtest.000005',master_log_pos=414;
mysql> start slave; //啟動從伺服器複製功能
mysql> change master to master_host='10.10.88.100',master_user='mysql_sync',master_password='123456',master_log_file='dbtest.000007',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.12 sec)
mysql> start slave;
Query OK, 0 rows affected (0.24 sec)
7. 檢查從伺服器複製功能狀態(在從伺服器上面執行檢查):
mysql> show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.88.100 --主伺服器IP
Master_User: mysql_sync --主伺服器資料庫帳號名稱
Master_Port: 3306 --資料庫連接埠
Connect_Retry: 60
Master_Log_File: dbtest.000005
Read_Master_Log_Pos: 414
Relay_Log_File: DBTEST2-relay-bin.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: dbtest.000005
Slave_IO_Running: Yes --狀態,必須為Yes
Slave_SQL_Running: Yes --狀態,必須為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: 414
Relay_Log_Space: 455
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: 73b077c6-626d-11e5-8f5b-000c29e840c1
Master_Info_File: /app/mysqldata/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)
配置過程中,因為後來從主庫cp過來所有的資料檔案,作為從庫的初始環境,結果就報錯如下:
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.
經過分析,是因為從主庫cp了所有檔案,導致server_uuid一致,所以從庫起不來,解決辦法如下:
1. 進入到資料目錄
cd /app/mysqldata
2. 備份auto.cnf 讓資料庫自動產生一個新的uuid來用
cp auto.cnf auto.cnf.bak
rm -rf auto.cnf
3. 重新啟動主庫以及從庫
查看主庫狀況,重設從庫狀態,啟動slave即可。
本文永久更新連結地址: