1. 程式人生 > >mysql主從複製,基於GTID主從複製,並行複製,半同步複製

mysql主從複製,基於GTID主從複製,並行複製,半同步複製

複製方式:

主–從複製(A-B一主一從或者A-BC一主多從)
基於GTID複製
非同步複製
半同步複製

複製原理:

Mysql中有一種日誌叫做bin日誌(二進位制日誌)。這個日誌會記錄下所有修改了資料庫的SQL語句
主從複製的原理其實就是把主伺服器上的bin日誌複製到從伺服器上執行一遍,這樣從伺服器上的資料就和
主伺服器上的資料相同了。

Mysql複製特點:

非同步複製:主節點中一個使用者請求一個寫操作時,主接點不需要把寫的資料在本地操作完成同時就響應使用者。
但是,從節點中的資料有可能會落後主服務,可以使用(很多軟體來檢查是否落後)導致主從資料不一致。

mysql主從複製原理:

從庫生成兩個執行緒,一個I/O執行緒,一個SQL執行緒;
i/o執行緒去請求主庫 的binlog,並將得到的binlog日誌寫到relay log(中繼日誌) 檔案中;
主庫會生成一個 log dump 執行緒,用來給從庫 i/o執行緒傳binlog;
SQL 執行緒,會讀取relay log檔案中的日誌,並解析成具體操作,來實現主從的操作一致,而最終資料一致;

AB機的主從複製:

如果想要配置一主多從,從節點配置相同,server-id不同即可

**主節點:**
    啟用二進位制日誌。
    為當前節點設定一個全域性唯一的server_id。
    建立有複製許可權的使用者賬號 REPLIACTION SLAVE ,REPLIATION CLIENT。

**從節點:**
    啟動中繼日誌。
    為當前節點設定一個全域性唯一的server_id。
    使用有複製許可權的使用者賬號連線至主節點,並啟動複製執行緒。

實驗環境:

主端:172.25.254.1  server1
從端:172.25.254.2  server2

主端:

[[email protected] ~]# ls
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
[[email protected] ~]# yum install -y *  安裝服務

這裡寫圖片描述

[[email protected] ~]# /etc/init.d/mysqld start  開啟服務
Initializing MySQL database:                               [  OK  ]
Installing validate password plugin:                       [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] ~]# vim /etc/my.cnf  編輯my.cnf常用引數配置
[[email protected] ~]# cat /etc/my.cnf | tail -n 2
啟用二進位制日誌檔案
    新增:log-bin = mysql-bin
    新增 :server-id =1
server-id=1   
log-bin=mysql-bin
[[email protected] ~]# /etc/init.d/mysqld restart  重啟服務檔案內容生效
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] ~]# grep password /var/log/mysqld.log   檢視日誌會看到出是密碼
2018-08-08T12:25:20.167210Z 1 [Note] A temporary password is generated for [email protected]: jdomj?l:W2yp
[email protected]:後面的就是密碼

這裡寫圖片描述
從端:

[[email protected] ~]# ls
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
[[email protected] ~]# yum install -y *  安裝服務

這裡寫圖片描述

[[email protected] ~]# vim /etc/my.cnf   編輯my.cnf常用引數配置
[[email protected] ~]# cat /etc/my.cnf | tail -n 1
server-id=2   填寫server-id和主端不同即可
[[email protected] ~]# /etc/init.d/mysqld start  開啟服務
Initializing MySQL database:                               [  OK  ]
Installing validate password plugin:                       [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] ~]# grep password /var/log/mysqld.log  過濾密碼
2018-08-08T12:27:29.954223Z 1 [Note] A temporary password is generated for [email protected]: srpy%>EUr5rA
[email protected]:後面的就是密碼

這裡寫圖片描述
主端初始化完成:

[[email protected] ~]# mysql_secure_installation  進入初始化安裝

Securing the MySQL server deployment.

Enter password for user root:   輸入預設的初始化密碼

The existing password for the user account root has expired. Please set a new password.

New password:   輸入新密碼:要求大小寫字母數字和字元

Password updated successfully!  密碼配置成功
Reloading privilege tables..
 ... Success!
Remove anonymous users? [Y/n]   詢問是否不允許匿名使用者登入
Disallow root login remotely? [Y/n]   詢問是否不允許遠端連線
Remove test database and access to it? [Y/n]  詢問是否刪除測試資料庫並訪問它
Reload privilege tables now? [Y/n]   詢問是否重新載入許可權表

這裡寫圖片描述
主端新增授權:

mysql> show databases;  顯示所有資料庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> grant replication slave on *.* to [email protected]'172.25.254.%' identified by 'Yakexi+007';  在主節點上建立有複製許可權的使用者,網段為另外一臺虛擬機器的IP
Query OK, 0 rows affected, 1 warning (0.17 sec)

mysql> show master status;  檢視狀態
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1428 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

圖文Position應該是一致的,因為每次作會有變化。
這裡寫圖片描述
從端可以登陸主端

[[email protected] ~]# mysql -u repl -p -h 172.25.254.1  登陸主端輸入主端設定的密碼即可登陸
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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.

mysql> quit
Bye

這裡寫圖片描述
從端初始化:

[[email protected] ~]# mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:   輸入預設的初始化密碼

The existing password for the user account root has expired. Please set a new password.

New password:   輸入新密碼:要求大小寫字母數字和字元

Password updated successfully!  密碼配置成功
Reloading privilege tables..
 ... Success!
Remove anonymous users? [Y/n]   詢問是否不允許匿名使用者登入
Disallow root login remotely? [Y/n]   詢問是否不允許遠端連線
Remove test database and access to it? [Y/n]  詢問是否刪除測試資料庫並訪問它
Reload privilege tables now? [Y/n]   詢問是否重新載入許可權表

這裡寫圖片描述

從端可以連線並檢視slave資訊:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> change master to master_host='172.25.254.1',master_user='repl',master_password='Yakexi+007',master_log_file='mysql-bin.000001',master_log_pos=1428;   在從節點配置訪問主節點的引數資訊,對應寫即可
Query OK, 0 rows affected, 2 warnings (1.38 sec)

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

mysql> show slave status\G   檢視從節點的狀態資訊看是否連線成功
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1428
               Relay_Log_File: server2-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes     ##這兩個引數變為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: 1428
              Relay_Log_Space: 529
              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: 1d70d2c8-9b06-11e8-ac6a-525400f867b2
             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)

這裡寫圖片描述
主端建立資料庫資料表插入欄位用於測試 :

mysql> create database love;  建立資料庫
Query OK, 1 row affected (0.21 sec)

mysql> use love;  
Database changed
mysql> create table ours(
    -> username varchar(10) not null,
    -> password varchar(10) not null);  建立資料表
Query OK, 0 rows affected (1.21 sec)

mysql> desc ours;  檢視資料表結構
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO   |     | NULL    |       |
| password | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into ours values('xfl','123');  插入欄位
Query OK, 1 row affected (0.30 sec)

mysql> insert into ours values('xyy','456');  插入欄位
Query OK, 1 row affected (0.26 sec)

mysql> select * from ours;  顯示欄位資訊
+----------+----------+
| username | password |
+----------+----------+
| xfl      | 123      |
| xyy      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

這裡寫圖片描述
從端可以檢視資料表字段資訊:

mysql> use love;  使用資料庫
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
mysql> select * from ours;  檢視欄位資訊已經複製成功
+----------+----------+
| username | password |
+----------+----------+
| xfl      | 123      |
| xyy      | 456      |
+----------+----------+
2 rows in set (0.00 sec)

Mysql-5.7 基於GTID的主從複製

從 MySQL 5.6.5 開始新增了一種基於 GTID 的複製方式。通過 GTID 保證了
每個在主庫上提交的事務在叢集中有一個唯一的ID。這種方式強化了資料庫的主備
一致性,故障恢復以及容錯能力。

實驗環境在上一個實驗基礎上進行配置:
從端修改my.cnf檔案並停止slave:

[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /etc/my.cnf | tail -n 4
server-id=2

gtid_mode=ON  開啟gtid模式
enforce-gtid-consistency=true   強制gtid一直性,用於保證啟動gitd後事務的安全;
[[email protected] ~]# /etc/init.d/mysqld restart  重啟服務
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

這裡寫圖片描述
主端修改配置檔案重啟服務:

[[email protected] ~]# cat /etc/my.cnf |tail -n 5
server-id=1   
log-bin=mysql-bin

gtid_mode=ON
enforce-gtid-consistency=true
[[email protected] ~]# /etc/init.d/mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

這裡寫圖片描述

[[email protected] ~]# mysql -p  登陸資料庫
mysql> show slave status\G  檢視slave狀態資訊
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.254.1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: server2-relay-bin.000005
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes   ##這兩行為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: 154
              Relay_Log_Space: 742
              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: 1d70d2c8-9b06-11e8-ac6a-525400f867b2
             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)

這裡寫圖片描述

mysql> stop slave;  停止slave
Query OK, 0 rows affected (0.17 sec)

這裡寫圖片描述
主端檢視狀態:

[[email protected] ~]# /etc/init.d/mysqld restart  重啟服務
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] ~]# mysql -p

mysql> show master status;  檢視master狀態
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

這裡寫圖片描述
從端重新建立連線:

MASTER_AUTO_POSITION:引數意思
該引數在mysql5.6.5版本引入,如果進行change master to時使用MASTER_AUTO_POSITION = 1,
slave連線master將使用基於GTID的複製協議。
使用基於GTID協議的複製,slave會告訴master它已經接收到或執行了哪些事務。
使用基於GTID的複製時(MASTER_AUTO_POSITION = 1),首先要開啟gtid_mode(在my.cnf中設定gtid-mode = ON),
mysql> change master to master_host='172.25.254.1',master_user='repl',master_password='Yakexi+007',MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.37 sec)

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

這裡寫圖片描述
主端在表中插入欄位:

mysql> use love;
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
mysql> show tables;
+----------------+
| Tables_in_love |
+----------------+
| ours           |
+----------------+
1 row in set (0.00 sec)

mysql> insert into ours values('xxx','789');  插入欄位
Query OK, 1 row affected (0.12 sec)

這裡寫圖片描述
從端可以複製欄位資訊實時檢視:

mysql> use love;
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
mysql> select * from ours;
+----------+----------+
| username | password |
+----------+----------+
| xfl      | 123      |
| xyy      | 456      |
| xxx      | 789      |
+----------+----------+
3 rows in set (0.00 sec)

這裡寫圖片描述
mysql主從複製存在的問題:

主庫宕機後,資料可能丟失
從庫只有一個sql Thread,主庫寫壓力大,複製很可能延時
解決方法:
半同步複製---解決資料丟失的問題
並行複製----解決從庫複製延遲的問題

並行複製:

MySQL從5.6開始有了SQL Thread多個的概念,可以併發還原資料,
即並行複製技術。MySQL 5.6中,設定引數slave_parallel_workers = 4(>1),
即可有4個SQL Thread(coordinator執行緒)來進行並行複製,其狀態為:
Waiting for an evant from Coordinator。
在MySQL 5.7中,引入了基於組提交的並行複製(Enhanced Multi-threaded Slaves),
設定引數slave_parallel_workers>0並且global.slave_parallel_type=‘LOGICAL_CLOCK’,
即可支援一個schema下,slave_parallel_workers個的worker執行緒
併發執行relay log中主庫提交的事務。其核心思想:一個組提交的事務
都是可以並行回放(配合binary log group commit);
slave機器的relay log中 last_committed相同的事務(sequence_num不同)
可以併發執行。
其中,變數slave-parallel-type可以有兩個值:DATABASE 預設值,
基於庫的並行複製方式;LOGICAL_CLOCK:基於組提交的並行複製方式
MySQL 5.7開啟Enhanced Multi-Threaded Slave配置

這裡寫圖片描述
新增引數永久生效:

[[email protected] ~]# cat /etc/my.cnf | tail -n 10
log-slave-updates

gtid_mode=ON
enforce-gtid-consistency=true

slave-parallel-type=LOGICAL_CLOCK

slave-parallel-workers=16

若將slave_parallel_workers設定為0,則MySQL 5.7退化為原單執行緒複製,
但將slave_parallel_workers設定為1,則SQL執行緒功能轉化為coordinator執行緒,
但是隻有1個worker執行緒進行回放,也是單執行緒複製。然而,這兩種效能卻又有一些
的區別,因為多了一次coordinator執行緒的轉發,因此slave_parallel_workers=1
的效能反而比0還要差

**master_info_repository=TABLE**

開啟MTS功能後,務必將引數master_info_repostitory設定為TABLE,
這樣效能可以有50%~80%的提升。這是因為並行複製開啟後對於元master.info
這個檔案的更新將會大幅提升,資源的競爭也會變大。在之前InnoSQL的版本中,
添加了引數來控制重新整理master.info這個檔案的頻率,甚至可以不重新整理這個檔案。
因為重新整理這個檔案是沒有必要的,即根據master-info.log這個檔案恢復本身就是
不可靠的。在MySQL 5.7中,Inside君推薦將master_info_repository設定為
TABLE,來減小這部分的開銷。
relay_log_info_repository=TABLE
relay_log_recovery=ON

這裡寫圖片描述
顯示哪些執行緒正在執行

mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| 28 | root        | localhost | test | Query   |    0 | starting                                               | show processlist |
| 30 | system user |           | NULL | Connect | 1301 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
引數
id       #ID標識,要kill一個語句的時候很有用
use      #當前連線使用者
host     #顯示這個連線從哪個ip的哪個埠上發出
db       #資料庫名
command  #連線狀態,一般是休眠(sleep),查詢(query),連線(connect)
time     #連線持續時間,單位是秒
state    #顯示當前sql語句的狀態
info     #顯示這個sql語句

這裡寫圖片描述
當開啟16個執行緒再次檢視哪些執行緒正在執行:

mysql> show processlist;  設定了16個執行緒的結果
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
|  1 | system user |           | NULL | Connect | 3748 | Slave has read all relay log; waiting for more updates | NULL             |
|  3 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  4 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  5 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  6 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  7 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  8 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
|  9 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 10 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 12 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 13 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 14 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 15 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 16 | system user |           | NULL | Connect | 3988 | Waiting for an event from Coordinator                  | NULL             |
| 17 | system user |           | NULL | Connect | 3987 | Waiting for an event from Coordinator                  | NULL             |
| 18 | system user |           | NULL | Connect | 3987 | Waiting for an event from Coordinator                  | NULL             |
| 19 | system user |           | NULL | Connect | 3987 | Waiting for an event from Coordinator                  | NULL             |
| 23 | system user |           | NULL | Connect | 3749 | Waiting for master to send event                       | NULL             |
| 24 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
19 rows in set (0.00 sec)

半同步複製:

預設情況下,MySQL的複製功能是非同步的,非同步複製可以提供最佳的效能, 
主庫把binlog日誌傳送給從庫,這一動作就結束了,並不會驗證從庫是
否接收完畢,這一過程,也就意味著有可能出現當主伺服器或從服務
器端發生故障的時候,有可能從伺服器沒有接收到主伺服器傳送過來的
binlog日誌,這就會造成主伺服器和從伺服器的資料不一致,甚至在恢
復時造成資料的丟失。注意:半同步複製模式必須在主伺服器和從伺服器
端同時開啟,否則主伺服器預設使用非同步複製模式。

這裡寫圖片描述
在主端:

mysql> show global variables like 'have_dynamic_loading';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| have_dynamic_loading | YES   |
+----------------------+-------+
1 row in set (0.00 sec)

mysql> show global variables like 'plugin_dir';
+---------------+--------------------------+
| Variable_name | Value                    |
+---------------+--------------------------+
| plugin_dir    | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+
1 row in set (0.00 sec)

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';  安裝服務外掛
Query OK, 0 rows affected (0.11 sec)
rpl_semi_sync_master_enabled 引數控制主節點是否開啟半同步複製;rpl_semi_sync_master_timeout 
引數控制主節點等待備節點返回確認資訊的超時時間,單位為毫秒,超過這個時間後半同步複製轉變成非同步複製,
mysql> set global rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> set global rpl_semi_sync_master_enabled = ON;
Query OK, 0 rows affected (0.00 sec)

這裡寫圖片描述
在從端:

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';  安裝外掛
Query OK, 0 rows affected (0.14 sec)

mysql> set global rpl_semi_sync_slave_enabled=ON;  rpl_semi_sync_master_enabled 引數控制主節點是否開啟半同步複製
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)

這裡寫圖片描述
主端檢視引數:

mysql> show status like '%rpl_semi_sync%';  
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

從端關閉io_thread:

mysql> stop slave io_thread;  
Query OK, 0 rows affected (0.06 sec)

這裡寫圖片描述
主端修改資料表字段:

mysql> use love;
Database changed
mysql> insert into ours values('ss','3');  插入欄位會有卡頓預設時間是10s,10s過後會自動變成非同步複製
Query OK, 1 row affected (10.83 sec)

mysql> show status like '%rpl_semi_sync%';  一些引數的值也會變化
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | OFF   |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec)

這裡寫圖片描述
從端開啟檢視io_thread

mysql> start slave io_thread;  開啟slave io_thread
Query OK, 0 rows affected (0.00 sec)

mysql> use love;
Database changed
mysql> select * from ours;  資料已經同步
+----------+----------+
| username | password |
+----------+----------+
| xfl      | 123      |
| xyy      | 456      |
| xxx      | 789      |
| ss       | 3        |
+----------+----------+
4 rows in set (0.00 sec)

這裡寫圖片描述

相關推薦

mysql主從複製基於GTID主從複製並行複製同步複製

複製方式: 主–從複製(A-B一主一從或者A-BC一主多從) 基於GTID複製 非同步複製 半同步複製 複製原理: Mysql中有一種日誌叫做bin日誌(二進位制日誌)。這個日誌會記錄下所有修改了資料庫的SQL語句 主從複製的原理其實就是把主伺服器上的bin日

運維筆記36 mysql的一主多從模型(原始主從複製基於GTID主從複製

概述: mysql的主從複製是十分經典的一個應用,但是主從之間總會有資料一致性(data consistency )的問題,一般情況從庫會落後主庫幾個小時,而且在傳統一主多從(mysql5.6之前)的模型中當master down掉後,我們不只是需要將一個sl

MySQL5.7主從複製基於GTID主從複製同步、組複製、全同步解析

一、主從複製 1.環境 系統:redhat6.5 防火牆:保持關閉 selinux=disabled mysql主機:server1 172.25.32.4/24 mysql從機:server2 172.25.32.5/24 2.MySQL

mysql 主從複製 基於gtid同步複製並行複製同步複製

一、mysql 主從複製 1.主從形式 mysql主從複製 靈活 一主一從 主主複製 一主多從---擴充套件系統讀取的效能,因為讀是在從庫讀取的; 多主一從---5.7開始支援 聯級複製--- 2.主從複製的用途及部署條件 mysql主從複製用途 實時災備,

mysql主從複製基於gtid主從複製

MySQL複製原理,其通過三個執行緒來完成,在master節點上執行的binlogdump執行緒以及在slave節點上執行的I/O執行緒和SQL執行緒。 1. master節點上的binlogdump執行緒,在slave與其正常連線的情況下,將binlog傳送到slave上。

mysql主從複製基於GTID主從同步複製並行複製

環境: 實驗環境: rhel6.5 , selinux和iptables均為disabled狀態,mysql均為5.7.17,或者slave比master版本高 實驗主機: 172.25.254.2 server2:master 172.25.254.3 server3:s

mysql主從複製基於gtid

mysql的主從複製 主伺服器將更新寫入二進位制日誌檔案,並維護檔案的一個索引以跟蹤日誌迴圈。這些日誌可以記錄傳送到從伺服器的更新.當一個從伺服器連線主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主

MySQL主從複製基於GTID及多執行緒

一、Mysql 5.6 複製管理工具 官方下載:http://dev.mysql.com/downloads/tools/utilities/#downloads mysqlreplicate 快速啟動複製 mysqlrplcheck 快速檢查複製環境

mysql5.7的主從複製基於GTID複製並行複製同步複製

一 最簡單的AB主從複製 MySQL之間資料複製的基礎是二進位制日誌檔案(binary log file)。一臺MySQL資料庫一旦啟用二進位制日誌後,其作為master,它的資料庫中所有操作都會以“事件”的方式記錄在二進位制日誌中,其他資料庫作為slave通

MySQL主從複製並行複製同步複製和組複製

主從複製 主從複製過程存在三個執行緒,Master端的I/O執行緒,Slave的I/O執行緒與SQL執行緒。Master端需要開啟binlog日誌,Slave端需要開啟relaylog。 1、Slave端的I/O讀取master.info檔案,獲取binlog檔名和位置點,然後向Mast

MHA叢集(涉及mysql安裝主從同步同步複製...)

MHA(Master High Availablity)-資料庫高可用叢集 由日本DeNA公司開發的一套實現mysql高可用的解決方案,可以保障資料庫自動故障切換操作在0~30s之內,可以確保切換過程中資料的一致,實現真正意義上的高可用。也就是在A主機上訪問資料,中途伺服器故障,客戶可以在繼續在

MySQL5.7安裝+基於GTID主從複製+並行複製+增強同步複製+讀寫分離+M-S-S架構(聯級複製

實驗環境: Centos7.2 角色 主機IP server_id 資料狀態 Proxysql 192.168.148.62 nul

mysql 5.7 基於GTID 主從同步的1236故障處理(其它事務故障等同)

其它 top 處理 set tid gtid stop eve 1-1 登錄從庫 stop slave; 查看執行事務 show slave status\G Retrieved_Gtid_Set: ee3bdb44-f6a1-11e7-b194-005056a35fd4

配置MYSQL基於GTID 主從復制詳細解析及步驟

spec sys tran allow ... ext mat mar 安裝 GTID的概念 全局事務標識:global transaction identifiers GTID是一個事務一一對應,並且全局唯一ID GTID在一個服務器上只執行一次,避免重復執行導致數據混

MySQL主從複製同步複製原理及搭建

在MySQL5.5之前的版本中,MySQL的複製是非同步複製,主庫和從庫的資料之間存在一定的延遲,比如網路故障等各種原因,這樣子容易存在隱患就是:當在主庫寫入一個事務成功後並提交了,但是由於從庫延遲沒有及時得到主庫推送的Binlog日誌時,主庫突然宕機了,那麼此時從庫就可能損失這個事務,從而造成主從不一致的狀

Mysql主從複製同步複製並行複製

一、主從複製 1.主從複製原理 MySQL之間資料複製的基礎是二進位制日誌檔案(binary log file)。一臺MySQL資料庫一旦啟用二進位制日誌後,其作為master,它的資料庫中所有操作都會以“事件”的方式記錄在二進位制日誌中,其他資料庫作為

MySQL主從復制與GTID主從復制

楓雨1.主從復制1.1原理 主庫開啟binlog功能並授權從庫連接主庫,從庫通過change master得到主庫的相關同步信息,然後連接主庫進行驗證,主庫IO線程根據從庫slave線程的請求,從master.info開始記錄的位置點向下開始取信息,同時把取到的位置點和最新的位置與binlog信息一同發給從

CentOS6.8下MySQL5.6.40基於GTID主從及多線程復制

GTID 復制 mysql大綱 一 GTID簡介 二 環境準備 三 數據庫的安裝 四 基於GTID主從配置步驟 五 驗證GTID復制功能 一 GTID簡介 GTID(Global Transaction ID)是對於一個已提交事務的編號,並且是一個全局唯一的編號。GTID實際上是由UUID+TID組成的。

mysql GTID 同步複製

1)什麼是GTID GTID(Global Transaction ID)是對於一個已提交事務的編號,並且是一個全域性唯一的編號。GTID實際上是由UUID+TID組成的。其中UUID是一個MySQL例項的唯一標 識,儲存在mysql資料目錄下的auto.cnf檔案裡。TID代表了該例項上已經提交的事務數量

配置mysql5.5主從複製同步複製、主主複製

mysql主伺服器 192.168.8.40 mysql從伺服器 192.168.8.41 全新配置過程(主和從資料庫都沒有資料):    主從複製主伺服器設定:      1.改server-id      2.啟用二進位制日誌      # mkdir /data/b