官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能,那么MySQL8.0基于GTID主从复制与之前版本有什么不同呢?下面我们一起来实践吧!
MySQL8.0之前版本的mysql密码加密方式为mysql_native_password,而MySQL8.0默认新添加的用户密码默认使用的caching_sha2_password,因此进行使用主从复制时可能会遇到错误:“error :Authentication plugin ’ caching_sha2_password’ reported error: Authentication requires secure connection.”。
解决方法有两种:
方法一: 需要修改数据库的认证方式为旧版mysql_native_password,网上95%博客都是采用此方法解决,在此不再过多陈述,大家可以自行查找文档。
方法二: 通过server的public key加密password
采用mysql8.0默认插件caching_sha2_password,客户端连接获取server的public key来加密password,即 get_master_public_key=1;,具体步骤见博客。
环境:
主数据库:192.168.0.44
从数据库:192.168.0.118
1、配置主服务器
1.1配置文件/etc/my.cnf 中开启gtid
[mysqld]
server-id=1
gtid_mode=ON # 开启gtid
enforce-gtid-consistency=ON
1.2 重启服务
systemctl restart mysqld
1.3 登录mysql ,建立同步账号
mysql -uroot -p密码
mysql> create user 'rootslave'@'192.168.0.118' identified by 'ZH123.com'; #创建用户
mysql> grant replication slave on *.* to 'rootslave'@'192.168.0.118'; #授权
mysql> flush privileges;
1.4 查看主服务器状态
- 查看gtid是否开启
- 查看主服务器的状态
2、从数据库配置
2.1 配置文件/etc/my.cnf 中开启gtid
[mysqld]
server-id=2
gtid_mode=ON
enforce-gtid-consistency=ON
2.2 重启服务
systemctl restart mysqld
2.3 配置从节点以使用基于GTID的自动定位
mysql> change master to
-> master_host='192.168.0.44',
-> master_port=3306,
-> master_user='repl',
-> master_password='ZH123.com',
-> master_auto_position=1,
-> get_master_public_key=1; # 通过server的public key加密password
2.4 开启主从复制
mysql> start slave;
2.5 查看主从复制状态
mysql> show slave status\G;
3、主从复制测试
(1) MySQL数据库的中事务自动提交,因此主从同步
<1> 主数据库表中插入数据
<2> 从数据库同步表信息
(2) MySQL数据库的手动开启事务,未提交事务时,主从不同步
<1> 主数据库开启事务,更改表数据,但未提交事务
<2> 从数据库不同步
<3> 主数据库提交事务
<4> 从数据库同步