1. 程式人生 > >MySQL5.7.23,主從複製的使用

MySQL5.7.23,主從複製的使用

先確保兩件事情,1. 主從資料庫版本最好一致,以免出現莫名其妙的問題。 2. 主從資料庫之間網路聯通沒有問題。因為我是單配置檔案部署的MySQL多例項,所以不存在這兩個問題。

一、修改配置檔案。

主資料庫配置,新增以下資訊

#主從複製配置
server-id=1  #設定server-id
log_bin=master-bin #開啟二進位制日誌
expire_logs_days=10
max_binlog_size=100M
binlog-do_db=szmbke
binlog_ignore_db=mysql

資料庫名'szmbke'自己替換,或者也可以不指定。

binlog_ignore_db忽略複製的資料庫。不過也曾看過有人說這個引數有坑,使用replicate-ignore-db = mysql替代,可以自己實驗一下。

從資料庫配置,新增以下資訊

server-id=2

這個值不與主資料庫重複,確保唯一就行。

二、主從複製配置。

進入主資料庫,建立用於同步的使用者賬號

mysql> CREATE USER 'replication'@'%' IDENTIFIED BY 'YourPassword';#建立使用者
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';#分配許可權
mysql>flush privileges;   #重新整理許可權

檢視master狀態,記錄二進位制檔名(master-bin.000009)和位置(154)

mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000009 |      154 | szmbke       | mysql            |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

進入從資料庫,執行同步sql語句

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='47.92.141.248',
    ->     MASTER_USER='replication',
    ->     MASTER_PASSWORD='YourPassword',
    ->     MASTER_LOG_FILE='mysql-bin.000009',
    ->     MASTER_LOG_POS=154;

啟動slave同步程序

mysql>start slave;

檢視slave狀態

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 47.92.141.248
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000009
          Read_Master_Log_Pos: 154
               Relay_Log_File: jim-relay-bin.000007
                Relay_Log_Pos: 369
        Relay_Master_Log_File: master-bin.000009
             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: 154
              Relay_Log_Space: 741
              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: 558ef375-bbe0-11e8-bc31-00163e009b3c
             Master_Info_File: /var/lib/mysql-3307/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

觀察 Slave_IO_Running: Yes ,Slave_SQL_Running: Yes這兩項,都是YES說明成功了,如果失敗了,注意看錯誤提示,之前遇到過Slave_IO_Running出現NO的情況,建議從兩方面排除

1. 主資料庫的同步使用者是否正常建立並且打開了許可權

2. 啟動從資料庫時是否指定了埠和socket檔案,不然會出現"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." 類似的錯誤提示。

成功後可以嘗試在主庫做操作。觀察從庫的更新情況。

這樣就OK了。