Linux学习-MySQL之MGR实现集群

MGR

MySQL Group Replication(简称MGR)是MySQL官方于2016年12月12日推出的一款高可用与高扩展的解决方案,它提供了高可用、高扩展、高可靠的MySQL集群服务,具有以下特点:

  • 强一致性:基于原生复制和paxos协议,保证数据传输的一致性和原子性
  • 高容错性:只要大多数节点没有出现故障,集群就可对外提供服务
  • 高扩展性:节点的加入和移除都是自动的,不需要人为过多干预
  • 高灵活性:具有单主模式和多主模式,单主模式在主宕机后自动选主,多主模式多节点写入
MGR工作原理

MGR基于主从复制基础架构实现,主要是在事务提交的过程中嵌入单独的binlog封装逻辑,并通过专门的group_replication_recovery复制通道进行数据传输,组复制插件使用Paxos协议的原子广播特性来保证集群内的大多数节点都能接收到数据包,当节点接收到write set(写集)之后,每个节点上的分布式状态机按照相同的规则对事务进行排序,并进行事务的冲突认证检测。对于写节点而主,当冲突认证检测通过之后,将数据变更写入自身的binlog中,然后在存储引擎层进行提交;对于读节点,当冲突认证检测通过后,把主库发送过来的binlog写入自身的relay_log中,然后SQL线程读取relay_log进行重放,并把重放的binlog写入自身的binlog中,接下来在存储引擎内部进行提交,

MGR实现单主
1.准备三台虚拟机:
MGR1:192.168.115.129
MGR2:192.168.115.128
MGR3:192.168.115.127
2.三台机器上安装MySQL8.0.36,以下命令在三台机器上分别执行
[root@mgr1 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
[root@mgr1 ~]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm 

#修改mysql-yum配置文件
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[root@mgr1 ~]# yum install -y mysql-server
#修改/etc/my.cnf配置文件,启动时跳过权限认证
[mysqld]
skip-grant-tables
...
[root@mgr1 mysql]# systemctl start mysqld
#修改MySQL初始密码为空
mysql> select User,Host,authentication_string from mysql.user;
+------------------+-----------+------------------------------------------------------------------------+
| User             | Host      | authentication_string                                                  |
+------------------+-----------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | localhost | $A$005:yKvr{2Q"xan9l[^Y^KcUGDRCDadfGa/ZHRHiZG.WzLw7PNO5ZX..O3KoUo3C |
+------------------+-----------+------------------------------------------------------------------------+
4 rows in set (0.01 sec)

mysql> update mysql.user set authentication_string='' where User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)


#修改三个节点/etc/hosts文件,添加以下行
192.168.115.129   mgr1
192.168.115.128   mgr2
192.168.115.127   mgr3

#修改三台节点/etc/my.cnf配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
#每台机器的server_id设置为不一样
server_id=1
#MGR要求开启GTID模式
gtid_mode=ON
enforce_gtid_consistency=ON
#基于安全考虑,MGR集群要求把主从复制信息记录到表
master_info_repository=TABLE
#二进制日志校验规则(MGR要求NONE)
binlog_checksum=NONE
#将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件
log_slave_updates=ON
#记录二进制日志名称(前缀)
log_bin=binlog
#MGR必须将binlog设置为ROW
binlog_format=ROW
#设置buffer_pool大小
innodb_buffer_pool_size=512M
#必须为每个事务收集写集合,使用XXHASH64哈希算法将其编码为散列
transaction_write_set_extraction=XXHASH64
plugin_load_add='group_replication.so'
#group_replication名称,格式同UUID,但不能和机器上MySQL实例UUID重复
group_replication_group_name="8d3cebd8-b132-11eb-8529-0242ac130003"
#不在重新启动的时候开启MGR
group_replication_start_on_boot=off
group_replication_bootstrap_group=off
#本地MGR实例使用的IP地址和端口,是MGR服务端口,MGR互相通信使用的端口
group_replication_local_address="192.168.115.129:33061"
#MGR实例控制的服务器IP地址和端口,这个是MGR端口
group_replication_group_seeds="192.168.115.129:33061,192.168.115.128:33061,192.168.115.127:33061"

#skip-grant-tables
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#在三个节点上分别创建复制账号
#关闭二进制日志记录
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)
mysql> create user rpl_user@'%' identified by 'Lotus!1120';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on *.* to rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> install plugin group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.04 sec)
#MGR需要这个异步复制通道实现新节点加入集群自动从其他节点复制数据,不需要手动指定从哪个节点复制,当一个成员加入组时会收到组内其他成员的配置信息。
mysql> change master to master_user='rpl_user',master_password='Lotus!1120' for channel 'group_replication_recovery';

#启动MGR集群中的第一个节点,MGR启动需要顺序,需要有其中一台数据库做引导,其他数据库才能加入,特别是单主模式,主库一定要先启动
mysql> set global group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> start group_replication;
Query OK, 0 rows affected (1.32 sec)

mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 7fe6c797-7ee9-11ed-9f0c-000c29c54dc0 | mgr1        |        3306 | ONLINE       | PRIMARY     | 8.0.31         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
1 row in set (0.01 sec)
#分别启动另外的节点
mysql> start group_replication;

#查看MGR成员情况
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 7fe6c797-7ee9-11ed-9f0c-000c29c54dc0 | mgr1        |        3306 | ONLINE       | PRIMARY     | 8.0.31         | XCom                       |
| group_replication_applier | 7fe82e5e-7ee9-11ed-b028-000c29219a70 | mgr3        |        3306 | ONLINE       | SECONDARY   | 8.0.31         | XCom                       |
| group_replication_applier | 7fe8618a-7ee9-11ed-afa0-000c2953cebe | mgr2        |        3306 | ONLINE       | SECONDARY   | 8.0.31         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
3 rows in set (0.00 sec)

你可能感兴趣的:(Linux,mysql,linux,学习)