MySQL主從複製架構及原理

    一、簡介

    在實際生產中,數據的重要性不言而喻,因此考慮到數據的重要性比如單點故障導致後端數據庫奔潰,或者後端數據庫訪問壓力過大等,mysql數據庫做主從非常有必要,減輕後端數據庫壓力,主服務器負責讀寫,從服務器只負責讀,這樣一來即保證了數據的可靠性,同時提高服務器的高可用。


MySQL主從複製架構如圖:

wKioL1g1rdzRcxtBAAGH4yjl32g556.png


MySQL主從複製原理:master服務器將數據的改變記錄二進制日誌,當master上的數據發生改變時,則將其改變寫入二進制日誌中,salve服務器會在一定時間間隔內對master二進制日誌進行探測其是否發生改變,如果發生改變,則開始一個I/OThread請求master二進制事件,同時主節點爲每個I/O線程啓動一個dump線程,用於向其發送二進制事件,並保存至從節點本地的中繼日誌中,從節點將啓動SQL線程從中繼日誌中讀取二進制日誌,在本地重放,使得其數據和主節點的保持一致,最後I/OThread和SQLThread將進入睡眠狀態,等待下一次被喚醒。

    

    二、主從複製配置實現

    要求:

    1、雙方mysql版本需一致,如不一致,只要主節點低於從節點

    2、兩節點間時間需同步

    

    配置:

    主服務器配置如下:

    1、修改/etc/my.cnf配置文件

    log-bin=/mydata/data/binlogs/master-bin

    2、創建此目錄並修改屬組屬主爲mysql 

    mkdir /mydata/binlogs/

    chown -R mysql.mysql /mydata/binlogs/

    3、授權用戶

    grant replication slave,replication client on *.* to 'repluser'@'10.1.10.%' identified by 'pass'; 

    flush privileges;

    從服務器配置如下: 

    1、修改/etc/my.cnf配置文件,註釋二進制日誌,開啓中繼日誌,修改server-id和主節點不一致  

    server-id=11

    relay-log=/mydata/relaylogs/relay-bin

    2、創建其目錄並授予此目錄的屬主、屬組爲mysql 

    mkdir /mydata/relaylogs/ 

    chown -R mysql.mysql /mydata/relaylogs/

    3、連接主服務器

    change master to master_host='10.1.10.1',master_user='repluser',master_password='pass'; 

    

    完成上訴配置過程即可各自啓動mysql服務器,且在從服務器上啓動I/O,SQL線程,例如:start slave

    

    測試兩數據可數據是否能同步,則可在主庫上插入數據在從庫上查看是否存在,注意:從庫只能讀,而不能寫

[root@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.32-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.10.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 326
               Relay_Log_File: relay-bin.000007
                Relay_Log_Pos: 611
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes #確保I/O和SQL線程開啓,即可實現數據同步
            Slave_SQL_Running: Yes  #確保I/O和SQL線程開啓,即可實現數據同步
              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: 326
              Relay_Log_Space: 1184
              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
1 row in set (0.00 sec)
ERROR: No query specified
MariaDB [(none)]> #測試在主庫上刪除dbs,驗證從庫是否存在dbs數據庫,結果如下:
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbs                |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]>
[root@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.32-MariaDB-log MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

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

MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbs                |
| hellodb            |
| mydbs              |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.02 sec)

MariaDB [(none)]> drop database dbs;
Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]>



    三、實戰:主從不同步時,如何進行數據同步至一致

    描述:當主服務器已經運行一段時間,並且存在不小的數據時,則需把主服務器備份,然後在從服務器恢復,從備份時所在的位置開始複製。


    1、將主服務器上的數據做完全備份

[root@centos6 ~]# mysqldump --lock-all-tables --all-databases --flush-logs --master-data=2 >/root/all.sql

    2、在從服務器上導入主服務上的完全備份,在導入時關閉I/O和SQL線程

[root@centos6 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 5.5.32-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> source  /root/alren.sql
MariaDB [testdbs]> change master to master_host='10.1.10.1',master_user='repluser',master_password='pass',MASTER_LOG_FILE='master-bin.000007',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.10.1
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000007 #已經恢復到主節點的二進制的位置
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000007
             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: 245
              Relay_Log_Space: 818
              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
1 row in set (0.00 sec)
MariaDB [testdbs]> show tables;
+-------------------+
| Tables_in_testdbs |
+-------------------+
| tbl               |
+-------------------+
1 row in set (0.00 sec)
MariaDB [testdbs]> select * from tbl;
+-------+
| name  |
+-------+
| tom   |
| jerry |
| lucy  |
+-------+
3 rows in set (0.00 sec)
MariaDB [testdbs]> use mydbs;
Database changed
MariaDB [mydbs]> show tables; #測試數據是否和主節點一致
+-----------------+
| Tables_in_mydbs |
+-----------------+
| students        |
+-----------------+
1 row in set (0.00 sec)
MariaDB [mydbs]> select * from students;
+------+-------+
| id   | name  |
+------+-------+
|    1 | tom   |
|    2 | jerry |
+------+-------+
2 rows in set (0.00 sec)
MariaDB [mydbs]>



   

     

    總結:此實戰中最爲關鍵主要有兩步①主服務器上鎖表做完全備份,並滾動日誌,②從服務器上進行半道恢復.










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章