MySQL 主從復(fù)制實驗

準備:

服務(wù)器1:192.168.37.130 (主)
服務(wù)器2:192.168.37.131 (從)

MySQL 版本:5.7.24

開始:

主服務(wù)器設(shè)置:

  1. 修改 my.cnf 配置,完成后重啟數(shù)據(jù)庫
[mysqld]
# 主服務(wù)器啟用二進制日志,之后從服務(wù)器將讀取該日志,復(fù)制到從服務(wù)器的 Relay log 中,并定時執(zhí)行新增的內(nèi)容
log-bin=/usr/local/mysql/logs/mysql-bin
# 指定數(shù)據(jù)庫
binlog-do-db=xxxx   二進制日志記錄的數(shù)據(jù)庫
# 忽略數(shù)據(jù)庫
binlog-ignore-db=xxxx
# 忽略 server-id 配置時,默認其為 0,主服務(wù)器將拒絕來自從服務(wù)器的任何連接
server-id=1

# 使用帶事務(wù)的InnoDB進行復(fù)制設(shè)置時盡可能提高持久性和一致性
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
  1. 確保 skip_networking 為關(guān)閉狀態(tài)(OFF)
mysql> SHOW VARIABLES LIKE '%skip_networking%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| skip_networking | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)
  1. 創(chuàng)建從服務(wù)器用戶復(fù)制數(shù)據(jù)的用戶并授予權(quán)限
mysql> GRANT REPLICATION SLAVE ON *.* to 'slave1'@'192.168.10.131' identified by 'password';

從服務(wù)器配置:

  1. 修改 my.cnf 配置,完成后重啟
[mysqld]
server-id=2

# 設(shè)定需要復(fù)制的數(shù)據(jù)庫
replicate-do-db=xxx
# 設(shè)定需要忽略的數(shù)據(jù)庫 
replicate-ignore-db=xxx
  1. 配置連接主服務(wù)器的賬號信息
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.37.130',
    -> MASTER_USER='slave1',
    -> MASTER_PASSWORD='password';
Query OK, 0 rows affected, 2 warnings
  1. 啟動復(fù)制線程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
  1. 檢查狀態(tài) Slave_IO_Running 與 Slave_SQL_Running 為 YES 則表示成功啟動
mysql> SHOW slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.37.130
                  Master_User: slave1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1464
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 1677
        Relay_Master_Log_File: mysql-bin.000002
             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: 1464
              Relay_Log_Space: 2101
              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:  c19b2ca3-b2e7-11ea-9aaa-000c29839863
             Master_Info_File: /usr/local/mysql/data/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

測試效果

在主服務(wù)器上執(zhí)行創(chuàng)建或插入語句,在從服務(wù)器中檢查是否成功復(fù)制

注意事項:

  1. 當啟用主從復(fù)制并主服務(wù)器中還有數(shù)據(jù)時,需要另外進行處理,可以先使用 mysqldump 將之前的數(shù)據(jù)復(fù)制到從數(shù)據(jù)庫
  2. 添加新的從服務(wù)器時,新的服務(wù)器的 server-id 不可與之前的服務(wù)器重復(fù)

常用命令

# 關(guān)閉從服務(wù)器所有類型線程,也就是停止從主服務(wù)器復(fù)制數(shù)據(jù)的操作
mysql> STOP SLAVE;

# 單獨關(guān)閉某個線程
mysql> STOP SLAVE IO_THREAD;
mysql> STOP SLAVE SQL_THREAD;

# 啟動線程
mysql> START SLAVE;
mysql> START SLAVE IO_THREAD;
mysql> START SLAVE SQL_THREAD;

# 從庫刪除主從關(guān)系
mysql> RESET SLAVE;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容