1. 程式人生 > >MySQL設置主從復制

MySQL設置主從復制

ror 抓取 log日誌 sql語句 解析 nds lec mys ddl

主從復制:

單向,雙向,環形,級聯,一主多從

雙機復制的5種情形
1、異步主從(默認常規)
2、雙寫(前段程序對兩個數據庫同時寫,必須兩邊都落實,程序才返回成功)
3、利用外掛軟件實現實時主庫Binlog日誌抓取,從而可以在當機的時候補全從庫
4、谷歌開發的半同步插件
5、DRBD

主從讀寫分離
1、通過程序實現(性能,效率最佳,推薦)
php,java等程序可以通過設置多個連接文件輕松實現主從讀寫分離。
2、通過軟件實現讀寫分離
MySQL-proxy, Amoeba等代理軟件也可以實現讀寫分離功能,但是最好還是程序實現。
3、開發dbproxy
讀寫分離邏輯圖展示:

主從同步原理:
實際上是異步的,即總是主庫寫完日誌,日誌才可能被從庫應用。

master slave
線程:IO 線程:IO/SQL
開啟log-bin 需要設置的啟動參數:CHANGE MASTER TO
怎刪改會去寫log-bin MASTER_HOST=‘192.168.1.111‘
MASTER_PORT=3308
MASTER_USER=‘alrinrep‘
MASTER_PASSWORD=‘password123‘
MASTER_LOG_FILE=‘mysql-bin.000047‘
MASTER_LOG_POS=‘5632‘
開啟同步方式:start slave

                從庫IO向主庫發起復制請求,主庫開始對從庫認證,通過後主庫IO根據從庫
                日誌要求開始發送bin-log到從庫relay-log。一個周期後,從庫更新master.info
                ,並重新向主庫發起請求。
                SQL線程從relay-log發現新數據,並寫到slave的數據文件和日誌系統。

                相關語句:--master-data=1,
                CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000020‘, MASTER_LOG_POS=1191;
建立相關的賬號,即alrinrep
並授予應有的權限

MySQL主從復制原理過程
1、Slave服務器上執行start slave,開啟主從復制開關
2、Slave服務器的IO線程會通過在Master上授權的復制用戶權限請求連接Master服務器,並請求從指定的bin-log日誌文件指定位置
(change master to 命令相關參數)之後發送binlog日誌
3、Master服務器接受到來自Slave服務器的IO線程的請求後,Master服務器上負責復制IO線程根據Slave服務器的IO線程請求的信息
讀取指定binlog日誌文件指定位置之後的binlog日誌信息,然後返回給Slave端的IO線程。返回的信息中除了binlog日誌的內容外,
還有本次返回日誌內容後在Master服務器端的新的binlog文件名稱以及在binlog中的下一個指定更新位置

4、當Slave服務器的IO線程獲取到來自Master服務器上的IO線程發送的日誌以及日誌文件及位置點後,將binlog日誌內容一次寫入到
slave端的Relay log(中繼日誌)文件(MySQL-relay-bin.xxxxxx)的最後端,並將新的binlog文件名和位置記錄到master-info文件
中,以便下一次讀取Master端新Binlog日誌時能夠告訴Master服務器需要從新binlog日誌的那個文件哪個文件開始新的binlog內容。
5、Slave服務器端的SQL線程會實時的檢測本地relay log中新增的日誌內容,然後及時把LOG文件中的內容解析成MASTER端曾經執行過
的SQL語句,並在自身SLAVE服務器上安順序執行這些SQL。應用玩後清理應用過的日誌。
6,經過了上述過程,就可以確保MASTER和SLAVE執行了同樣的SQL。在復制正常的情況下,MASTER和SLAVE數據是完全一致的。

主從復制上機實驗:

0,環境:3308/3309同機,IP為192.168.199.151
1,修改my.cnf,檢查server-id和log-bin,主從server-id必須不同。主庫必須開啟bin-log
2,添加復制賬號和對應的 replication slave 權限

    mysql> select user,host,password from user;
    +-------+-----------+-------------------------------------------+
    | user  | host      | password                                  |
    +-------+-----------+-------------------------------------------+
    | root  | localhost | *A0F874BC7F54EE086FCE60A37CE7887D8B31086B |
    | alrin | %         | *A0F874BC7F54EE086FCE60A37CE7887D8B31086B |
    +-------+-----------+-------------------------------------------+
    2 rows in set (0.00 sec)

    mysql> grant replication slave on *.* to alrinrep@‘192.168.199.%‘ identified by ‘password123‘;
    Query OK, 0 rows affected (0.00 sec)

    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    mysql> show variables like ‘log_bin‘;
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | log_bin       | ON    |
    +---------------+-------+
    1 row in set (0.00 sec)

    mysql> 

3,主庫上做備份。先鎖表,再備份

    mysql> flush table with read lock;
    Query OK, 0 rows affected (0.03 sec
    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000004 |      423 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
新開session窗口後執行備份:

[root@localhost backup]# mysqldump -uroot -ppassword123 -S /data/mysqldata/3308/mysql.sock -A -B --events --master-data=2| gzip > /data/mysqldata/backup/rep.sql.gz

備份後檢查:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      423 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
解鎖表:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> 

4,從庫操作

    mysql> change master to 
        -> MASTER_HOST=‘192.168.199.151‘,
        -> MASTER_PORT=3308,
        -> MASTER_USER=‘alrinrep‘, 
        -> MASTER_PASSWORD=‘password123‘, 
        -> MASTER_LOG_FILE=‘mysql-bin.000004‘, 
        -> MASTER_LOG_POS=423;
    Query OK, 0 rows affected, 2 warnings (0.05 sec)

5,開啟復制

    mysql> start slave;
    Query OK, 0 rows affected (0.25 sec)

6,檢查slave狀態:

mysql> show slave status\G
    *************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.199.151
              Master_User: alrinrep
              Master_Port: 3308
            Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 423
               Relay_Log_File: mysql-relay-bin.000002
            Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000004
             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: 423
              Relay_Log_Space: 456
              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: 16b758c5-2455-11e9-8fc6-080027339667
             Master_Info_File: /data/mysqldata/3309/data/master.info
                SQL_Delay: 0
          SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
    1 row in set (0.00 sec)

    mysql> 

7,嘗試在主庫DDL,檢查復制狀態
主庫:

    mysql> create database alrin;
    Query OK, 1 row affected (0.00 sec)
    從庫:
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | alrin              |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    5 rows in set (0.00 sec)

命令匯總:
1,grant replication slave on . to alrinrep@‘192.168.199.%‘ identified by ‘password123‘;
2,flush table with read lock;
3,show master status;
4,unlock tables;
5,change master to
MASTER_HOST=‘192.168.199.151‘,
MASTER_PORT=3308,
MASTER_USER=‘alrinrep‘,
MASTER_PASSWORD=‘password123‘,
MASTER_LOG_FILE=‘mysql-bin.000004‘,
MASTER_LOG_POS=423;
6,start slave;
7,show slave status\G

MySQL設置主從復制