1.什么是GTID?
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比延平網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式延平網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋延平地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
GTID(Global Transaction ID)是對于一個已提交事務(wù)的編號,并且是一個全局唯一的編號;
GTID實際上是由UUID+TID組成的。其中UUID是一個MySQL實例的唯一標(biāo)識。TID代表了該實例上已經(jīng)提交的事務(wù)數(shù)量,并且隨著事務(wù)提交單調(diào)遞增;
#查看本數(shù)據(jù)庫實例的uuid號:
root@localhost [(none)]>select @@server_uuid;
+--------------------------------------+
| @@server_uuid |
+--------------------------------------+
| 83373570-fe03-11e6-bb0a-000c29c1b8a9 |
+--------------------------------------+
#也可以通過系統(tǒng)層面查看uudi:
[root@Darren2 data]# cat /data/mysql/mysql3306/data/auto.cnf
[auto]
server-uuid=83373570-fe03-11e6-bb0a-000c29c1b8a9
#linux中可以通過uuidgen產(chǎn)生隨機uuid,mysql中可以通過select uuid()產(chǎn)生;
如:
[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語句復(fù)制(主庫直接報錯)
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
(3)不支持一個sql同時更新一個事物引擎和非事物引擎的表;
(4)在一個復(fù)制組中,必須要求統(tǒng)一開啟GTID或是關(guān)閉GTID;
(5)開啟GTID需要重啟(5.7支持在線切換);
(6)開啟GTID后就不再使用原來的傳統(tǒng)的復(fù)制方式;
(7)對于create temporary table和drop temporary talbe語句不支持(不報錯,但是也不存在表);
(8)不支持sql_slave_skip_counter;
2.環(huán)境配置
master | slave | |
數(shù)據(jù)庫版本 | 5.7.16 | 5.7.16 |
IP | 192.168.91.18 | 192.168.91.20 |
serverid | 330618 | 330620 |
端口號 | 3306 | 3306 |
3.配置文件參數(shù)設(shè)置
(1)master:
配置文件中設(shè)置:
server-id = 330618
binlog_format = row
log-bin = /data/mysql3306/logs/mysql-bin
#GTID
gtid_mode=on
enforce-gtid-consistency=on
(2)slave:
配置文件中設(shè)置:
server-id = 330620
binlog_format = row
relay-log=relay-bin
relay-log-index=relay-bin.index
read_only = on
#復(fù)制進程就不會隨著數(shù)據(jù)庫的啟動而啟動
skip_slave_start=1
#如果這個從庫還有從庫,需要開啟這個參數(shù)
log_slave_updates=0
#GTID
gtid_mode=on
enforce-gtid-consistency=on
4.主庫創(chuàng)建用戶
master:
創(chuàng)建rep用戶:
create user rep@'192.168.91.%' identified by '147258';
grant replication slave on *.* to rep@'192.168.91.%';
flush privileges;
5.備份還原初始化
(1)主庫備份數(shù)據(jù)庫:
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)查看復(fù)制狀態(tài)
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 |
+------+------+