MySQL-5.7.28二进制主从复制(基于GTID)

GTID介绍

  • 传统的基于binlog position复制的方式有个严重的缺点:如果slave连接master时指定的binlog文件错误或者position错误,会造成遗漏或重复,很多时候前后数据是有依赖性的,这样就会出错而导致数据不一致。
  • 从MySQL5.6开始,MySQL开始支持GTID复制。GTID全称是global transaction id,表示的是全局事务ID。GTID的分配方式为uuid:transaction_id,其中:uuid是每个MySQL服务器唯一的,记录在$datadir/auto.cnf中。如果复制结构中,任意两台服务器uuid重复的话(比如直接冷备份时,auto.cnf中的内容是一致的),在启动复制功能的时候会报错。这时就可以删除auto.cnf文件,再重启mysqld。

基于GTID主从复制优点

  • 保证同一事物在某个slave上绝对只执行一次,没有执行过的GTID事物总是会被执行。
  • 不用像传统复制那样保证binlog的坐标准确,因为根本不需要binlog以及坐标。
  • 故障转移到新的master的时候很方便,简化了很多任务。
  • 很容易判断master和slave的是否一致。只要master上提交的事物在slave上也提交了,那么一定是一致的。
  • MySQL提供了选项可以控制跳过某些GTID事物,防止slave第一次启动复制执行master上的所有事物而导致耗时过久。
  • 虽然对于row-based和statement-based的格式都能进行GTID复制,但建议采用row-based格式。

GTID工作原理

  • master更新数据时,会在事物前产生GTID,一同记录到binlog日志中。

  • slave的I/O线程将变更的binlog,写入到本地的relay log中。

  • sql线程从relay log中获取GTID,然后对比slave端的binlog是否有记录。

  • 如果有记录,说明该GTID的事物已经执行,slave会忽略。

  • 如果没有记录,slave就会从relay log中执行该GTID的事物,并记录到binlog。

  • 在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。

  • MySQL第一次启动的时候创建auto.cnf文件,并生成server_uuid(MySQL使用机器网卡,当前时间,随机数等拼接成一个128bit的uuid)。之后MySQL再启动时不会重复生成uuid,而是使用auto.cnf中的uuid

  • 同一个server_uuid下的transaction_id一般是递增的。如果一个事务是通过用户线程执行,那么MySQL在生成的GTID时,会使用它自己的server_uuid,然后再递增一个transaction_id作为该事务的GTID。如果事务是通过SQL线程回放relay log时产生,那么GTID就直接使用binlog里的了。在MySQL5.6中不用担心binlog里没有GTID,因为如果从库开启了GTID模式,主库也必须开启,否则IO线程在建立连接的时候就中断了。

  • server_uuid全局唯一,具备幂等性

GTID找点儿原理

  • 在未开启GTID模式的情况下,从库用(File_name和File_pos)二元组标识执行到的位置。start slave时,从库会先向主库发送一个binlog_dump命令,在binlog_dumo命令中指定File_name和File_pos,主库就从这位置开始发送binlog。
  • 在开启GTID模式的情况下,如果指定master_auto_position=1。start slave时,从库会计算Retrieved_Gtid_Set和Executed_Gtid_Set的并集(通过show slave status可以查看),然后把这个GTID并集发送给主库。主库会使用从库请求的GTID集合和自己的gtid_executed比较,把从库GTID集合里确实的事务全都发送给从库。如果从库缺失的GTID,已经被主库pruge了,那么从库报1236错误,IO线程中断。
    在这里插入图片描述

POS与GTID有什么区别?

  • 两者都是日志文件里事件的一个标志,如果将整个MySQL集群看做一个整体,pos就是局部的,GTID就是全局的。
  • 一主两从,在master,slave1,slave2日志文件里的pos,都各不相同,就是一个event,在master的日志里,pos可能是700,而在slave1,slave2里,pos可能就是300,400,因为众多slave也可能不是同事加入集群的,不是从同一个位置进行同步。
  • 而GTID,在master,slave1,slave2各自的日志文件里,同一个event的GTID值都是一样的。

主从复制GTID模式实战

前文有二进制多实例安装的演示参考前文再做以下实验!
请点击:二进制多实例安装

[root@localhost ~]# mkdir -p /usr/local/mysql/binlog

环境条件
MySQL-3306实例为主节点
MySQL-3309实例为从节点

开启binlog日志与GTID

[root@localhost ~]# vi /etc/my.cnf 
[mysqld]
log_bin=/usr/local/mysql/binlog/mysql-bin
binlog_format=row
sync_binlog=1
gtid_mode=on
enforce_gtid_consistency
[root@localhost ~]# cat >> /usr/local/mysql-3309/my.cnf << EOF
> gtid_mode=on
> enforce_gtid_consistency
> EOF

授权

[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql*

重启3306与3309实例

[root@localhost ~]# systemctl restart mysqld.service
[root@localhost ~]# systemctl restart mysqld-3309.service 

查看是否开启GTID

[root@localhost ~]# mysql -uroot -p000000 -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
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | ON        |   <----是否开启GTID模块
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
[root@localhost ~]# mysql -uroot -p000000 -S /usr/local/mysql-3309/mysql.sock -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@localhost ~]# mysql -uroot -p000000 -e "grant replication slave on *.* to giao@'%' identified by '000000';"

开启GTID主从复制

#进入3309实例开启GTID连接到3306实例
[root@localhost ~]# mysql -uroot -p000000 -S /usr/local/mysql-3309/mysql.sock
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 3
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> change master to master_host='192.168.10.143',master_user='giao',master_password='000000',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.07 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

查看主从复制状态

[root@localhost ~]# mysql -uroot -p000000 -S /usr/local/mysql-3309/mysql.sock -e "show slave status\G;" | egrep 'Slave_IO_Running:|Slave_SQL_Running:|Seconds_Behind_Master:'
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        Seconds_Behind_Master: 0

测试GTID模式效果
可以看到当前两个实例都没有giao库,接下来在3306实例创建giao库,然后查看3309库是否有同步。

[root@localhost ~]# mysql -uroot -p000000 
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 5
Server version: 5.7.28-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.07 sec)
[root@localhost ~]# mysql -uroot -p000000 -S /usr/local/mysql-3309/mysql.sock 
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 8
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.06 sec)

可以看到3309实例从库也同步创建了giao库

#主库先创建giao库
[root@localhost ~]# mysql -uroot -p000000 -e "create database giao;"
[root@localhost ~]# mysql -uroot -p000000 -e "show databases;"      
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| giao               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
#观察从库有没有同步giao库
[root@localhost ~]# mysql -uroot -p000000 -S /usr/local/mysql-3309/mysql.sock -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| giao               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想敲代码的运维

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值