Mysql 5.7 主從複製

Mysql 5.7 版本主從複製mysql

  • 準備事項
    • 確保兩臺服務器的Mysql版本一致 (本文是5.7);
    • 明確兩臺服務器IP
      • master : 192.168.33.10
      • slave : 192.168.33.11
      • 要保證防火牆3306端口開放

 

  1. 配置master
      1. 修改配置文件 /etc/mysql/mysql.conf.d/mysql.cnf (根據本身安裝的路徑 如: /etc/my.cnf)
        1 [mysqld]
        2 log-bin=mysql-bin
        3 server-id=1
        4 binlog-ignore-db=information_schema
        5 binlog-ignore-db=performance_schema
        6 binlog-ignore-db=sys
        7 binlog-ignore-db=mysql
        8 binlog-do-db=test

        其中 log-bin 是日誌類型(前綴) 
                server-id=1 是用於標識惟一的數據庫,在從庫必須設置爲不一樣的值 
                binlog-ignore-db 表示同步的時候忽略的數據庫
           binlog-do-db  指定須要同步的數據庫

        sql

  2. 重啓Mysql
    sudo service mysql restart

     

  3. 進入mysql,mysql -uroot -p,回車,輸入mysql密碼進入。賦予權限
    1. 賦予從庫權限帳號,容許用戶在主庫上讀取日誌,賦予192.168.33.11也就是Slave機器有File權限,

      只賦予Slave機器有File權限還不行,還要給它REPLICATION SLAVE的權限才能夠。數據庫

      1 grant FILE on *.* to 'root'@'192.168.33.11' identified by 'root';
      2 grant replication slave on *.* to 'root'@'192.168.33.11' identified by 'root';
      3 flush privileges;

      注: 這裏的 root 是同步的時候從庫使用的用戶。能夠本身建立新的 user 和 權限, 這裏再也不贅述ubuntu

        1. 重啓mysql,登陸mysql,查看主庫信息
          sudo service mysql restart
          show master status\G;

          若是出現如下信息 說明配置正確服務器

           show master status\G;
          *************************** 1. row ***************************
                       File: mysql-bin.000003
                   Position: 154
               Binlog_Do_DB: test
           Binlog_Ignore_DB: information_schema,performance_schema,mysql,sys
          Executed_Gtid_Set:
          1 row in set (0.00 sec)

          其中須要記錄的有: File 表明日誌文件名
          Position 表明主從複製開始的位置ide

  4. 配置從庫 slave測試

    1. 修改配置文件 /etc/mysql/mysql.conf.d/mysql.cnf (根據本身安裝的路徑 如: /etc/my.cnf)spa

       1 log-bin=mysql-bin
       2 server-id=2
       3 binlog-ignore-db=information_schema
       4 binlog-ignore-db=performance_schema
       5 binlog-ignore-db=mysql
       6 replicate-do-db=test
       7 replicate-ignore-db=mysql
       8 log-slave-updates
       9 slave-skip-errors=all
      10 slave-net-timeout=60

       

    2. 重啓mysql,登陸mysql, 配置 master 鏈接屬性
      stop slave;
      change master to master_host='192.168.33.10',master_user='root',master_password='root',master_log_file='mysql-bin.000003', master_log_pos=154;
      start slave;

      其中: master_log_file 是 master 記錄的 File 字段
      master_log_pos 是 master 記錄的 Position 字段vagrant

    3. 查看從庫信息debug

      show slave status\G

      若是出現如下信息 說明配置正確

      *************************** 1. row ***************************
                     Slave_IO_State: Waiting for master to send event
                        Master_Host: 192.168.33.10
                        Master_User: root
                        Master_Port: 3306
                      Connect_Retry: 60
                    Master_Log_File: mysql-bin.000003
                Read_Master_Log_Pos: 2602
                     Relay_Log_File: vagrant-ubuntu-trusty-64-relay-bin.000003
                      Relay_Log_Pos: 2768
              Relay_Master_Log_File: mysql-bin.000003
                   Slave_IO_Running: Yes
                  Slave_SQL_Running: Yes
                    Replicate_Do_DB: test
                Replicate_Ignore_DB: mysql
                 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: 2602
                    Relay_Log_Space: 2994
                    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: fe3645a3-d3d1-11e7-94d1-08002784ba04
                   Master_Info_File: /var/lib/mysql/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)

      其中 Slave_IO_Running Slave_SQL_Running 都爲 yes 表示正常
      Seconds_Behind_Master 表示 主從延遲
                         值爲 0 : 表示正常 沒有延遲
             正數: 表示 出現延遲
                               負數 : 理論不可能出現的值 (詢問 DBA 後 , 有些狀況會出現,可是這個屬性的值不能夠賦值爲負數)    

  5. debug
    1. 查詢錯誤日誌
      tail /var/log/mysql/error.log

      若是出現如下 1593 錯誤

       [ERROR] Slave I/O for channel '': 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. Error_code: 1593

      是由於兩臺數據庫的UUid 相同 緣由是 複製虛擬機 致使文件UUid相同

    2. 解決辦法 : 找到 auto.cnf 文件重命名一下 而後 從新 啓動 Mysql, 就會自動生成新的UUid 這樣的話  主從兩臺服務器的UUid 不相同 報錯解決!
      cd /var/lib/mysql
      mv auto.cnf auto.cnf.bk

       若是不知道 auto.cnf 文件在哪裏 能夠進入Mysql 輸入

      show variables like 'datadir';

      會獲得

      +---------------+-----------------+
      | Variable_name | Value           |
      +---------------+-----------------+
      | datadir       | /var/lib/mysql/ |
      +---------------+-----------------+
      1 row in set (0.01 sec)

      其中 /var/lib/mysql/  就是 auto.cnf 的位置

  6. 測試
    1. 在主庫master 對錶 test 進行 insert update delete 操做 從庫表現一致  即爲成功!
相關文章
相關標籤/搜索