MySQL複製之初體驗

svoid發表於2014-07-18

配置主庫

主庫必須啟用二進位制日誌並有唯一的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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29733787/viewspace-1222327/,如需轉載,請註明出處,否則將追究法律責任。

相關文章