mysql gtid配置_配置MYSQL基于GTID 主从复制详细解析及步骤

本文详细介绍了MySQL的全局事务标识符(GTID)的概念、优势和工作原理,以及如何配置基于GTID的主从复制。GTID简化了主从复制的设置和故障转移,提供更安全的数据一致性。通过设置服务器ID、开启GTID模式和binlog格式,以及授权和启动复制,成功实现了GTID主从同步。
摘要由CSDN通过智能技术生成

GTID的概念

全局事务标识:global transaction identifiers

GTID是一个事务一一对应,并且全局唯一ID

GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或主从不一致

GTID用来代替传统复制方法,不再使用MASTER_LOG_FILE+MASTER_LOG_POS开启复制。而是使用MASTER_AUTO_POSTION=1的方式开始复制。

MySQL-5.6.5开始支持的,MySQL-5.6.10后开始完善。

在传统的slave端,binlog是不用开启的,但是在GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID(强制)。

GTID的组成

GTID = source_id:transaction_id

source_id,用于鉴别原服务器,即mysql服务器唯一的的server_uuid,由于GTID会传递到slave,所以也可以理解为源ID。transaction_id,为当前服务器上已提交事务的一个序列号,通常从1开始自增长的序列,一个数值对应一个事务。

示例:

3E11FA47-71CA-11E1-9E33-C80AA9429562:23

前面的一串为服务器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,后面的23为transaction_id

GTID的优势

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

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

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

4、GTID是连续的没有空洞的,保证数据的一致性,零丢失。

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、在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。

配置基于GTID的复制

环境说明:

数据库角色

IP

应用与系统

主数据库

192.168.24.128

centos7 myslq-5.7

从数据库

192.168.24.130

centos7 myslq-5.7

配置安装二台myslq服务

在主数据库服务器端操作如下

编辑配置文件

[root@linfan ~]# vim /etc/my.cnf

[root@linfan ~]# cat /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

//添加以下内容

# GTID

server-id=128 //主服务器id

gtid-mode=on //开启gtid模式

enforce-gtid-consistency=on //强制gtid一致性,开启后对于特定create table不被支持

# binlog

log_bin=master-binlog

log-slave-updates=1

binlog_format=row //强烈建议,其他格式可能导致数据不一致

# relay log

skip_slave_start=1

重启myslq服务

[root@linfan ~]# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

在主数据库上创建一个同步账户授权给从数据库使用

[root@linfan ~]# 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.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create user 'repl'@'192.168.24.130' identified by '123456';

Query OK, 0 rows affected (0.06 sec)

mysql> grant replication slave on *.* to 'repl'@'192.168.24.130';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.03 sec)

给主数据库上读锁

另开一个终端,给数据库上读锁,避免在备份期间有其他人在写入导致数据同步的不一致

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.17 sec) 此锁表的终端必须在备份完成以后才能退出(退出锁表失效)

在从数据库服务器操作如下

编辑配置文件

[root@linfan ~]# cat /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

# GTID

gtid_mode=on

enforce_gtid_consistency=on

server_id=130

# binlog

log-bin=slave-binlog

log-slave-updates=1

binlog-format=row //强烈建议,其他格式可能导致数据不一致

# relay log

skip-slave-start=1

重启mysql服务器

[root@linfan ~]# service mysqld restart

Shutting down MySQL.. SUCCESS!

Starting MySQL... SUCCESS!

配置并启动主从复制

mysql> change master to

-> master_host='192.168.24.128',

-> master_user='repl',

-> master_password='123456',

-> master_port=3306,

-> master_auto_position= 1 ;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

查看从服务器的状态:

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.24.128

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-binlog.000001

Read_Master_Log_Pos: 154

Relay_Log_File: linfan-relay-bin.000002

Relay_Log_Pos: 375

Relay_Master_Log_File: master-binlog.000001

Slave_IO_Running: Yes //此处必须为yes

Slave_SQL_Running: Yes //此处必须为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: 154

Relay_Log_Space: 583

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: 128

Master_UUID: 3396fe35-b1e1-11e8-81bb-000c292340f6

Master_Info_File: /opt/data/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:

Executed_Gtid_Set:

Auto_Position: 1

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

解除主库的锁表状态,直接退出交互式界面即可

mysql> quit

Bye

验证:

在主数据库创建一个库doudou

mysql> create database doudou;

Query OK, 1 row affected (0.01 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| doudou |

| mysql |

| performance_schema |

| sys |

+--------------------+

5 rows in set (0.03 sec)

在从数据库服务器上查看是否同步存在doudou库

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| doudou |

| mysql |

| performance_schema |

| sys |

+--------------------+

5 rows in set (0.01 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值