配置主庫
主庫必須啓用二進制日誌並有唯一的server ID,設置後需要重啓mysql服務生效。主從數據傳輸同步是根據binlog實現的,如果未啓用binlog,複製是不可能的。複製組內每個服務器都必須有一個唯一的server ID,此ID用來標識各個服務器,且爲1-2的32次方之間的整數,默認爲0。
[mysqld]
log-bin=master_info
server-id=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
配置從庫
從庫也必須有唯一的server ID,不能省略,否則拒絕連接。
[mysqld]
server-id=2
創建複製用戶
帳號需可被從庫訪問,並具有REPLICATION SLAVE權限,也可不必單獨爲複製創建帳號。
[master]
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.90.129' IDENTIFIED BY 'mypass';
Query OK, 0 rows affected (0.02 sec)
獲取主庫二進制日誌執行位置並同步數據
配置複製需要指定從庫開始執行主庫的二進制日誌的起始執行位置,因此當從庫同步主庫現有數據時,需要停止在主庫上執行更新語句,然後獲得主庫的當前二進制日誌座標並轉儲數據。如果主庫繼續執行更新操作,可能導致主從數據不一致。
獲取主庫二進制日誌座標的方式:
- 主庫執行FLUSH TABLES WITH READ LOCK 語句:
mysql> FLUSH TABLES WITH READ LOCK;
該語句刷新所有表及塊寫入語句,對於InnoDB表還會阻塞Commit。非正常退出該session,FLUSH TABLES仍會生效,如使用exit退出鎖會被釋放。
- 執行SHOW MASTER STATUS語句確定當前的二進制日誌文件名和位置
mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| master_info.000007 | 120 | | | |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
- 同步主從數據
如果正在創建一個全新的主從複製可忽略本步驟,如果創建的主庫已存在數據,需要創建一個主庫的數據快照並在從庫中恢復。或許數據快照的方式很多,可使用備份工具如:mysqldump、xtrabackup等,也可直接拷貝主庫數據文件。
- 釋放讀鎖
mysql> UNLOCK TABLES;
從庫執行CHANGE MASTER TO語句,啓動複製
mysql> change master to master_user='repluser',
-> master_password='mypass',
-> master_host='192.168.90.129',
-> master_log_file='master_info.000007',
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.11 sec)
mysql> start slave; ## 啓動複製線程
Query OK, 0 rows affected (0.06 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.90.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master_info.000004
Read_Master_Log_Pos: 120
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master_info.000004
Slave_IO_Running: No
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema
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: 120
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.
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: /home/mysql/slave_a/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: 140716 23:49:33
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)
提示有相同的UUID,查找原因發現是複製文件時將auto.cnf文件也拷貝過來了,
該文件裏面記錄了數據庫的uuid,每個庫的uuid應該是不一樣的。
[master]
mysql> show variables like '%server%id%';
+----------------+--------------------------------------+
| Variable_name | Value |
+----------------+--------------------------------------+
| server_id | 1 |
| server_id_bits | 32 |
| server_uuid | 71b00d22-fe27-11e3-bc27-000c29348dbe |
+----------------+--------------------------------------+
3 rows in set (0.03 sec)
[slave]
mysql> show variables like '%server%id%';
+----------------+--------------------------------------+
| Variable_name | Value |
+----------------+--------------------------------------+
| server_id | 3 |
| server_id_bits | 32 |
| server_uuid | 71b00d22-fe27-11e3-bc27-000c29348dbe |
+----------------+--------------------------------------+
3 rows in set (0.03 sec)
解決辦法,按照這個16進制格式,隨便改下,重啓mysql即可
mysql> show variables like '%server%id%';
+----------------+--------------------------------------+
| Variable_name | Value |
+----------------+--------------------------------------+
| server_id | 3 |
| server_id_bits | 32 |
| server_uuid | 71b00d22-fe27-11e3-bc27-000c29348888 |
+----------------+--------------------------------------+
3 rows in set (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.90.129
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master_info.000004
Read_Master_Log_Pos: 120
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 285
Relay_Master_Log_File: master_info.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema
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: 120
Relay_Log_Space: 452
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: 71b00d22-fe27-11e3-bc27-000c29348dbe
Master_Info_File: /home/mysql/slave_a/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)
驗證同步效果
[master]
mysql> create table test.sysc (
> id int primary key,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> insert into test.sysc values(1001,'1001');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test.sysc values(1002,'1002');
Query OK, 1 row affected (0.03 sec)
[slave]
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| sysc |
+----------------+
1 row in set (0.00 sec)
mysql> select * from sysc;
+------+------+
| id | name |
+------+------+
| 1001 | 1001 |
| 1002 | 1002 |
+------+------+
2 rows in set (0.00 sec)
整理自網絡
Svoid
2014-07-16