类型 | 说明 |
---|---|
操作系统 | Linux version 3.10.0-693.el7.x86_64 |
MySQL 版本 | mysql/mysql-5.7.23-linux 64 位 |
本次部署为多机单实例
basedir
/usr/local/mysql
IP | 端口号 | 数据目录 | group_replication 通信端口 |
---|---|---|---|
172.18.0.11 | 3306 | /data/mgr/mysql3306{data,logs,tmp} | 23306 |
172.18.0.12 | 3306 | /data/mgr/mysql3306{data,logs,tmp} | 23306 |
172.18.0.13 | 3306 | /data/mgr/mysql3306{data,logs,tmp} | 23306 |
# cd /opt/mysql/
# tar zxvf /path/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
# ln -s /opt/mysql/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz mysql
# groupadd mysql
# useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -MN mysql
# chown -R mysql:mysql mysql/
在三个服务器上都创建这样的目录
# mkdir -p mgr/mysql3306/{data,logs,tmp}
# chown -R mysql:mysql mgr
IP | 端口号 | 配置文件 |
---|---|---|
172.18.0.11 | 3306 | /data/mgr/mysql3306/my3306.cnf |
172.18.0.12 | 3306 | /data/mgr/mysql3306/my3306.cnf |
172.18.0.13 | 3306 | /data/mgr/mysql3306/my3306.cnf |
配置文件中注意参数
#group replication
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="3db33b36-0e51-409f-a61d-c99756e90155"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.18.0.11:23306"
loose-group_replication_group_seeds= "172.18.0.11:23306,172.18.0.12:23307,172.18.0.13:23308"
loose-group_replication_bootstrap_group= off
## configure single-master OR multi-master
#loose-group_replication_single_primary_mode=off
#loose-group_replication_enforce_update_everywhere_checks=on
The name of the group which this server instance belongs to. Must be a valid UUID. This UUID is used internally when setting GTIDs for Group Replication events in the binary log.
Important
A unique UUID must be used.
可在命令行中通过 uuidgen 生成唯一的 UUID
# uuidgen
621f32ad-54e2-4f43-93b9-5808f463b931
Whether the server should start Group Replication or not during server start.
将 group_replication_start_on_boot 设置为 off,尽量手动操作集群节点的加入
The network address which the member provides for connections from other members, specified as a
host:port
formatted string. This address must be reachable by all members of the group because it is used by XCOM, the internal group communication system.
Warning
Do not use this address for communication with the member.
Other Group Replication members contact this member through this
host:port
for all internal group communication. This is not the MySQL server SQL protocol host and port.
A list of group members that provide a member which joins the group with the data required for the joining member to gain synchrony with the group. The list consists of the seed member’s network addresses specified as a comma separated list, such as host1:port1,host2:port2.
Important
These addresses must not be the member’s SQL hostname and port.
Usually this list consists of all members of the group, but you can choose a subset of the group members to be seeds. The list must contain at least one valid member address. Each address is validated when starting Group Replication. If the list does not contain any valid host names, issuing START GROUP_REPLICATION fails.
Configure this server to bootstrap the group.
This option must only be set on one server and only when starting the group for the first time or restarting the entire group. After the group has been bootstrapped, set this option to OFF.
It should be set to OFF both dynamically and in the configuration files. Starting two servers or restarting one server with this option set while the group is running may lead to an artificial split brain situation, where two independent groups with the same name are bootstrapped.
Instructs the group to automatically pick a single server to be the one that handles read/write workload. This server is the PRIMARY and all others are SECONDARIES.
Enable or disable strict consistency checks for multi-primary update everywhere.
Allow the current server to join the group even if it has transactions not present in the group.
Warning
Use caution when enabling this option as incorrect usage could lead to inconsistencies in the group.
新节点未作 reset master
处理,具有 GTID
信息,设置该参数为 1,允许带有 GTID 信息的节点加入集群
# /usr/local/mysql/bin/mysqld --defaults-file=/data/mgr/mysql3306/my3306.cnf --initialize-insecure
# /usr/local/mysql/bin/mysqld --defaults-file=/data/mgr/mysql3306/my3306.cnf &
进入 MySQL
加载 group replication 的 plugin 2 种方式
mysql> install plugin group_replication soname 'group_replication.so';
mysql> show plugins;
# group replication plugin
plugin_load_add=group_replication.so
mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+----------------------+---------+
| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
mysql> set sql_log_bin=0;
mysql> create user group_repl_user@'172.18.0.%';
mysql> grant replication slave on *.* to group_repl_user@'172.18.0.%' identified by 'group_repl_password';
mysql> set sql_log_bin=1;
# channel 名称 group_replication_recovery 不能改变
mysql> change master to master_user='group_repl_user',
master_password='group_repl_password' for channel 'group_replication_recovery';
使用第一个节点来引导集群(只在第一个节点使用)
mysql> set global group_replication_bootstrap_group = on;
mysql> start group_replication;
ysql> set global group_replication_bootstrap_group = off;
集群引导结束后,需要将 group_replication_bootstrap_group
设为 off
mysql> set global group_replication_bootstrap_group = off;
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 655d0632-7910-11e9-b24a-0242ac12000b | zst1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
Important
在 MGR 环境中建立的表必须要有主键,否则不允许向该表插入数据
mysql> create database test;
mysql> create table t1(id int not null auto_increment primary key, name varchar(10));
mysql> insert into t1(name) select 'a';
mysql> insert into t1(name) select 'b';
mysql> insert into t1(name) select 'c';
检索数据
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+------+
# /usr/local/mysql/bin/mysqld --defaults-file=/data/mgr/mysql3306/my3306.cnf &
进入 MySQL
加载 group replication 的 plugin 2 种方式
mysql> install plugin group_replication soname 'group_replication.so';
mysql> show plugins;
# group replication plugin
plugin_load_add=group_replication.so
mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+----------------------+---------+
| group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL |
+----------------------------+----------+--------------------+----------------------+---------+
mysql> set sql_log_bin=0;
mysql> create user group_repl_user@'172.18.0.%';
mysql> grant replication slave on *.* to group_repl_user@'172.18.0.%' identified by 'group_repl_password';
mysql> set sql_log_bin=1;
# channel 名称 group_replication_recovery 不能改变
mysql> change master to master_user='group_repl_user',
master_password='group_repl_password' for channel 'group_replication_recovery';
使用第一个节点来引导集群(只在第一个节点使用)
mysql> start group_replication;
mysql> select * from performance_schema.replication_group_members;
参考启动第二个节点
server_id
loose-group_replication_local_address
loose-group_replication_group_seeds
truncate table slave_worker_info
flush privileges
待定 (reset master)set @@ global.gtid_pruged='xxx'
start group_replication;
在三个节点上分别进行
3306:insert into t1(name) select '3306';
3307:insert into t1(name) select '3307';
3308: insert into t1(name) select '3308';
观察下哪个语句会执行成功,思考为什么?
MGR 默认是单主模式,也就是组中三个节点中只有一个节点可以写,剩余的两个节点只能读。
在 Group Replication 配置中,默认的模式是 Single-master
模式——集群中只有一个节点可进行写操作,其他节点是开启着:read-only 模式。
当主节点宕机后,集群会从其他节点选举出新的 master
.
在 single-master
模式下,主节点挂掉后,会从集群中原来的节点选举出一个节点成为新的 primary
节点,而其他节点依然是 read-only
状态
在 single-master
模式中可利用下面 SQL 来发现主节点
SQL 1
show status like 'group_replication_primary_member';
SQL 2
select * from performance_schema.global_status where variable_name = 'group_replication_primary_member';
使用 multi-master 模式时,可配置为只写其中一个节点,以此避免发生故障。
该结构不是默认结构,如果要使用 multi-master
结构,需要在配置文件的 [mysqld]
部分添加以下配置
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
在 multi-master
结构中需要注意,不能同时在不同节点对同一行数据进行 update 操作,如果进行,客户端会收到报错。在使用这个结构时,需要应用自己去控制并行度。
在该结构中所有的节点上都可以进行读和写操作,任何一个节点挂掉后,可以从负载设备中自动摘除即可。例如(haproxy, proxysql 等等)
在 multi-master
结构中,每个节点都可进行读写,因此不存所谓的 primary
节点,使用以下 SQL
select * from performance_schema.global_status where variable_name = 'group_replication_primary_member';
返回查询结果 group_replication_primary_member
为空
集群重启分成两种情况
start group_replication;
select * from performance_schema.replication_group_members;
第一个节点启动 (single-master
& multi-master
相同)
set global group_replication_bootstrap_group=on;
start group_replication;
set global group_replication_bootstrap_group=off;
其他节点启动
start group_replication;
select * from performance_schema.replication_group_members where member_id = @@server_uuid;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | ea468068-78a5-11e9-9caf-000c29aae378 | LinuxModel | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
select * from performance_schema.global_variables where variable_name in ('read_only', 'super_read_only');
+-----------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+-----------------+----------------+
| read_only | ON |
| super_read_only | ON |
+-----------------+----------------+
select RECEIVED_TRANSACTION_SET from performance_schema.replication_connection_status where channel_name = 'group_replication_applier';
select @@global.gtid_executed;
延迟的 GTID 数量 = 远程获取的 GTID - 本节点执行的 GTID
select * from performance_schema.replication_group_member_stats where member_id=@@server_uuid
set group_replication_flow_control_mode=QUOTA
set group_replication_flow_control_mode='DISABLED'
slave_parallel_type=logical_clock
slave_parallel_workers=4|8
group_replication_compression_threshold=1000000~2000000