您好,登錄后才能下訂單哦!
1.什么是GTID?
GTID(Global Transaction ID)是對于一個已提交事務的編號,并且是一個全局唯一的編號;
GTID實際上是由UUID+TID組成的。其中UUID是一個MySQL實例的唯一標識。TID代表了該實例上已經提交的事務數量,并且隨著事務提交單調遞增;
#查看本數據庫實例的uuid號:
root@localhost [(none)]>select @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 83373570-fe03-11e6-bb0a-000c29c1b8a9 |
+--------------------------------------+
#也可以通過系統層面查看uudi:
[root@Darren2 data]# cat /data/mysql/mysql3306/data/auto.cnf
[auto]
server-uuid=83373570-fe03-11e6-bb0a-000c29c1b8a9
#linux中可以通過uuidgen產生隨機uuid,mysql中可以通過select uuid()產生;
如:
[root@Darren2 ~]# uuidgen
eceac2d7-4878-429b-81ca-e6aea02b1739
root@localhost [(none)]>select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| bc959381-1c89-11e7-8786-000c29c1b8a9 |
+--------------------------------------+
GTID的限制
(1)不支持非事物引擎
(2)不支持create table ... select語句復制(主庫直接報錯)
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
(3)不支持一個sql同時更新一個事物引擎和非事物引擎的表;
(4)在一個復制組中,必須要求統一開啟GTID或是關閉GTID;
(5)開啟GTID需要重啟(5.7支持在線切換);
(6)開啟GTID后就不再使用原來的傳統的復制方式;
(7)對于create temporary table和drop temporary talbe語句不支持(不報錯,但是也不存在表);
(8)不支持sql_slave_skip_counter;
2.環境配置
master | slave | |
數據庫版本 | 5.7.16 | 5.7.16 |
IP | 192.168.91.18 | 192.168.91.20 |
serverid | 330618 | 330620 |
端口號 | 3306 | 3306 |
3.配置文件參數設置
(1)master:
配置文件中設置:
server-id = 330618
binlog_format = row
log-bin = /data/mysql3306/logs/mysql-bin
#GTID
gtid_mode=on
enforce-gtid-consistency=on
(2)slave:
配置文件中設置:
server-id = 330620
binlog_format = row
relay-log=relay-bin
relay-log-index=relay-bin.index
read_only = on
#復制進程就不會隨著數據庫的啟動而啟動
skip_slave_start=1
#如果這個從庫還有從庫,需要開啟這個參數
log_slave_updates=0
#GTID
gtid_mode=on
enforce-gtid-consistency=on
4.主庫創建用戶
master:
創建rep用戶:
create user rep@'192.168.91.%' identified by '147258';
grant replication slave on *.* to rep@'192.168.91.%';
flush privileges;
5.備份還原初始化
(1)主庫備份數據庫:
mysqldump -uroot -p147258 --single-transaction --master-data=2 -A > /tmp/master.sql
scp master.sql root@192.168.91.20:/tmp/
(2)把備份文件maser.sql,還原到slave:
mysql -uroot -p147258 < master.sql
#注意:
備份文件中有這么一條命令:
SET @@GLOBAL.GTID_PURGED='83373570-fe03-11e6-bb0a-000c29c1b8a9:1-10908';
在還原的時候使用,表示從庫還原之后GTID就會達到10908,在10908之前的事物不需要同步過來,從10909開始同步事物;
6.從庫 master to
(1)添加主庫信息到從庫slave:
change master to
master_host='192.168.91.18',
master_port=3306,
master_user='rep',
master_password='147258',
master_auto_position=1;
(2)啟動從庫
root@localhost [(none)]>start slave;
(3)查看主庫信息
root@localhost [testdb]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 1120 | | | f4b6894e-c7fd-11e6-aaf8-000c29aacb77:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
(4)查看從庫信息
root@localhost [(none)]>show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 330621 | | 3306 | 330618 | 5af344c7-c861-11e6-ad80-000c290f28e2 |
| 330620 | | 3306 | 330618 | 31ba9bcb-c861-11e6-ad7f-000c29cc71ad |
+-----------+------+------+-----------+--------------------------------------+
(5)查看復制狀態
slave:
root@localhost [testdb]>show slave status\G
7.測試
master:
root@localhost [(none)]>use testdb;
root@localhost [testdb]>create table t1(id int,name char(10));
root@localhost [testdb]>insert into t1 values(1,'aaa'),(2,'bbb');
slave:
root@localhost [testdb]>select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | bbb |
+------+------+
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。