目录
1.GTID介绍
1.1GTID是什么
1.2 GTID Replication介绍
1.3 GTID的组成
1.4GTID的作用
1.5为什么要用GTID
1.6GTID比传统复制的优势
1.7GTID复制实现的工作原理
2 主从复制
2.1 一主一从
2.1.1部署数据库可查看 2.1.2 授权一个用户,用于主从复制主从>
2.1.3 修改master主配文件
2.1.4 slave主配文件
2.1.5 配置slave主机,授权从数据库
2.1.6 验证主从复制
2.2 一主多从
2.2.1 第二台从数据库slave主配文件
2.2.2 配置第二台slave主机
2.3 多主一从
2.3.1 两台主数据库配置和上述主数据库相同,从数据库配置也相同2.3.2 查看主库信息
2.3.3 配置slave主机
2.3.4 在从库上配置主库信息
2.3.5 验证测试
1.GTID介绍
1.1GTID是什么
GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号。GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识,保存在mysql数据目录下的f文件里。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。下面是一个GTID的具体形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23。
全称为: Global Transaction ID ,可简化MySQL(5.6版本之后)的主从切换以及Failover。GTID是一个字符串类似 `3E11FA47-71CA-11E1-9E33-C80AA9429562:23` 由UUID+TID组成,UUID代表MySQL实例,TID代表这个实例提交的事务数量。
1.2 GTID Replication介绍
从MySQL5.6开始增加了强大的GTID(Global Transaction ID,全局事务ID)这个特性,用来强化数据库的主备一致性, 故障恢复, 以及容错能力。用于取代过去传统的主从复制(即:基于binlog和position的异步复制)。借助GTID,在发生主备切换的情况下,MySQL的其他slave可以自动在新主上找到正确的复制位置,这大大简化了复杂复制拓扑下集群的维护,也减少了人为设置复制position发生误操作的风险。另外,基于GTID的复制可以忽略已经执行过的事务,减少了数据发生不一致的风险。
1.3 GTID的组成
GTID是由server_uuid和事务id组成的,即GTID=server_uuid:transaction_id。
server_uuid,是在MySQL第一次启动时自动生成并持久化到f文件(存放在数据目录下,每台机器的server_uuid都不一样。
transaction_id,是一个从1开始的自增计数,表示在这个主库上执行的第n个事务。MySQL会保证事务与GTID之间的1:1映射,如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1
表示在以6ba9a76d-606b-11ea-b3ce-000c29cb3421为唯一标识的MySQL实例上执行的第1个数据库事务。
一组连续的事务可以用 “-” 连接的事务序号范围表示。例如:6ba9a76d-606b-11ea-b3ce-000c29cb3421:1-15
1.4GTID的作用
根据GTID可以知道事务最初是在哪个实例上提交的
GTID的存在方便了Replication的Failover
1.5为什么要用GTID
在主从复制中,尤其是半同步复制中, 由于Master 的dump进程一边要发送binlog给Slave,一边要等待Slave的ACK消息,这个过程是串行的,即前一个事物的ACK没有收到消息,那么后一个事物只能排队候着; 这样将会极大地影响性能;有了GTID后,SLAVE就直接可以通过数据流获得GTID信息,而且可以同步
主从故障切换中,如果一台MASTER down,需要提取拥有最新日志的SLAVE做MASTER,这个是很好判断,而有了GTID,就只要以GTID为准即可方便判断;而有了GTID后,SLAVE就不需要一直保存这bin-log 的文件名和Position了;只要启用MASTER_AUTO_POSITION 即可
当MASTER crash的时候,GTID有助于保证数据一致性,因为每个事物都对应唯一GTID,如果在恢复的时候某事物被重复提交,SLAVE会直接忽略
1.6GTID比传统复制的优势
更简单的实现failover,不用以前那样在需要找log_file和log_Pos
更简单的搭建主从复制
比传统复制更加安全
GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过
1.7GTID复制实现的工作原理
主节点更新数据时,会在事务前产生GTID,一起记录到binlog日志中
从节点的I/O线程将变更的bin log,写入到本地的relay log中
SQL线程从relay log中获取GTID,然后对比本地binlog是否有记录(所以MySQL从节点必须要开启binary log)
如果有记录,说明该GTID的事务已经执行,从节点会忽略
如果没有记录,从节点就会从relay log中执行该GTID的事务,并记录到bin log
在解析过程中会判断是否有主键,如果没有就用二级索引,如果有就用全部扫描
2 主从复制
2.1 一主一从
2.1.1部署数据库可查看 <MYSQL主从>
2.1.2 授权一个用户,用于主从复制
mysql> show variables like '%gtid%';+----------------------------------+-----------+| Variable_name| Value|+----------------------------------+-----------+| binlog_gtid_simple_recovery| ON || enforce_gtid_consistency | OFF || gtid_executed_compression_period | 1000|| gtid_mode | OFF || gtid_next | AUTOMATIC || gtid_owned | || gtid_purged | || session_track_gtids | OFF |+----------------------------------+-----------+8 rows in set (0.01 sec)mysql> create user 'zj'@'192.168.25.142' identified by "1";Query OK, 0 rows affected (0.01 sec)mysql> grant replication slave on *.* to 'zj'@'192.168.25.142' ;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
2.1.3 修改master主配文件
[root@localhost ~]# vim /etc/f[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306pid-file = /opt/data/mysql.pidskip-name-resolvelog-bin = mysql-bin #开启二进制日志 server-id = 1gtid-mode = on #开启gtid模式enforce-gtid-consistency = on #强制gtid一致性,开启后对特定的create table不被支持binlog-format = row #默认为mixed混合模式,更改成row复制,为了数据一致性log-slave-updates = 1 #从库binlog才会记录主库同步的操作日志skip-slave-start = 1 #跳过slave复制线程// 重启数据库服务[root@localhost ~]# service mysqld restartShutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! // 查看master数据库状态mysql> show master status;+------------------+----------+--------------+------------------+------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+------------------------------------------+| mysql-bin.000014 |1951 | | | b736875b-097e-11ec-b557-000c29810dc2:1-9 |+------------------+----------+--------------+------------------+------------------------------------------+1 row in set (0.00 sec)
2.1.4 slave主配文件
[root@localhost ~]# vim /etc/f[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306 pid-file = /opt/data/mysql.pidskip-name-resolve server-id = 2 log-bin = mysql-bin binlog-format = row skip-slave-start = 1log-slave-updates = 1gtid-mode = on enforce-gtid-consistency = on [root@localhost ~]# service mysqld restartShutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
2.1.5 配置slave主机,授权从数据库
// 检查gtid模式状态mysql> show variables like '%gtid%';+----------------------------------+-----------+| Variable_name| Value|+----------------------------------+-----------+| binlog_gtid_simple_recovery| ON || enforce_gtid_consistency | ON || gtid_executed_compression_period | 1000|| gtid_mode | ON || gtid_next | AUTOMATIC || gtid_owned | || gtid_purged | || session_track_gtids | OFF |+----------------------------------+-----------+mysql> reset slave;Query OK, 0 rows affected (0.01 sec)mysql> change master to-> master_host='192.168.25.140' ,-> master_user='zj' ,-> master_password='1' ,-> 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> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.25.140Master_User: zjMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000014Read_Master_Log_Pos: 154Relay_Log_File: localhost-relay-bin.000003Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000014Slave_IO_Running: YesSlave_SQL_Running: Yes
2.1.6 验证主从复制
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || JJ || mysql || performance_schema || sys|+--------------------+5 rows in set (0.00 sec)mysql> show master status;+------------------+----------+--------------+------------------+-------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set|+------------------+----------+--------------+------------------+-------------------------------------------+| mysql_bin.000006 |1389 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |+------------------+----------+--------------+------------------+-------------------------------------------+1 row in set (0.00 sec)
2.2 一主多从
2.2.1 第二台从数据库slave主配文件
[root@localhost ~]# vim /etc/f[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306 pid-file = /opt/data/mysql.pidskip-name-resolve server-id = 3 //第三台从服务器id 不能与前面使用过的一样 log-bin = mysql-bin binlog-format = row skip-slave-start = 1log-slave-updates = 1gtid-mode = on enforce-gtid-consistency = on [root@localhost ~]# service mysqld restartShutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS
2.2.2 配置第二台slave主机
mysql> show variables like '%gtid%';+----------------------------------+-----------+| Variable_name| Value|+----------------------------------+-----------+| binlog_gtid_simple_recovery| ON || enforce_gtid_consistency | ON || gtid_executed_compression_period | 1000|| gtid_mode | ON || gtid_next | AUTOMATIC || gtid_owned | || gtid_purged | || session_track_gtids | OFF |+----------------------------------+-----------+mysql> reset slave;Query OK, 0 rows affected (0.01 sec)mysql> change master to-> master_host='192.168.25.140' ,-> master_user='zj' ,-> master_password='1' ,-> 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> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.25.140Master_User: zjMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000014Read_Master_Log_Pos: 1776Relay_Log_File: localhost-relay-bin.000004Relay_Log_Pos: 454Relay_Master_Log_File: mysql-bin.000014Slave_IO_Running: YesSlave_SQL_Running: Yesmysql> show databases;+--------------------+| Database |+--------------------+| information_schema || JJ || mysql || performance_schema || sys|+--------------------+5 rows in set (0.00 sec)mysql> show master status;+------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set|+------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+| mysql_bin.000008 |750 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10,ba11a7ff-097f-11ec-9272-000c29959565:1,ddd6f885-0a26-11ec-81cc-000c29f2278e:1 |+------------------+----------+--------------+------------------+---------------------------------------------------------------------------------------------------------------------------+
2.3 多主一从
2.3.1 两台主数据库配置和上述主数据库相同,从数据库配置也相同
2.3.2 查看主库信息
//主库1mysql> show master status;+------------------+----------+--------------+------------------+-------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set|+------------------+----------+--------------+------------------+-------------------------------------------+| mysql-bin.000014 |2128 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |+------------------+----------+--------------+------------------+-------------------------------------------+1 row in set (0.00 sec)主库2mysql> show master status;+------------------+----------+--------------+------------------+-------------------------------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set|+------------------+----------+--------------+------------------+-------------------------------------------+| mysql-bin.000002 |154 | | | b736875b-097e-11ec-b557-000c29810dc2:1-10 |+------------------+----------+--------------+------------------+-------------------------------------------+1 row in set (0.00 sec)
2.3.3 配置slave主机
//检查gtid模式状态mysql> show variables like '%gtid%';+----------------------------------+-----------+| Variable_name| Value|+----------------------------------+-----------+| binlog_gtid_simple_recovery| ON || enforce_gtid_consistency | ON || gtid_executed_compression_period | 1000|| gtid_mode | ON || gtid_next | AUTOMATIC || gtid_owned | || gtid_purged | || session_track_gtids | OFF |+----------------------------------+-----------+mysql> reset slave;Query OK, 0 rows affected (0.01 sec)
2.3.4 在从库上配置主库信息
//配置主库1//下面两条命令不执行,后续会报错mysql> set global master_info_repository='table';Query OK, 0 rows affected (0.00 sec)mysql> set global relay_log_info_repository='table';Query OK, 0 rows affected (0.00 sec)mysql> change master to-> master_host='192.168.25.140' , //这里是主库IP-> master_user='zj' ,-> master_password='1' ,-> master_log_file='mysql-bin.000014'-> master_log_pos=2128-> for channel 'master-1'; //创建隧道配置主库2mysql> change master to-> master_host='192.168.25.142' ,-> master_user='zj' ,-> master_password='1' ,-> master_log_file='mysql-bin.000002'-> master_log_pos=154-> for channel 'master-2'; //创建隧道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 eventMaster_Host: 192.168.25.140Master_User: zjMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000014Read_Master_Log_Pos: 2128Relay_Log_File: localhost-relay-bin-master@002d1.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000014Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 2128Relay_Log_Space: 544Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: b736875b-097e-11ec-b557-000c29810dc2Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: master-1Master_TLS_Version: *************************** 2. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.25.142Master_User: zjMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 154Relay_Log_File: localhost-relay-bin-master@002d2.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000002Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 544Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2Master_UUID: ba11a7ff-097f-11ec-9272-000c29959565Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: baf6e1ae-0a32-11ec-bf3d-000c29f2278e:1Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: master-2Master_TLS_Version: 2 rows in set (0.00 sec)
2.3.5 验证测试
主库1上创建数据库llmysql> create database ll;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || ll || mysql || performance_schema || sys|+--------------------+5 rows in set (0.00 sec)主库2上创建数据库mmmysql> create database mm;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys|| mm |+--------------------+5 rows in set (0.00 sec)// 从库上开启从服务后查看mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || ll || mysql || performance_schema || sys|| mm |+--------------------+6 rows in set (0.00 sec)