真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

mysql主從復(fù)制及維護--單主、雙主

本文講述了MySQL單主、雙主模式的配置方法、注意事項、和維護的一些事項。

成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的曲阜網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!


1. 單主配置實例

1.1 整體流程:

  1. master與slave時間同步
  2. master開啟二進制記錄功能并授權(quán)一個特定的用戶對二進制文件有復(fù)制權(quán)限
  3. slave開啟中繼日志功能,執(zhí)行同步語句,開啟同步功能。
    • 注 :版本號不一致時,master版本可比slave版本低

1.2 master端配置

編輯/etc/my.cnf,在mysqld配置段中添加以下內(nèi)容(需重啟服務(wù))

server-id=1
log_bin=/data/mysql/logs/mysql-bin

啟動數(shù)據(jù)庫并做授權(quán):

MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'node2'@'192.168.200.%' IDENTIFIED BY 'node2pass';
MariaDB [(none)]> FLUSH PRIVILEGES;

1.3 master端注意事項

  1. /data/mysql/logs目錄需存在,且屬主、屬組為運行mysql的用戶(因mysql需在該目錄創(chuàng)建、寫入文件)
  2. mysql-bin可自行定義。
  3. server-id后面的數(shù)值唯一,各節(jié)點之間不能重復(fù)。

1.4 slave端配置

編輯/etc/my.cnf,在mysqld配置段中添加以下內(nèi)容(需重啟服務(wù))

server-id=2
relay-log=/data/mysql/logs/relay-bin

啟動服務(wù)后設(shè)置主節(jié)點并啟動

MariaDB [(none)]> change master to master_host='192.168.200.41',master_port=3306,master_user='node2',master_password='node2pass',master_log_file='mysql-bin.000003', master_log_pos =4;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.41
                  Master_User: node2
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 927
               Relay_Log_File: relay-bin.000003
                Relay_Log_Pos: 604
        Relay_Master_Log_File: mysql-bin.000003
             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: 927
              Relay_Log_Space: 1783
              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

查看show slave status時,Slave_IO_Running和Slave_SQL_Running狀態(tài)為yes時即成功。可在主節(jié)點創(chuàng)建庫或者表查看從節(jié)點是否也有。

1.5 slave端注意事項

  1. /data/mysql/logs目錄需存在,且屬主、屬組為運行mysql的用戶(因mysql需在該目錄創(chuàng)建、寫入文件)
  2. relay-bin名字可自行定義。
  3. server-id后面的數(shù)值唯一,不能重復(fù)。
  4. change master時,若master_log_pos的位置不正確,可能會導(dǎo)致Slave I/O thread失敗。錯誤日志如下:
    180603 16:46:49 [Note] Slave I/O thread: connected to master 'node2@192.168.200.41:3306',replication started in log 'mysql-bin.000003' at position 760
    180603 16:46:49 [ERROR] Error reading packet from server: binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000003' at 760, the last event read from 'mysql-bin.000003' at 760, the last byte read from 'mysql-bin.000003' at 769. ( server_errno=1236)
    180603 16:46:49 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000003' at 760, the last event read from 'mysql-bin.000003' at 760, the last byte read from 'mysql-bin.000003' at 769.', Error_code: 1236

    解決方法:
    通過show binary logs查看當(dāng)前的二進制文件有哪些,再通過show binlog events in 'BINLOG FILE NAME',找到正確的POS位置,并在change master時使用正確的POS位置。

    MariaDB [(none)]> show binary logs;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |     30355 |
    | mysql-bin.000002 |   1038814 |
    | mysql-bin.000003 |       491 |
    +------------------+-----------+
    3 rows in set (0.00 sec)
    MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    | Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    | mysql-bin.000003 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
    | mysql-bin.000003 | 245 | Query       |         1 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
    | mysql-bin.000003 | 416 | Query       |         1 |         491 | flush privileges                                                                                 |
    +------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
    3 rows in set (0.00 sec)

    如上代碼:選擇Pos(開始位置)或者End_log_pos(結(jié)束位置)都可以。


2. 雙主配置實例

2.1 整體流程:

  1. 節(jié)點1與節(jié)點2時間同步
  2. 節(jié)點1與節(jié)點2開啟二進制日志及中繼日志功能并授權(quán)一個特定的用戶對二進制文件有復(fù)制權(quán)限
  3. 節(jié)點1與節(jié)點2執(zhí)行同步語句,開啟同步功能。
    注 :
    版本號不一致時,master版本可比slave版本低

2.2 配置文件:

節(jié)點1:編輯/etc/my.cnf,在mysqld配置段添加以下內(nèi)容保存退出:

log_bin=/data/mysql/logs/mysql-bin
relay_log=/data/mysql/logs/relay-log

注:要確保mysql對/data/mysql/logs目錄有讀寫權(quán)限
節(jié)點2:編輯/etc/my.cnf,在mysqld配置段添加以下內(nèi)容保存退出:

log_bin=/data/mysql/logs/mysql-bin
relay_log=/data/mysql/logs/relay-log

注:要確保mysql對/data/mysql/logs目錄有讀寫權(quán)限

節(jié)點1 IP:192.168.200.41 節(jié)點2 IP:192.168.200.42

2.3 數(shù)據(jù)庫操作:

節(jié)點1與節(jié)點2都進行同樣的操作以授權(quán)一個用戶:

MariaDB [(none)]> grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

查看要復(fù)制的Pos位置:
節(jié)點1:

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      491 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| mysql-bin.000003 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
| mysql-bin.000003 | 245 | Query       |         1 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
| mysql-bin.000003 | 416 | Query       |         1 |         491 | flush privileges                                                                                 |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

節(jié)點2:

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      491 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show binlog events in 'mysql-bin.000003';
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                             |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
| mysql-bin.000003 |   4 | Format_desc |         2 |         245 | Server ver: 5.5.56-MariaDB, Binlog ver: 4                                                        |
| mysql-bin.000003 | 245 | Query       |         2 |         416 | grant replication slave,replication client on *.* to lxk@'192.168.200.%' identified by 'lxkpass' |
| mysql-bin.000003 | 416 | Query       |         2 |         491 | flush privileges                                                                                 |
+------------------+-----+-------------+-----------+-------------+--------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

因為為新數(shù)據(jù)庫,兩個節(jié)點的pos值相同,都為491,生產(chǎn)環(huán)境根據(jù)需要調(diào)整,一般位置的選取都是從授權(quán)后的位置開始。

執(zhí)行同步語句:

節(jié)點1:

MariaDB [(none)]> change master to master_host='192.168.200.42',master_user='lxk',master_password='lxkpass',master_log_file='mysql-bin.000003',master_log_pos=491;
Query OK, 0 rows affected (0.27 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

節(jié)點2:

MariaDB [(none)]> change master to master_host='192.168.200.41',master_user='lxk',master_password='lxkpass',master_log_file='mysql-bin.000003',master_log_pos=491;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

2.4 查看狀態(tài)

        MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.41       #主節(jié)點地址
                  Master_User: lxk                          #主節(jié)點用戶名
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 491
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes                  #IO線程開啟
            Slave_SQL_Running: Yes              #SQL線程開啟

只要執(zhí)行結(jié)果中slave_IO_Runing和Slave_SQL_Runing狀態(tài)為Yes,即表示從節(jié)點連接主節(jié)點成功并成功開啟復(fù)制功能。

2.5 測試:

  • 節(jié)點2創(chuàng)建一個數(shù)據(jù)庫名為testdb
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)
  • 節(jié)點1查看本地數(shù)據(jù)庫
MariaDB [(none)]> show databases;                    
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

3 監(jiān)控和維護:

  1. 清理日志
    • 隨著服務(wù)的運行,日志文件會越來越多,越來越大。二進制最好復(fù)制到其它節(jié)點(二進制文件一定要保存),再在mysql命令行下刪除。通過PURGE的操作會反應(yīng)到.index文件中。
MariaDB [testdb]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     30355 |
| mysql-bin.000002 |   1038814 |
| mysql-bin.000003 |      1364 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [testdb]> purge binary logs to 'mysql-bin.000003';          #清空至mysql-bin.000003的日志
Query OK, 0 rows affected (0.01 sec)

MariaDB [testdb]> show binary logs;         #再次查看日志
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |      1364 |
+------------------+-----------+
1 row in set (0.00 sec)

[root@node1 ~]# ls /data/mysql/logs/            #查看二進制日志保存目錄,000003之前的都已經(jīng)刪除
mysql-bin.000003  mysql-bin.index  relay-log.000001  relay-log.000002  relay-log.index
  1. 復(fù)制監(jiān)控

    • 判斷從服務(wù)器是否落后于主服務(wù)器,通過show slave status\G查看。
    • Seconds_Behind_Master: 0 #表示未落后
  2. 如何確定主從節(jié)點數(shù)據(jù)是否一致

    • 雙主模型中極易發(fā)生,可用procona-tools中的pt-table-checksum
  3. 主從數(shù)據(jù)不一致時的修復(fù)方法
    • 停掉復(fù)制線程再重新啟動,從服務(wù)器就會啟動復(fù)制,達到與主節(jié)點一致。

本文題目:mysql主從復(fù)制及維護--單主、雙主
標題網(wǎng)址:http://weahome.cn/article/gehspi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部