MySQL组复制是一个MySQL插件,它基于常规的MySQL复制,利用了基于行格式的二进制日志和GTID等特性。
MySQL组复制是MySQL 5.7.17开始引入的新功能,为主从复制实现高可用功能。它支持单主模型和多主模型两种工作方式(默认是单主模型)
。
组由多个服务器构成,通过传递消息进行交互,通信层保证原子消息传递。MGR构建于此通信层抽象之上,并实现了多主更新复制协议。组中的每个服务器独立地执行事务,但是所有读写事务只有在得到组的批准后才会提交。只读事务在组内不需要协调,因此立即提交。对于任何读写事务,当事务准备好在始发服务器处提交时,服务器以原子方式广播写入值(更改的行)和对应的写入集(更新的行的唯一标识符),然后将该事务加入全局事务列表。最终所有服务器都以相同的顺序接收并应用相同的事务集,所以它们在组内保持一致。
主机名 | ip | 服务 |
---|---|---|
server1 | 172.25.1.1 | master |
server2 | 172.25.1.2 | slave |
server3 | 172.25.1.3 | slave |
在master(server1)上:
步骤一:在网上下载mysql安装包并安装
步骤二:打开mysqld
systemctl start mysqld
步骤三:关闭mysqld
systemctl stop mysqld
步骤四:复制uuid
cat /var/lib/mysql/auto.cnf
rm -fr /var/lib/mysql/*
步骤六:修改配置文件/etc/my.cnf
server-id=1
gtid_mode=ON
enforce-gtid-consistency=true
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE #关闭binlog校验
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW #组复制依赖基于行的复制格式
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="508e830d-4983-11ea-9103-525400a1ffb6" #可以看/var/lib/mysql/auto.cnf
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.1.1:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off ##插件是否自动引导,这个选项一般都要off掉,只需要由发起组复制的节点开启,并只启动一次,如果是on,下次再启动时,会生成一个同名的组,可能会发生脑裂
loose-group_replication_ip_whitelist="127.0.0.1,172.25.1.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF #后两行是开启多主模式的参数
systemctl start mysqld
步骤八:初始化数据库
查看初始密码
grep password /var/log/mysqld.log
mysql_secure_installation
方式二:登录数据库,修改密码
mysql> alter user root@localhost identified by 'Westos+007';
mysql> SET SQL_LOG_BIN=0; #关闭二进制日志,防止传到其他server上
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+007'; #创建用户
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%'; #授权
mysql> FLUSH PRIVILEGES; #刷新授权表
mysql> SET SQL_LOG_BIN=1; ##开启日志
步骤八:配置slave连接master的信息
所用用户,登陆密码及组插件
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+007' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> SHOW PLUGINS; ##查看插件
mysql> SET GLOBAL group_replication_bootstrap_group=ON; ##组复制发起节点开启这个参数
mysql> START GROUP_REPLICATION; ##开启组复制
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
mysql> SELECT * FROM performance_schema.replication_group_members; ##查看server1是否online
添加server2到组内
步骤一:安装数据库软件
步骤二:开启数据库
systemctl start mysqld
步骤三:关闭数据库并删除数据库信息
systemctl stop mysqld
rm -fr /var/lib/mysql/*
server-id=2
gtid_mode=ON
enforce-gtid-consistency=true
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="508e830d-4983-11ea-9103-525400a1ffb6" #注意:这里和server1的保持一致!!!!!!
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.1.2:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.1.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
systemctl start mysqld
步骤四:修改登陆密码
grep password /var/log/mysqld.log
mysql_secure_installation
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+007';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+007' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> START GROUP_REPLICATION;
cat /var/log/mysqld.log
日志中提示要开启变量group_replication_allow_local_disjoint_gtids_join
设置全局变量
mysql> STOP GROUP_REPLICATION;
mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
mysql> START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;
添加server3到组内
步骤一:安装数据库软件
步骤二:开启数据库
systemctl start mysqld
步骤三:关闭数据库并删除数据库信息
systemctl stop mysqld
rm -fr /var/lib/mysql/*
server-id=3
gtid_mode=ON
enforce-gtid-consistency=true
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="508e830d-4983-11ea-9103-525400a1ffb6" #注意:这里和server1的保持一致!!!!!!
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.25.1.3:24901"
loose-group_replication_group_seeds= "172.25.1.1:24901,172.25.1.2:24901,172.25.1.3:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.1.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
步骤三:启动mysqld
systemctl start mysqld
步骤四:修改登陆密码
grep password /var/log/mysqld.log
mysql_secure_installation
步骤五:创建组复制用户并授权
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Westos+007';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Westos+007' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
设置全局变量
mysql> set global group_replication_allow_local_disjoint_gtids_join=on;
mysql> START GROUP_REPLICATION;
mysql> SELECT * FROM performance_schema.replication_group_members;
看到3台都是online,表示正常
这时在任何一个节点都可以写入和查看数据
测试:
在server3上插入数据:
mysql> create database yan;
mysql> show databases;
mysql> show databases;
mysql> use yan;
mysql> create table test (
-> name varchar(10) not null,
-> age int(10) not null);
mysql> insert into test values ('user1','18');
mysql> use yan;
mysql> show tables;
mysql> select * from test;
# 所有节点执行
mysql> stop group_replication;
mysql> set global group_replication_enforce_update_everywhere_checks=OFF;
mysql> set global group_replication_single_primary_mode=ON;
# 主节点(server1)执行
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# 从节点(server2 server3)执行
START GROUP_REPLICATION;
# 主节点(server1)查看MGR组信息
mysql> SELECT * FROM performance_schema.replication_group_members;
实现过程:
在master(server1上):
在slave(server2):
在slave(server3):
在master(server1上):
在slave(server2):
在slave(server3):
在master(server1上):
测试:
在slave(server2)上写入数据:
在slave(server3)上写入数据:
报错,证明slave无写入功能
在master(server1上)写入数据:
正常写入并查看
在slave(server2)上查看数据:
以上可得出,单主模式配置成功。master节点可以写入和读出,slave节点只能读出不能写入。
# 停止组复制(所有节点执行):
mysql> stop group_replication;
mysql> set global group_replication_single_primary_mode=OFF;
mysql> set global group_replication_enforce_update_everywhere_checks=ON;
# 随便选择某个节点执行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 其他节点执行
mysql> START GROUP_REPLICATION;
# 查看组信息,所有节点的online
mysql> SELECT * FROM performance_schema.replication_group_members;
总结:
实验中出现的报错并改正
:
mysql> insert into test values (‘user1’,‘18’);
ERROR 3098 (HY000): The table does not comply with the requirements by an external plugin.
改正方法
:
给表添加主键
mysql> alter table test modify age int primary key;