mysql互為主從

業(yè)務(wù)場(chǎng)景:

目前項(xiàng)目由于業(yè)務(wù)需要,增加一臺(tái)備機(jī),在主機(jī)宕機(jī)時(shí)(拔掉網(wǎng)線等),備機(jī)工作.主備機(jī)使用不同的數(shù)據(jù)庫(kù).

配置描述:

        - 主機(jī): 192.168.101.179,端口:3306,版本:5.7.35

        - 備機(jī): 192.168.101.57,端口:3306,版本:5.7.35

配置注意:

注意: 配置時(shí)一定要停止寫入數(shù)據(jù) 不然主從庫(kù)數(shù)據(jù)不一致會(huì)出現(xiàn)無法同步的情況

配置:

1.打開主數(shù)據(jù)庫(kù)的my.cnf文件 編輯如下

server-id = 1 #主庫(kù)id
skip-name-resolve #忽略客戶端鏈接時(shí)的主機(jī)名日志打印(可提高速度,非必選)
long_query_time=1 #查詢速度低于1秒的進(jìn)行日志記錄
innodb-file-per-table=1 #開啟獨(dú)立表空間
innodb_flush_log_at_trx_commit = 2 #提交事務(wù)的時(shí)候?qū)?redo 日志寫入磁盤中
log_warnings = 1 #記錄警告標(biāo)識(shí)
connect_timeout = 60 #鏈接超時(shí)時(shí)間
net_read_timeout = 120 #數(shù)據(jù)讀取超時(shí)時(shí)間
performance_schema_max_table_instances = 400 #檢測(cè)表對(duì)象的最大數(shù)量
log-bin=master-bin # bin日志名稱
sync-binlog=1 #使執(zhí)行1次寫入后,與硬盤同步
binlog-do-db=location #主數(shù)據(jù)庫(kù)名稱(這里替換成你的數(shù)據(jù)庫(kù)名字)
binlog-ignore-db = mysql #忽略以下庫(kù)的同步
binlog-ignore-db = performance_schema #忽略以下庫(kù)的同步
binlog-ignore-db = information_schema #忽略以下庫(kù)的同步
relay-log = relay-log #副本日志名稱
relay-log-index = relay-log.index#副本日志索引位置
binlog_format = ROW #復(fù)制方法,逐條復(fù)制
  1. 打開從數(shù)據(jù)庫(kù)的my.cnf文件 編輯如下(除了server-id其他配置都一樣)
server-id = 2 #從庫(kù)id
skip-name-resolve #忽略客戶端鏈接時(shí)的主機(jī)名日志打印(可提高速度,非必選)
long_query_time=1 #查詢速度低于1秒的進(jìn)行日志記錄
innodb-file-per-table=1 #開啟獨(dú)立表空間
innodb_flush_log_at_trx_commit = 2 #提交事務(wù)的時(shí)候?qū)?redo 日志寫入磁盤中
log_warnings = 1 #記錄警告標(biāo)識(shí)
connect_timeout = 60 #鏈接超時(shí)時(shí)間
net_read_timeout = 120 #數(shù)據(jù)讀取超時(shí)時(shí)間
performance_schema_max_table_instances = 400 #檢測(cè)表對(duì)象的最大數(shù)量
log-bin=master-bin # bin日志名稱
sync-binlog=1 #使執(zhí)行1次寫入后,與硬盤同步
binlog-do-db=location #主數(shù)據(jù)庫(kù)名稱(這里替換成你的數(shù)據(jù)庫(kù)名字)
binlog-ignore-db = mysql #忽略以下庫(kù)的同步
binlog-ignore-db = performance_schema #忽略以下庫(kù)的同步
binlog-ignore-db = information_schema #忽略以下庫(kù)的同步
relay-log = relay-log #副本日志名稱
relay-log-index = relay-log.index#副本日志索引位置
binlog_format = ROW #復(fù)制方法,逐條復(fù)制

3.登錄主庫(kù)(使用navicat或者直接登錄主庫(kù))

[root@localhost ~]# mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13222
Server version: 5.7.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4.切換數(shù)據(jù)庫(kù)

mysql> use location;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

5.查看主庫(kù)狀態(tài)

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-bin.000007
         Position: 894227729
     Binlog_Do_DB: location
 Binlog_Ignore_DB: mysql,performance_schema,information_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

記錄這些值:

File: master-bin.000007 #當(dāng)前日志文件的名字
Position: 894227729 #日志文件的位置

6.登錄從庫(kù),變更從庫(kù)的復(fù)制位置

CHANGE MASTER TO
    MASTER_HOST ='192.168.101.179',
    MASTER_USER ='root',
    MASTER_PASSWORD ='root',
    MASTER_LOG_FILE = 'master-bin.000007',
    MASTER_LOG_POS = 894227729;

7.開啟從庫(kù)復(fù)制

start slave;

8.查看從庫(kù)slave狀態(tài)

show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.179
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000007
          Read_Master_Log_Pos: 894227729
               Relay_Log_File: relay-log.000037
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-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: 4701104
              Relay_Log_Space: 689
              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: 2
                  Master_UUID: db36dfe3-9479-11ec-b4a7-7478271b8174
             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)

如果顯示如下,則表示主從復(fù)制成功:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果其中有一個(gè)IO為no 則表示失敗,這時(shí)可以查看日志,看看失敗原因并糾正:

Last_IO_Error: io失敗原因
Last_SQL_Error:sql失敗原因
Slave_SQL_Running_State:從庫(kù)sql運(yùn)行狀態(tài)

9.查看從庫(kù)master狀態(tài)

mysql> show master status\G;
*************************** 1. row ***************************
             File: master-bin.000002
         Position: 4701104 | 
     Binlog_Do_DB: location
 Binlog_Ignore_DB: mysql,performance_schema,information_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

記錄一下值

File: master-bin.000002
Position: 4701104

10.登錄主庫(kù),切換主庫(kù)master

CHANGE MASTER TO
    MASTER_HOST ='192.168.101.57',
    MASTER_USER ='root',
    MASTER_PASSWORD ='root',
    MASTER_LOG_FILE = 'master-bin.000002',
    MASTER_LOG_POS = 4701104;

11.啟動(dòng)主庫(kù)slave

start slave;

12.查看主庫(kù)slave狀態(tài)

show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.101.57
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 4701104
               Relay_Log_File: relay-log.000037
                Relay_Log_Pos: 321
        Relay_Master_Log_File: master-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: 4701104
              Relay_Log_Space: 689
              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: 2
                  Master_UUID: db36dfe3-9479-11ec-b4a7-7478271b8174
             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)

如果顯示如下,則表示主從復(fù)制成功:

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果其中有一個(gè)IO為no 則表示失敗,這時(shí)可以查看日志,看看失敗原因并糾正:

Last_IO_Error: io失敗原因
Last_SQL_Error:sql失敗原因
Slave_SQL_Running_State:從庫(kù)sql運(yùn)行狀態(tài)

至此配置結(jié)束!這時(shí)當(dāng)主庫(kù)/從庫(kù) 拔掉網(wǎng)線,在啟動(dòng)時(shí)會(huì)自動(dòng)同步從/主庫(kù)信息

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • MySQL主從復(fù)制原理、半同步操作步驟及原理 轉(zhuǎn)載2016年09月21日 11:51:23 7195 1.1 企業(yè)...
    阿斯蒂芬2閱讀 3,101評(píng)論 0 7
  • 今天練習(xí)了下mysql的主從復(fù)制功能,根據(jù)網(wǎng)絡(luò)上的一些文章整理修改 環(huán)境 操作系統(tǒng) ubuntu18.04mysq...
    白紅薯粉閱讀 611評(píng)論 0 0
  • 一、MySQL主從復(fù)制 1.1 為什么需要主從? 數(shù)據(jù)庫(kù)損壞了(業(yè)務(wù)不能使用數(shù)據(jù)庫(kù))原因: 外在原因1. 網(wǎng)絡(luò)2....
    PickachuDev閱讀 805評(píng)論 0 0
  • 一、環(huán)境準(zhǔn)備 操作系統(tǒng):CentOS Linux release 7.9.2009 (虛擬機(jī))數(shù)據(jù)庫(kù)版本:mysq...
    liurongming閱讀 323評(píng)論 0 0
  • 這篇博客會(huì)詳細(xì)介紹如何配置主從配置,但重點(diǎn)是想分享如何當(dāng)配置未成功時(shí),如何調(diào)試。 原文連接(推薦這個(gè)看 比較清晰)...
    海里的沙丁魚閱讀 999評(píng)論 0 2

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