1 什么是GTID
GTID(Global Transaction ID)是对于一个已提交事务的唯一编号,并且是一个全局(主从复制)唯一的编号
官方定义如下
GTID=source_id:transaction_id
7E11FA47-31CA-19E1-9E56-C43AA21293967:29
什么是server_uuid,和server_id区别?
特性:server_uuid全局唯一,具备幂等性
1.2 GTID核心参数
gtid-mode=on --启用gtid类型
enforce-gtid-consistency=true --强制gtid一致性
log-slave-updates=1 --slave更新是否计入日志
1.3 GTID复制配置过程:
1.3.1 清理环境
pkill mysqld
rm -rf /data/mysql/data/*
rm -rf /data/binlog/*
1.3.2 准备配置文件
主库db01:
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/data/mysql/
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=51
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db01 [\\d]>
EOF
slave1(db02):
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/data/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=52
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db02 [\\d]>
EOF
slave2(db03):
cat > /etc/my.cnf <<EOF
[mysqld]
basedir=/data/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=53
port=3306
secure-file-priv=/tmp
autocommit=0
log_bin=/data/binlog/mysql-bin
binlog_format=row
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1
[mysql]
prompt=db03 [\\d]>
EOF
1.3.3 初始化数据
mysqld --initialeze-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data (主库和从库都要执行)
1.3.4 启动数据库
/etc/init.d/mysqld start
1.3.5 开始构建主从
master 51
slave 52,53
51:
grant replication slave on *.* to repl@'10.0.0.%' identified by '123';
52\53;
change master to
master_host='10.0.0.51',
master_user='repl',
master_password='123' ,
MASTER_AUTO_POSITION=1;
start slave;