基于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全局唯一,具备幂等性
前文有二进制多实例安装的演示参考前文再做以下实验!
请点击:二进制多实例安装
[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 |
+--------------------+