您现在的位置是:网站首页> 编程资料编程资料

MySQL示例DTID主从原理解析_Mysql_

2023-05-26 372人已围观

简介 MySQL示例DTID主从原理解析_Mysql_

1.GTID基本概念

MySQL 5.6.5开始支持的,全局事务标识符(GTID(Global Transaction ID))是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。
此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。
所有交易和所有GTID之间都有一对一的映射关系 。
它由服务器ID以及事务ID组合而成。
这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。
正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。
一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。

2.GTID优点

保证同一个事务在某slave上绝对只执行一次,没有执行过的gtid事务总是会被执行。
不用像传统复制那样保证binlog的坐标准确,因为根本不需要binlog以及坐标。
故障转移到新的master的时候很方便,简化了很多任务。
很容易判断master和slave的数据是否一致。只要master上提交的事务在slave上也提交了,那么一定是一致的。

3.GTID的工作原理

在这里插入图片描述

1.当一个事务在主库端执行并提交时,产生GTID,一同记录到binlog日志中。
2.binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值。
3、sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID。
4、如果有记录,说明该GTID的事务已经执行,slave会忽略。
5、如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。
6、在解析过程中会判断是否有主键,如果有就用二级索引,如果没有就用全部扫描。

4.GTID比传统复制的优势

1.更简单的实现故障转移(failover),不需要找log_file,log_pos

2.更简单的搭建主从复制

3.更加安全

4.GTID是连续没有空洞的,因此主数据库发生冲突时,可以添加空事件的方式进行跳过

5.启动的方法

  • 方法一:如果是新搭建的服务器,直接启动即可
  • 方法二:如果是以及跑的服务器,需要重启一下mysql server

启动前,先关闭master的写入,保证master端和slave端数据保持同步,所有slave需要加上skip_slave_start=1的配置参数,避免启动后还是使用之前的复制协议

6.GTID(一主一从)配置

6.1环境:

centos8.0 ip:192.168.136.239 有数据 hostname:mysql01

centos8.0 ip:192.168.136.219 无数据 hostname:mysql02

#二进制安装以及mysql自启动服务略

6.2在主库上给从库授权:

 mysql> grant replication slave on *.* to 'slave'@'192.168.136.219' identified by 'slave'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #俩服务器均关闭防火墙 [root@mysql01 ~]# systemctl stop firewalld [root@mysql01 ~]# setenforce 0 [root@mysql02 ~]# systemctl stop firewalld [root@mysql02 ~]# setenforce 0 从库测试连接: [root@mysql02 ~]# mysql -u slave -p'slave' -h192.168.136.239 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

6.3确保数据一致操作

 1.对主库进行锁表 mysql> flush tables with read lock; 2.对主库进行全备 [root@mysql01 ~]# mysqldump -uroot -A > /clq/all-databases-20210519.sql 3.拷贝到从库主机上去 [root@mysql01 ~]# scp /clq/all-databases-20210519.sql root@192.168.136.219:/backup/ [root@mysql02 backup]# ll -rw-r--r--. 1 root root 873527 5月 19 16:40 all-databases-20210519.sql 4.从库上进行主库的恢复 [root@mysql02 backup]# mysql -uroot -pHuawei0917@ < all-databases-20210519.sql 

6.4配置主库

 [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables log-bin = master_bin #开启主库日志 server-id = 10 #服务唯一标识id gtid-mode = on #GTID模式开启 enforce_gtid_consistency = on #强制gtid模式一致性 log-slave-updates = 1 #从库允许更新日志,同步操作日志 binlog_format = row #binlog日志格式为行格式, 默认是mixed混合模式 skip_slave_start = 1 #跳过从库开启,以主库开始开启 #重启 systemctl restart mysqld 

6.5配置从库

 [root@mysql02 data]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables gtid_mode=on enforce_gtid_consistency=on server-id=20 log-bin=slave_binlog #开启从库日志 log_slave-updates=1 #从库允许更新 binlog_format=row #格式为行 skip-slave_start=1 #重启 systemctl restart mysqld 

查看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.00 sec) 

6.6配置主从复制

 #从库上root登录配置 #help change master to 可以查看帮助文档实例 mysql> change master to -> master_host='192.168.136.239', -> master_user='slave', -> master_password='slave', -> master_port=3306, #主库端口 -> master_auto_position=1; #位置 #master_use_gtid = current_pos 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; Slave_IO_Running: Connecting Slave_IO_Running: Yes Slave_SQL_Running: Yes 保证系统一致性 授权一致性 

(一主一从GTID)测试

主库创建一个数据库test,进行测试查看

从库创建一个数据库test02,进行测试查看

 #主库创建一个test数据库 mysql> create database test; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ #从库上查看同步情况 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 6 rows in set (0.00 sec) #从库创建test02库 mysql> create database test02; Query OK, 1 row affected (0.00 sec) #主库上查看 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | #是没有test02库的 | test | +--------------------+ 5 rows in set (0.00 sec) 

小结:主库上的数据操作会同步到从库上面去,而从库上的数据操作与主库没联系

7.GTID(一主俩从)

第三台mysql连接的话,相应配置

第3台mysql ,版本:centos8 ip:192.168.136.230 主机名:mysql03

 [root@mysql03 ~]# cat /etc/my.cnf [mysqld] basedir = /usr/local/mysql datadir = /opt/data socket = /tmp/mysql.sock port = 3306 user = mysql pid-file = /opt/data/mysql.pid skip-name-resolve #skip-grant-tables # replication config log-bin = master_bin server-id = 21 #id必须与之前不同 gtid-mode = on enforce-gtid-consistency = on log-slave-updates = 1 binlog-format = row skip-slave-start = 1 #查看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 | +----------------------------------+-----------+ #由于之前只权限了一个ip,此刻在mysql01主数据库上再授权一个ip mysql> grant replication slave on *.* to 'slave'@'192.168.136.230' identified by 'slave'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #测试连接 [root@mysql ~]# mysql -uslave -pslave -h192.168.136.239 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 17 Server version: 5.7.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> #mysql03从库上root用户连接进行相应配置 [root@mysql03 ~]# mysql -uroot -p1 mysql> change master to -> master_host='192.168.136.239', #主库ip -> master_user='slave', #主库授权的普通用户 -> master_password='slave', -> master_port=3306, #主库端口 -> master_auto_position=1; #位置从1开始同
                
                

-六神源码网