GTID主從與傳統主從複製

GTID主從與傳統主從複製linux

1.主從複製

  • 普通主從複製:

普通主從複製主要是基於二進制日誌文件位置的複製,所以主必須啓動二進制日誌記錄並創建惟一的服務器ID,複製組中的每一個服務器都必須配置惟一的服務器ID。若是您省略server-id(或者明確地將其設置爲其默認值0),則主設備將拒絕來自從設備的任何鏈接。sql

  • GTID 主從:

MySQL 5.6 的新特性之一,全局事務標識符(GTID)是建立的惟一標識符,並與在源(主)服務器上提交的每一個事務相關聯。此標識符不可是惟一的,並且在給定複製設置中的全部服務器上都是惟一的。全部交易和全部GTID之間都有一對一的映射關係 。它由服務器ID以及事務ID組合而成。這個全局事務ID不只僅在原始服務器上惟一,在全部存在主從關係 的mysql服務器上也是惟一的。正是由於這樣一個特性使得mysql的主從複製變得更加簡單,以及數據庫一致性更可靠。一個GTID在一個服務器上只執行一次,避免重複執行致使數據混亂或者主從不一致。數據庫

2.靠什麼同步

主從複製,默認是經過pos複製(postion),就是說在日誌文檔裏,將用戶進行的每一項操做都進行編號(pos),每個event都有一個起始編號,一個終止編號,咱們在配置主從複製時從節點時,要輸入master的log_pos值就是這個緣由,要求它從哪一個pos開始同步數據庫裏的數據,這也是傳統複製技術.vim

MySQL5.6以後增長了GTID複製,GTID就是相似於pos的一個做用,不過它是整個mysql複製架構全局通用的,就是說在這整個mysql冗餘架構中,它們的日誌文件裏事件的GTID值是一致的.服務器

3.pos與GTID的什麼區別

二者都是日誌文件裏事件的一個標誌,若是將整個mysql集羣看做一個總體,pos就是局部的,GTID就是全局的.session

image

上圖就是一個mysql節點的集羣,一主兩從,在master,slave1,slave2日誌文件裏的pos,都各不相同,就是一個event,在master的日誌裏,pos多是700,而在slave1,slave2裏,pos可能就是300,400了,由於衆多slave也可能不是同時加入集羣的,不是從同一個位置進行同步.架構

而GTID,在master,slave1,slave2各自的日誌文件裏,同一個event的GTID值都是同樣的.socket

你們都知道,這整個集羣架構的節點,一般狀況下,是master在工做,其餘兩個結點作備份,並且,各個節點的機器,性能不可能徹底一致,因此,在作備份時,備份的速度就不同,當master忽然crash掉以後,立刻會啓用從節點機器,接管master的工做,當有多個從節點時,選擇備份日誌文件最接近master的那個節點ide

如今就出現狀況了,當salve1變成主節點,那slave2就應該從slave1去獲取日誌文件,進行同步

image

若是使用的是pos,三者的pos不一致,slave2怎麼去獲取它當前要同步的事件在slave1裏的pos呢,很難.
因此就有了GTID,全局的,將全部節點對於同一個event的標記徹底一致,當master crash掉以後,slave2根據同一個GTID直接去讀取slave1的日誌文件,繼續同步.

4.GTID的工做原理

一、當一個事務在主庫端執行並提交時,產生GTID,一同記錄到binlog日誌中。
二、binlog傳輸到slave,並存儲到slave的relaylog後,讀取這個GTID的這個值設置gtid_next變量,即告訴Slave,下一個要執行的GTID值。
三、sql線程從relay log中獲取GTID,而後對比slave端的binlog是否有該GTID。
四、若是有記錄,說明該GTID的事務已經執行,slave會忽略。
五、若是沒有記錄,slave就會執行該GTID事務,並記錄該GTID到自身的binlog,
   在讀取執行事務前會先檢查其餘session持有該GTID,確保不被重複執行。
六、在解析過程當中會判斷是否有主鍵,若是有就用二級索引,若是沒有就用所有掃描。

5.GTID參數配置

master:192.168.112.174

slave:192.168.112.175

服務端須要關閉防火牆和selinux

5.1 在主數據庫裏建立一個同步帳號受權給從數據庫使用

[root@localhost ~]# mysql -uroot -pcwh123!
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 3
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.112.175' identified by 'repl123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

5.2 配置主數據庫

//寫配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
log-bin = mysql-bin   //開啓log-bin日誌
server-id = 1         //定義server-id
gtid-mode = on        //開啓gtid複製
enforce-gtid-consistency = on  //強制gtid一致

//重啓mysql
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//查看master狀態
[root@localhost ~]# mysql -uroot -pcwh123!
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 2
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5.3配置從數據庫

//寫配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
server-id = 6
gtid-mode = on
enforce-gtid-consistency = on
log-bin = mysql-bin

//重啓服務
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

//配置並啓動gtid主從複製
[root@localhost ~]# mysql -uroot -pcwh123!
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 2
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> change master to master_host='192.168.112.174',master_user='repl',master_password='repl123',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

//查看從服務器狀態
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.112.174
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

5.4測試驗證

//在主服務器中加入數據庫cwh
[root@localhost ~]# mysql -uroot -pcwh123!
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 4
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create database cwh;
Query OK, 1 row affected (0.00 sec)

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


//在從服務器上查看是否同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cwh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

//能夠看出來成功了
相關文章
相關標籤/搜索