赞
踩
从 MySQL 5.6.5 开始新增了一种基于 GTID 的复制方式。通过 GTID保证了每个在主库上提交的事务在集群中有一个唯一的ID。这种方式强化了数据库的主备一致性,故障恢复以及容错能力。
在原来基于二进制日志的复制中,从库需要告知主库要从哪个偏移量进行增量同步,如果指定错误会造成数据的遗漏,从而造成数据的不一致。借助GTID,在发生主备切换的情况下,MySQL的其它从库可以自动在新主库上找到正确的复制位置,这大大简化了复杂复制拓扑下集群的维护,也减少了人为设置复制位置发生误操作的风险。另外,基于GTID的复制可以忽略已经执行过的事务,减少了数据发生不一致的风险。
GTID (Global Transaction ID) 是对于一个已提交事务的编号,并且是一个全局唯一的编号。 GTID 实际上 是由UUID+TID 组成的。其中 UUID 是一个 MySQL 实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
下面是一个GTID的具体形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23,冒号分割前边为uuid,后边为TID。
GTID 集合可以包含来自多个 MySQL 实例的事务,它们之间用逗号分隔。
如果来自同一MySQL实例的事务序号有多个范围区间,各组范围之间用冒号分隔。例如: e6954592-8dba-11e6-af0e-fa163e1cf111:1-5:11-18,e6954592-8dba-11e6-af0e-fa163e1cf3f2:1-27 可以使用show master status实时查看当前事务执行数.
Gtid采用了新的复制协议,旧协议是,首先从服务器上在一个特定的偏移量位置连接到主服务器上一个给定的二进制日志文件,然后主服务器再从给定的连接点开始发送所有的事件。
新协议有所不同,支持以全局统一事务ID (GTID)为基础的复制。当在主库上提交事务或者被从库应用时,可以定位和追踪每一个事务。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,确保不被重复执行。
⑥在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
安装MySQL的步骤略,详情可以看前面的博客(MySQL进阶)
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.220.8 | RHEL8/ mysql-5.7 | 无数据 |
从数据库 | 192.168.220.9 | RHEL8/ mysql-5.7 | 无数据 |
mysql> grant replication slave on *.* to 'rhel'@'192.168.220.%' identified by 'rhel';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
配置主库文件
[root@master ~]# cat /etc/my.cnf [mysqld] port = 3306 basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock pid-file = /opt/data/mysql.pid skip-name-resolve # replacation slave config log-bin = mysql_bin //开启二进制日志 log-slave-updates = 1 //从库binlog才会记录主库同步的操作日志 skip-slave-start = 1 //跳过slave复制线程 server-id = 10 gtid-mode = on //开启gtid模式 enforce-gtid-consistency = on //强制gtid一致性,开启后对特定的create table不被支持 binlog-format = row //默认为mixed混合模式,更改成row复制,为了数据一致性 //重启服务 [root@master ~]# /usr/local/mysql/support-files/mysql.server restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! //查看gtid模式状态 [root@master ~]# mysql -uroot -predhat -e 'show variables like "%gtid%";' mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------------------+-----------+ | 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 | +----------------------------------+-----------+
配置从库文件
[root@salve src]# cat /etc/my.cnf [mysqld] datadir = /opt/data basedir = /usr/local/mysql pid-file = /opt/data/mysql.pid socket = /tmp/mysql.sock port = 3306 skip-name-resolve # replacation slave config log-bin = mysql_bin log-slave-updates = 1 skip-slave-start = 1 server-id = 20 binlog-format = row gtid-mode = on enforce-gtid-consistency = on //重启服务 [root@salve src]# /usr/local/mysql/support-files/mysql.server restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! //查看gtid模式状态 [root@salve src]# mysql -uroot -predhat -e 'show variables like "%gtid%";' mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------------------+-----------+ | 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> change master to -> master_host='192.168.220.8', -> master_user='rhel', -> master_password='rhel', -> 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 event Master_Host: 192.168.220.8 Master_User: rhel Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000003 Read_Master_Log_Pos: 154 Relay_Log_File: salve-relay-bin.000003 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql_bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes(记得关闭防火墙和selinux)
查看主从库的信息是否同步
//查看主库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) //查看从库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) //主库创建库从库查看是否同步 mysql> create database school; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.00 sec) //从库查看是否同步 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school (同步) | | sys | +--------------------+ 5 rows in set (0.00 sec)
查看主库状态
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set(已执行的Gtid集) |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql_bin.000003 | 632 | | | fbf0cb0d-0976-11ec-aaf4-000c29bb4cb2:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
查看从库状态
mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set(已执行的Gtid集) | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql_bin.000002 | 632 | | | fbf0cb0d-0976-11ec-aaf4-000c29bb4cb2:1-3 | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec) //从库停掉, mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.220.8 Master_User: rhel Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000003 Read_Master_Log_Pos: 632 Relay_Log_File: salve-relay-bin.000003 Relay_Log_Pos: 845 Relay_Master_Log_File: mysql_bin.000003 Slave_IO_Running: No Slave_SQL_Running: No
在主库创建数据后启用从库查看是否同步
mysql> use school Database changed mysql> create table student(id int not null primary key auto_increment,name varchar(100) not null,age tinyint); Query OK, 0 rows affected (0.01 sec) mysql> desc student; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ mysql> insert student(name,age) values('we',23),('df',22),('rer',21),('fd',18); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert student(name,age) values('we',23),('df',22),('rer',21),('fd',18); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert student(name,age) values('we',23),('df',22),('rer',21),('fd',18); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> insert student(name,age) values('we',23),('df',22),('rer',21),('fd',18); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from student; +----+------+------+ | id | name | age | +----+------+------+ | 1 | we | 23 | | 2 | df | 22 | | 3 | rer | 21 | | 4 | fd | 18 | | 5 | we | 23 | | 6 | df | 22 | | 7 | rer | 21 | | 8 | fd | 18 | | 9 | we | 23 | | 10 | df | 22 | | 11 | rer | 21 | | 12 | fd | 18 | | 13 | we | 23 | | 14 | df | 22 | | 15 | rer | 21 | | 16 | fd | 18 | | 17 | we | 23 | | 18 | df | 22 | | 19 | rer | 21 | | 20 | fd | 18 | +----+------+------+ 20 rows in set (0.00 sec) //查看主库状态 mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set(已执行的Gtid集) | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql_bin.000003 | 2368 | | | fbf0cb0d-0976-11ec-aaf4-000c29bb4cb2:1-9(改变) | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec) //查看从库状态 mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set(已执行的Gtid集) | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql_bin.000002 | 632 | | | fbf0cb0d-0976-11ec-aaf4-000c29bb4cb2:1-3(无变化) | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
从库开启查看是否同步
mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> select * from school.student; +----+------+------+ | id | name | age | +----+------+------+ | 1 | we | 23 | | 2 | df | 22 | | 3 | rer | 21 | | 4 | fd | 18 | | 5 | we | 23 | | 6 | df | 22 | | 7 | rer | 21 | | 8 | fd | 18 | | 9 | we | 23 | | 10 | df | 22 | | 11 | rer | 21 | | 12 | fd | 18 | | 13 | we | 23 | | 14 | df | 22 | | 15 | rer | 21 | | 16 | fd | 18 | | 17 | we | 23 | | 18 | df | 22 | | 19 | rer | 21 | | 20 | fd | 18 | +----+------+------+ 20 rows in set (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+------------------------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set(已执行的Gtid集) | +------------------+----------+--------------+------------------+------------------------------------------+ | mysql_bin.000002 | 2313 | | | fbf0cb0d-0976-11ec-aaf4-000c29bb4cb2:1-9(同步) | +------------------+----------+--------------+------------------+------------------------------------------+ 1 row in set (0.00 sec)
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.220.8 | RHEL8/ mysql-5.7 | 无数据 |
从数据库 | 192.168.220.9 | RHEL8/ mysql-5.7 | 无数据 |
从数据库 | 192.168.220.10 | RHEL8/ mysql-5.7 | 无数据 |
安装MySQL步骤省略,详情可以看前面的博客(MySQL进阶)
前面两个主从已配置完成,192.168.220.10从库配置
配置从库文件配置
[root@localhost ~]# cat /etc/my.cnf [mysqld] port = 3306 pid-file = /opt/data/mysql.pid socket = /tmp/mysql.sock datadir = /opt/data basedir = /usr/local/mysql skip-name-resolve # replacation slave config log-bin = mysql_bin server-id = 21 log-slave-updates = 1 skip-slave-start = 1 gtid-mode = on binlog-format = row enforce-gtid-consistency = on //重启服务 [root@localhost ~]# /usr/local/mysql/support-files/mysql.server restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! //查看gtid模式状态 [root@localhost ~]# mysql -uroot -predhat -e 'show variables like "%gtid%";' mysql: [Warning] Using a password on the command line interface can be insecure. +----------------------------------+-----------+ | 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> change master to -> master_host='192.168.220.8', -> master_user='rhel', -> master_password='rhel', -> 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.220.8 Master_User: rhel Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql_bin.000003 Read_Master_Log_Pos: 2368 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql_bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes //查看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 | +----------------------------------+-----------+ 8 rows in set (0.01 sec)
主库创建库查看从库是否同步
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> create database info; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | info | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
从库查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。