mysql高可用之组复制

MySQL Group Replication( 简称 MGR ) 是 MySQL 官方于 2016 年 12 月推出的一个全新的高可用与高扩展的解决方案组复制是 MySQL 5.7.17 版本出现的新特性,它提供了高可用、高扩展、高可靠的 MySQL 集群服务MySQL 组复制分单主模式和多主模式,传统的 mysql 复制技术仅解决了数据同步的问题,MGR 对属于同一组的服务器自动进行协调。对于要提交的事务,组成员必须就全局事务序列中给定事务的顺序达成一致提交或回滚事务由每个服务器单独完成,但所有服务器都必须做出相同的决定如果存在网络分区,导致成员无法达成事先定义的分割策略,则在解决此问题之前系统不会继续进行,这是一种内置的自动裂脑保护机制MGR由组通信系统 ( Group Communication System , GCS ) 协议支持该系统提供故障检测机制、组成员服务以及安全且有序的消息传递 。
组复制流程:
组复制分为
单写/单组模式
多写/多组模式:

#数据需要保持强一致性
#为了避免实验中出现问题

#把mysql全部停掉

[root@mysql1 ~]# /etc/init.d/mysqld sto
Shutting down MySQL... SUCCESS!
[root@mysql2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.... SUCCESS!
[root@mysql3 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.... SUCCESS!
#把数据删掉(建的库表那些)#仅对于实验,在实际的开发环境中,切记要备份数据,数据不可随意删除。
[root@mysql1 ~]# rm -rf /data/mysql/*

#编辑配置文件

#组复制暂时仅支持innodb存储引擎,其他的不能用,为了避免报错,把其他的存储引擎给禁止了。
[root@mysql1 ~]# vim /etc/my.cnf
[root@mysql1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=1
gtid_mode=on
enforce-gtid-consistency=on
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" #禁用指定存储引擎
master_info_repository=TABLE #复制事件数据到表中而不记录在数据目录中
relay_log_info_repository=TABLE
binlog_checksum=NONE #禁止对二进制日志校验
log_slave_updates=ON #打开数据库中继,#当slave中sql线程读取日志后也会写入到自己的binlog中
log_bin=binlog #重新指定log名称
binlog_format=ROW #使用行日志格式
plugin_load_add='group_replication.so' #加载组复制插件
transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #通知插件正式加入#或创建的组名#名称为uuid格式
group_replication_start_on_boot=off #在server启动时不自动启动组复制
group_replication_local_address="172.25.254.6:33061" #指定插件接受其他成员的信息端口
group_replication_group_seeds="172.25.254.6:33061,172.25.254.66:33061,172.25.254.166:33061" #本地地址允许访问成员列表
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8" #主机白名单
group_replication_bootstrap_group=off #不随系统自启而启动
group_replication_single_primary_mode=OFF #使用多主模式
group_replication_enforce_update_everywhere_checks=ON #组同步中有任何改变检测更新
group_replication_allow_local_disjoint_gtids_join=1 #放弃自己信息以master事件为主

#数据库初始化
[root@mysql1 ~]# mysqld --user=mysql --initialize
2024-08-27T16:15:22.770393Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-27T16:15:33.915392Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-27T16:15:34.446094Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-27T16:15:34.971204Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 97168320-648f-11ef-aff6-000c29c6c80f.
2024-08-27T16:15:34.984340Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-27T16:15:36.041466Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-27T16:15:36.041564Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-27T16:15:36.044474Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-27T16:15:36.542994Z 1 [Note] A temporary password is generated for root@localhost: DpFfykyxX6#y
#登录-改密码
[root@mysql1 ~]# /etc/init.d/mysqld start

Starting MySQL..Logging to '/data/mysql/mysql1.err'.

...... SUCCESS!

[root@mysql1 ~]# mysql -uroot -p'DpFfykyxX6#y'

mysql> alter user root@localhost identified by 'redhat';

Query OK, 0 rows affected (0.01 sec)

#先关掉日志功能,不记录
#建立数据同步用的用户,授权
#打开日志功能
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'redhat';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

#master即为seed种子主机

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='redhat' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.03 sec)

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 warning (2.83 sec)

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW DATABASES;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | 7599627f-649e-11ef-94d3-000c29c6c80f | mysql1 | 3306 | ONLINE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

1 row in set (0.01 sec)

##三台主机都做地址解析

[root@mysql1 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.254.6 mysql1

172.25.254.66 mysql2

172.25.254.166 mysql3

#添加另外两台主机

[root@mysql1 ~]# scp /etc/my.cnf root@172.25.254.66:/etc/my.cnf

root@172.25.254.66's password:

my.cnf 100% 1580 245.4KB/s 00:00

[root@mysql1 ~]# scp /etc/my.cnf root@172.25.254.166:/etc/my.cnf

root@172.25.254.166's password:

my.cnf 100% 1580 210.9KB/s 00:00

#有的地方需要更改

[root@mysql2 ~]# vim /etc/my.cnf

[root@mysql2 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0
server-id=2

gtid_mode=on

enforce-gtid-consistency=on

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" #禁用指定存储引擎

master_info_repository=TABLE #复制事件数据到表中而不记录在数据目录中

relay_log_info_repository=TABLE

binlog_checksum=NONE #禁止对二进制日志校验

log_slave_updates=ON #打开数据库中继,#当slave中sql线程读取日志后也会写入到自己的binlog中

log_bin=binlog #重新指定log名称

binlog_format=ROW #使用行日志格式

plugin_load_add='group_replication.so' #加载组复制插件

transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列

group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #通知插件正式加入#或创建的组名#名称为uuid格式

group_replication_start_on_boot=off #在server启动时不自动启动组复制
group_replication_local_address="172.25.254.66:33061" #指定插件接受其他成员的信息端口
group_replication_group_seeds="172.25.254.6:33061,172.25.254.66:33061,172.25.254.166:33061" #本地地址允许访问成员列表

group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8" #主机白名单

group_replication_bootstrap_group=off #不随系统自启而启动

group_replication_single_primary_mode=OFF #使用多主模式

group_replication_enforce_update_everywhere_checks=ON #组同步中有任何改变检测更新

group_replication_allow_local_disjoint_gtids_join=1 #放弃自己信息以master事件为主

[root@mysql2 ~]# rm -rf /data/mysql/*

[root@mysql2 ~]# mysqld --user=mysql --initialize

[root@mysql2 ~]# /etc/init.d/mysqld start

Starting MySQL... SUCCESS!

[root@mysql2 ~]# mysql -uroot -p'u(&#E;uWr0M1';

mysql> alter user root@localhost identified by 'redhat';

Query OK, 0 rows affected (0.01 sec)

mysql> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'redhat';

Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='redhat' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected, 1 warning (6.68 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | 0034a5ff-64df-11ef-a462-000c294c383d | mysql2 | 3306 | RECOVERING |

| group_replication_applier | 7599627f-649e-11ef-94d3-000c29c6c80f | mysql1 | 3306 | ONLINE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

2 rows in set (0.01 sec)

slave2同slave1:

[root@mysql3 ~]# vim /etc/my.cnf

[root@mysql3 ~]# cat /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0
server-id=3

gtid_mode=on

enforce-gtid-consistency=on

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" #禁用指定存储引擎

master_info_repository=TABLE #复制事件数据到表中而不记录在数据目录中

relay_log_info_repository=TABLE

binlog_checksum=NONE #禁止对二进制日志校验

log_slave_updates=ON #打开数据库中继,#当slave中sql线程读取日志后也会写入到自己的binlog中

log_bin=binlog #重新指定log名称

binlog_format=ROW #使用行日志格式

plugin_load_add='group_replication.so' #加载组复制插件

transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列

group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #通知插件正式加入#或创建的组名#名称为uuid格式

group_replication_start_on_boot=off #在server启动时不自动启动组复制

group_replication_local_address="172.25.254.166:33061" #指定插件接受其他成员的信息端口
group_replication_group_seeds="172.25.254.6:33061,172.25.254.66:33061,172.25.254.166:33061" #本地地址允许访问成员列表

group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8" #主机白名单

group_replication_bootstrap_group=off #不随系统自启而启动

group_replication_single_primary_mode=OFF #使用多主模式

group_replication_enforce_update_everywhere_checks=ON #组同步中有任何改变检测更新

group_replication_allow_local_disjoint_gtids_join=1 #放弃自己信息以master事件为主

..............................

第三台主机做好后

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | 0034a5ff-64df-11ef-a462-000c294c383d | mysql2 | 3306 | ONLINE |

| group_replication_applier | 3cceba2b-649c-11ef-bb42-000c29a7a897 | mysql3 | 3306 | ONLINE |

| group_replication_applier | 7599627f-649e-11ef-94d3-000c29c6c80f | mysql1 | 3306 | ONLINE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

3 rows in set (0.01 sec)

测试:
在每个节点都可以完成读写
[root@mysql1 ~]# mysql -predhat
mysql> CREATE DATABASE folian2;

Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE folian2.userlist(

-> username VARCHAR(10) PRIMARY KEY NOT NULL,

-> password VARCHAR(50) NOT NULL

-> );

Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO folian2.userlist VALUES ('user1','111');

Query OK, 1 row affected (0.02 sec)

mysql> SELECT * FROM folian2.userlist;

+----------+----------+

| username | password |

+----------+----------+

| user1 | 111 |

+----------+----------+

1 row in set (0.00 sec)

[root@mysql2 ~]# mysql -predhat
[root@mysql2 ~]# mysql -p
mysql> INSERT INTO folian2.userlist values ('user2','222');
Query OK, 1 row affected (0.00 sec)
mysql> select * from lee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 111 |
| user2 | 222 |
+----------+----------+
2 rows in set (0.00 sec)
[root@mysql3 ~]# mysql -p
mysql> INSERT INTO folian2.userlist values ('user3','333');
Query OK, 1 row affected (0.00 sec)
mysql> select * from lee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 111 |
| user2 | 222 |
| user3 | 333 |
+----------+----------+
3 rows in set (0.00 sec)

相关推荐
C吴新科3 小时前
MySQL入门操作详解
mysql
Ai 编码助手5 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员6 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle6 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻6 小时前
MySQL排序查询
数据库·mysql
萧鼎6 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^6 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋36 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
荒川之神6 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
IT培训中心-竺老师6 小时前
Oracle 23AI创建示例库
数据库·oracle