Gtid基本概念:
传统的基于binlog position复制的方式有个严重的缺点:如果slave连接master时指定的binlog文件错误或者position错误,会造成遗漏或者重复,很多时候前后数据是有依赖性的,这样就会出错而导致数据不一致。
从MYSQL5.6开始,mysql开始支持GTID复制。GTID的全称是global transaction id,表示的是全局事务ID。GTID的分配方式为uuid:trans_id,其中:
uuid是每个mysql服务器都唯一的,记录在$datadir/auto.cnf中。如果复制结构中,任意两台服务器uuid重复的话(比如直接冷备份时,auto.conf中的内容是一致的),在启动复制功能的时候会报错。这时可以删除auto.conf文件再重启mysqld。
Gtid的作用:
Gtid,采用了新的复制协议,旧协议是,首先从服务器上在一个特定的偏移量位置连接到主服务器上一个给定的二进制日志文件,然后主服务器再从给定的连接点开始发送所有的事件。
新协议有所不同,支持以全局统一事务ID(GTID)为基础的复制。当在主库上提交事务或者被从库应用时,可以定位和追踪每一个事务。GTID复制是全部以事务为基础,使得检查主从一致性变得非常简单。如果所有主库上提交的事务也同样提交到从库上,一致性就得到了保证。
Gtid复制的好处:
1.保证同一个事务在某slave上绝对只执行一次,没有执行过的gtid事务总是会被执行。
2.不用像传统复制那样保证binlog的坐标准确,因为根本不需要binlog以及坐标。
3.故障转移到新的master的时候很方便,简化了很多任务。
4.很容易判断master和slave的数据是否一致。只要master上提交的事务在slave上也提交了,那么一定是一致的。
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到自身的b在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行。
6.在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
开始部署主从
准备两台虚拟机
192.168.27.137 主
192.168.27.138 从
1.关掉防火墙
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0
2.安装启动mysql5.7版本
[root@localhost ~]# wget -i -c https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm [root@localhost ~]# yum -y install mysql-community-server [root@localhost ~]# systemctl start mysqld
5.7mysql首次登录需要修改密码,先获取密码 grep password /var/log/mysqld.log 然后登录
[root@localhost ~]# grep "password" /var/log/mysqld.log 2020-11-10T05:10:23.949222Z 1 [Note] A temporary password is generated for root@localhost: Dod;Zt%Lp6kl
获取到mysql初始密码,登录mysql并修改密码 mysql -uroot -p
输入获取到的密码
进入数据库后,执行修改密码语句:
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.7.32 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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> alter user 'root'@'localhost' identified by '新密码'; 新密码需要符合5.7的密码复杂度要求,弱密码用不了。 修改完密码后,必须执行刷新策略。 mysql>flush privileges; mysql>exit
3.主上的配置
[root@localhost ~]# vim /etc/my.cnf server-id=1 ##服务器标识 binlog_format=row log-bin=mysql-bin ##开启二进制日志 gtid_mode=ON ## 开启gtid模式 enforce-gtid-consistency=true ## 强制gtid复制 [root@localhost ~]# systemctl restart mysqld [root@localhost ~]# mysql -uroot -p Enter password: mysql> grant all on *.* to 'tom'@'%' identified by 'H9VtyDlad!T'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4.从上的配置
[root@localhost ~]# vim /etc/my.cnf server-id=2 ##服务器标识 binlog_format=row gtid_mode=ON ## 开启gtid模式 enforce-gtid-consistency=true ## 强制gtid复制 重启 [root@localhost ~]# systemctl restart mysqld [root@localhost ~]# mysql -uroot -p Enter password: mysql> change master to -> master_host='192.168.27.137', -> master_user='tom', -> master_password='H9VtyDlad!T', -> 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 statusG; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.27.137 Master_User: tom Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 585 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 798 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 585 Relay_Log_Space: 1009 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 0979161c-2313-11eb-b4d2-000c295cd742 Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 0979161c-2313-11eb-b4d2-000c295cd742:1-2 Executed_Gtid_Set: 0979161c-2313-11eb-b4d2-000c295cd742:1-2 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
主从搭建成功
需要了解更多数据库技术:Mysql5.7安装 Gtid原理作用+主从复制,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/813359.html