GTID主从配置

Posted 卑微小胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GTID主从配置相关的知识,希望对你有一定的参考价值。

GTID主从

GTID主从简介

GTID是基于mysql生成的事务ID,由服务器ID和事务ID组成。
这个ID在主库及从库上都是唯一的。
这个特性可以让mysql的主从复制变得更加简单,一致性更加可靠。

GTID优势

  1. 更简单的实现同步,不需要再找log_file和log_pos
  2. GTID是连续的,没有中断,数据一致性高,不会发生丢失
  3. 搭建主从更简单
  4. 比传动主从更安全

GTID劣势

  1. 不支持主从关系中存在非事务数据库
  2. 不支持CREATE TABLE … SELECT语句
  3. 不支持针对临时表的操作:
  4. 开启 GTID 后不能再使用原来的传统的复制方式;

GTID与传统主从的区别

传统主从请参照传统主从

GTID比传统复制的优势:

1、更简单的实现failover,不用以前那样在需要找log_file和log_Pos。

2、更简单的搭建主从复制。

3、比传统复制更加安全。

4、GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过。

主从复制,默认是通过pos复制(postion),就是说在日志文档里,将用户进行的每一项操作都进行编号(pos),每一个event都有一个起始编号,一个终止编号,我们在配置主从复制时从节点时,要输入master的log_pos值就是这个原因,要求它从哪个pos开始同步数据库里的数据,这也是传统复制技术.

MySQL5.6之后增加了GTID复制,GTID就是类似于pos的一个作用,不过它是整个mysql复制架构全局通用的,就是说在这整个mysql冗余架构中,它们的日志文件里事件的GTID值是一致的

两者都是日志文件里事件的一个标志,如果将整个mysql集群看作一个整体,pos就是局部的,GTID就是全局的.
在这里插入图片描述

如果使用的是pos,三者的pos不一致,slave2怎么去获取它当前要同步的事件在slave1里的pos呢,很难.
所以就有了GTID,全局的,将所有节点对于同一个event的标记完全一致,当master crash掉之后,slave2根据同一个GTID直接去读取slave1的日志文件,继续同步.

GTID主从配置

环境说明:

数据库角色IP应用与系统版本有无数据
主数据库192.168.96.129centos8/redhat8 mysql-5.7无数据
从数据库192.168.96.133centos8/redhat8 mysql-5.7无数据
从数据库192.168.96.134centos8/redhat8 mysql-5.7无数据

1 mysql安装

分别在主从三台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考MySQL5.7安装

2先把三台服务器的防火墙关闭

#主
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; v>
   Active: inactive (dead)
     Docs: man:firewalld(1)

May 11 22:30:55 localhost.localdomain systemd[1]: Starting firewalld - dy>
May 11 22:30:56 localhost.localdomain systemd[1]: Started firewalld - dyn>
May 11 22:30:57 localhost.localdomain firewalld[932]: WARNING: AllowZoneD>
May 11 23:10:02 localhost.localdomain systemd[1]: Stopping firewalld - dy>
May 11 23:10:03 localhost.localdomain systemd[1]: firewalld.service: Succ>
May 11 23:10:03 localhost.localdomain systemd[1]: Stopped firewalld - dyn>
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
#从1
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl stastus firewalld
Unknown operation stastus.
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; ve>
   Active: inactive (dead) since Tue 2021-05-11 23:11:38 CST; 21s ago
     Docs: man:firewalld(1)
  Process: 916 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_>
 Main PID: 916 (code=exited, status=0/SUCCESS)

May 11 22:27:06 localhost.localdomain systemd[1]: Starting firewalld - dy>
May 11 22:27:07 localhost.localdomain systemd[1]: Started firewalld - dyn>
May 11 22:27:08 localhost.localdomain firewalld[916]: WARNING: AllowZoneD>
May 11 23:11:38 localhost.localdomain systemd[1]: Stopping firewalld - dy>
May 11 23:11:38 localhost.localdomain systemd[1]: firewalld.service: Succ>
May 11 23:11:38 localhost.localdomain systemd[1]: Stopped firewalld - dyn>
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive
#从2
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; ve>
   Active: inactive (dead) since Tue 2021-05-11 23:13:21 CST; 6s ago
     Docs: man:firewalld(1)
  Process: 932 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_>
 Main PID: 932 (code=exited, status=0/SUCCESS)

May 11 22:26:44 localhost.localdomain systemd[1]: Starting firewalld - dy>
May 11 22:26:44 localhost.localdomain systemd[1]: Started firewalld - dyn>
May 11 22:26:45 localhost.localdomain firewalld[932]: WARNING: AllowZoneD>
May 11 23:13:21 localhost.localdomain systemd[1]: Stopping firewalld - dy>
May 11 23:13:21 localhost.localdomain systemd[1]: firewalld.service: Succ>
May 11 23:13:21 localhost.localdomain systemd[1]: Stopped firewalld - dyn>
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce 
Permissive

3查看各个服务器中的mysq中数据是否一致

# 主
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
# 从1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
# 从2
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

如果数据库中的数据不一样,则需要全备主服务器中数据,然后恢复到从服务器中,备份时使用以下命令锁表,使其不能插入数据。

mysql> FLUSH TABLES WITH READ LOCK;

全量备份若有疑问请参考 全量备份

4在主数据库里创建一个同步账号授权给从数据库使用

mysql> CREATE USER 'bwxh'@'192.168.96.133' IDENTIFIED BY 'bwxh123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'bwxh'@'192.168.96.133';
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER 'bwxh'@'192.168.96.134' IDENTIFIED BY 'bwxh123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'bwxh'@'192.168.96.134';
Query OK, 0 rows affected (0.00 sec)

5配置主数据库

[root@localhost ~]# vim /etc/my.cnf
#添加以下数据
[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
log-bin=mysql_bin
server_id=10	#数据库服务器唯一标识符,主库的server-id值必须比从库的小
gtid_mode=on	#强制gtid一致性,开启后对于特定create table不被支持
enforce_gtid_consistency=on
log-slave-updates=1
binlog_format=row
skip_slave_start=1

#重启MySQL服务
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS!

#查看主库的状态
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysqld-bin.000001 |      154 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

6配置从数据库

从1

[root@localhost ~]# vim /etc/my.cnf
//添加以下内容
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
og-bin=mysql_bin
server_id=10	#数据库服务器唯一标识符,主库的server-id值必须比从库的小
gtid_mode=on	#强制gtid一致性,开启后对于特定create table不被支持
enforce_gtid_consistency=on
log-slave-updates=1
binlog_format=row
skip_slave_start=1
//重启从库的MySQL服务
[root@localhost ~]#  service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

//配置并启动主从复制
ysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.96.129',
    -> MASTER_USER='bwxh',
    -> MASTER_PASSWORD='bwxh123',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysqld-bin.000001',
    -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>reset slave;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

//查看从服务器状态
mysql> show slave status \\G
*************************** 1. row ***************************
               ......
             Slave_IO_Running: Yes		//此次必须为yes
            Slave_SQL_Running: Yes		//此次必须为yes
            ......
1 row in set (0.00 sec)

从2

[root@localhost ~]# vim /etc/my.cnf
//添加以下内容
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
og-bin=mysql_bin
server_id=10	#数据库服务器唯一标识符,主库的server-id值必须比从库的小
gtid_mode=on	#强制gtid一致性,开启后对于特定create table不被支持
enforce_gtid_consistency=on
log-slave-updates=1
binlog_format=row
skip_slave_start=1
//重启从库的MySQL服务
[root@localhost ~]#  service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

//配置并启动主从复制
ysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.96.129',
    -> MASTER_USER='bwxh',
    -> MASTER_PASSWORD='bwxh123',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysqld-bin.000001',
    -> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>reset slave;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

//查看从服务器状态
mysql> show slave status \\G
*************************** 1. row ***************************
               ......
             Slave_IO_Running: Yes		//此次必须为yes
            Slave_SQL_Running: Yes		//此次必须为yes
            ......
1 row in set (0.00 sec)

7测试验证

在主服务器的数据库中创建bwxh库,在bwxh库中创建表student并插入数据:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database hyh;
Query OK, 1 row affected (0.00 sec)

mysql> use hyh;
Database changed

mysql> insert student(name,age) values('bwxh',18),('jj',23),('tom',
,20);
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | bwxh      |   18 |
|  2 | jj        |   23 |
|  3 | tom       |   20 |
+----+-----------+------+
3 rows in set (0.00 sec)

在从数据库中查看数据是否同步:

//从1
mysql> select * from hyh.student;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | bwxh      |   18 |
|  2 | jj        |   23 |
|  3 | tom       |   20 |
+----+-----------+------+
3 rows in set (0.01 sec)
//从2
mysql> select * from hyh.student;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | bwxh      |   18 |
|  2 | jj        |   23 |
|  3 | tom       |   20 |
+----+-----------+------+
3 rows in set (0.00 sec)

以上是关于GTID主从配置的主要内容,如果未能解决你的问题,请参考以下文章

MySQL5.7配置GTID主从---搭建GTID主从

MySQL5.7配置GTID主从---GTID介绍

配置MySQL GTID 主从复制

mysql主从之基于gtid的主从复制

MySQL GTID 主从复制的原理及配置

MySQL 基于 GTID 的主从复制和多实例配置