1、MYSQL主从复制-Gtid方式
2、
3、
Gtid方式的主从复制方式与传统方式的原理一致,配置大部分相同,仅有一小部分的配置不同。
Gtid主从复制如下,如想实现多从,将下述的从再配置一次即可。
1、环境准备
数据库主从复制如下,如需实现多从,将以下实验从案例再配置一次即可。
操作系统 | MYSQL版本 | IP | 角色 |
---|---|---|---|
centos7 | 5.7 | 192.168.70.160 | Master |
centos7 | 5.7 | 192.168.70.161 | Slave |
/etc/hosts
文件解析(两台机器一样)
192.168.70.160 master1
192.168.80.161 slave1
2、Master
配置
//打开二进制日志
[root@master1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/mysql
socket=/var/lib/mysql/mysql.sock
log-bin #打开二进制日志
server-id=160 #给一个server-id
gtid_mode = ON #打开Gtid
enforce_gtid_consistency = 1 #还有这一行
//重启数据库
[root@master1 ~]# systemctl restart mysqld
//准备数据
[root@master1 ~]# mysql -pcba-123
mysql> create database linuxdb;
mysql> use linuxdb;
mysql> create table t1(id int,name varchar(20));
mysql> insert into t1 values (1,'tt');
mysql> insert into t1 values (2,'ttt');
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | tt |
| 2 | ttt |
+------+------+
//确认二进制日志有没有开启
[root@master1 ~]# mysql -pcba-123 -e "show variables like 'log_bin%';"
+---------------------------------+--------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------+
| log_bin | ON |
| log_bin_basename | /mysql/master1-bin |
| log_bin_index | /mysql/master1-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+--------------------------+
//授权,允许能够远程连接的主机
mysql> grant replication slave,replication client on *.* to 'rep'@'192.168.70.%' identified by 'Rep123.com';
//导出当前数据,主从复制也要基于全备,这次备份是准备复制给slave主机的
[root@master1 ~]# mysqldump -uroot -pcba-123 --all-databases --single-transaction --master-data=1 --flush-logs >/root/db-$(date +%F)-all.sql;
//传输给slave主机
[root@master1 ~]# scp /root/db-2019-11-30-all.sql root@slave1:/root
replication是响应的意思
3、Slave
配置
//检测是否能登录master的数据库
[root@slave1 ~]# mysql -hmaster1 -urep -p'Rep123.com'
//给一个server-id,不用配置bin-log
[root@slave1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/mysql
socket=/var/lib/mysql/mysql.sock
server-id=161
gtid_mode = ON #打开Gtid
enforce_gtid_consistency = 1 #还有这一行
//重启mysql服务
[root@slave1 ~]# systemctl restart mysqld
//导入数据
[root@slave1 ~]# mysql -uroot -p'cba-123' -e "source /root/db-2019-11-30-all.sql;"
//连接master
mysql> change master to master_host='master1',
master_user='rep',
master_password='Rep123.com',
master_auto_position=1;
//启动slave角色
mysql> start slave;
//查看角色是否同步
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master1
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master1-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: slave1-relay-bin.000003
Relay_Log_Pos: 371
Relay_Master_Log_File: master1-bin.000001
Slave_IO_Running: Yes #这两个线程一定要是YES才可以
Slave_SQL_Running: Yes
4、报错&解决
报错一:UUID冲突
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
解决办法:
将从上面的/mysql/auto.cnf更改名字,然后一下数据库
mv /mysql/auto.cnf /mysql/auto.cnf.bak