暂无图片
暂无图片
9
暂无图片
暂无图片
暂无图片

MGR 单主模式部署教程(基于 MySQL 8.0.28)

原创 代野(Tank) 2022-02-23
6721

MySQL 8.0.28
CentOS 7.6

在开动前,首先推荐大家快速读一遍 MySQL 官方文档中 Group Replication 章节,内容虽厚重,但极其详尽。相比网上总结的文章也包括本篇,官网是最靠谱的,毕竟每个人的操作环境、技能水平以及表达方式各有不同,建议实操以官网为主、总结篇为辅,少踩坑、高效完成部署。

本次基于 8.0.28 版本部署 MGR 花费时间并不短,从准备虚机环境、部署验证加上文档整理,断断续续折腾了近两天的时间。结合部署过程,在正式部署前先分享几个要点:

  • 务必做好环境初始化工作,重点是防火墙的处理;
  • MySQL 8 版本密码认证方式和以往版本的差异;
  • 如果是克隆的数据库,需要删除 auto.cnf 后重启生成新 id,否则入组会产生冲突。

1. 环境准备

第一步是提前准备好3 个 MySQL 实例,部署文档可以参考CentOS7.6 安装部署 MySQL 8.0.28一文,官网说明请参考 第 2 章 “安装和升级 MySQL”

组复制是MySQL Server 8.0提供的内置 MySQL 插件,因此不需要进行额外安装。有关 MySQL 插件的更多背景信息,请参见第 5.6 节 “MySQL 服务器插件”

三实例的架构是最小规模的 MGR 架构,添加更多实例可提高 Group 的容错能力,一个 Group 中最多可以有 9 个实例,详细信息请参考第 17.1.4.2 节 “故障检测”

服务器列表

IP Role Host Name DB Port Internal Port
1 192.168.56.103 Primary s1 3306 33061
2 192.168.56.104 Secondary s2 3306 33061
3 192.168.56.105 Secondary s3 3306 33061

基础环境

修改 host 文件

cat >> /etc/hosts << EOF 192.168.56.103 s1 192.168.56.104 s2 192.168.56.105 s3 EOF

关闭防火墙

systemctl stop firewalld.service systemctl disable firewalld.service

2. Server S1 (Primary)

为组复制配置实例属性

-- 以下操作请在 S1 中执行 cp /etc/my.cnf /etc/my.cnf.bak cat >> /etc/my.cnf << EOF # # Disable other storage engines # disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" # # Replication configuration parameters # server_id=1 gtid_mode=ON enforce_gtid_consistency=ON log_bin=binlog log_slave_updates=ON #binlog_checksum=NONE # Not needed from 8.0.21 binlog_format=ROW master_info_repository=TABLE relay_log_info_repository=TABLE transaction_write_set_extraction=XXHASH64 # # Group Replication configuration # plugin_load_add='group_replication.so' group_replication_group_name="b309fcc3-93e8-11ec-b5bd-080027c850b1" group_replication_start_on_boot=off group_replication_local_address= "s1:33061" group_replication_group_seeds= "s1:33061,s2:33061,s3:33061" group_replication_bootstrap_group=off EOF
  • plugin-load-add:实例启动时会将组复制插件加载到插件列表中;
  • group_replication_group_name:在数据库中使用 SELECT UUID() 命令生成;
  • group_replication_start_on_boot:决定插件在服务启动时是否自启;
  • group_replication_local_address:内部通讯端口,推荐使用 33061;
  • group_replication_bootstrap_group:在首个实例联机后,需要设置为 off。如果多次引导组则人为触发裂脑,产生两个具有相同名称的不同组。

重启 S1 的 MySQL 数据库

systemctl restart mysqld tail -200 /var/log/mysqld.log

创建复制用户

mysql -uroot -p SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY 'Ro0t@2022'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%'; GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%'; FLUSH PRIVILEGES;

image_1x.png

启动组复制

当上面配置文件中已经配置“plugin_load_add=‘group_replication.so’”参数后,需检查插件是否已成功安装,请执行命令并检查输出。

SHOW PLUGINS;

image_2x.png

引导

首次启动组的过程称为引导。您可以使用group_replication_bootstrap_group系统变量来引导组。引导只能由单个服务器完成,该服务器启动组并且只能执行一次。
这就是group_replication_bootstrap_group选项的值未存储在实例的选项文件中的原因。如果它保存在选项文件中,则在重新启动服务器时,服务器会自动引导具有相同名称的第二个组。这将导致两个具有相同名称的不同组。
若正确地引导组,请连接到 s1 执行如下语句:

SET GLOBAL group_replication_bootstrap_group=ON; START GROUP_REPLICATION USER='rpl_user', PASSWORD='Ro0t@2022'; SET GLOBAL group_replication_bootstrap_group=OFF; --确认成员信息 SELECT * FROM performance_schema.replication_group_members;

image_3x.png

为了验证后续其他节点入组情况,下面将创建一个表并向其中添加一些数据进行验证。

CREATE DATABASE test; USE test; CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL); INSERT INTO t1 VALUES (1, 'Tank');

检查表的内容和二进制日志信息。

SELECT * FROM t1; SHOW BINLOG EVENTS;

此时,组中有一个成员 s1,也包括一些测试数据。下面继续添加其他实例扩展组。

3. Server S2 (Secondary)

在 s2 中,首先修改 MySQL 配置文件,该配置类似于用于服务器 s1 的配置,但 server_id 和 group_replication_local_address 除外。

cp /etc/my.cnf /etc/my.cnf.bak cat >> /etc/my.cnf << EOF [mysqld] # # Disable other storage engines # disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" # # Replication configuration parameters # server_id=2 gtid_mode=ON enforce_gtid_consistency=ON # binlog_checksum=NONE # Not needed from 8.0.21 # # Group Replication configuration # plugin_load_add='group_replication.so' group_replication_group_name="b309fcc3-93e8-11ec-b5bd-080027c850b1" group_replication_start_on_boot=off group_replication_local_address= "s2:33061" group_replication_group_seeds= "s1:33061,s2:33061,s3:33061" group_replication_bootstrap_group= off EOF

与服务器 s1 的配置过程类似,在配置文件就位后可以启动服务器。

systemctl restart mysqld tail -200 /var/log/mysqld.log

然后按如下所示配置进行配置,这些命令与设置服务器 s1 时使用的命令相同,因为用户是在组内共享。

mysql -uroot -p SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY 'Ro0t@2022'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%'; GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%'; FLUSH PRIVILEGES; SET SQL_LOG_BIN=1; CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user', SOURCE_PASSWORD='Ro0t@2022' FOR CHANNEL 'group_replication_recovery';

image_4x.png
启动组复制,s2 启动加入组中。

START GROUP_REPLICATION USER='rpl_user', PASSWORD='Ro0t@2022'; SELECT * FROM performance_schema.replication_group_members;

image_5x.png

确认数据同步情况。

SHOW DATABASES LIKE 'test'; SELECT * FROM test.t1; SHOW BINLOG EVENTS;

image_6x.png

4. Server S3 (Secondary)

向组添加其他实例与添加第 2 台服务器的步骤顺序基本相同,只是必须更改配置。总结所需的命令如下。

1.修改配置文件

cp /etc/my.cnf /etc/my.cnf.bak cat >> /etc/my.cnf << EOF [mysqld] # # Disable other storage engines # disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" # # Replication configuration parameters # server_id=3 gtid_mode=ON enforce_gtid_consistency=ON binlog_checksum=NONE # Not needed from 8.0.21 # # Group Replication configuration # plugin_load_add='group_replication.so' group_replication_group_name="b309fcc3-93e8-11ec-b5bd-080027c850b1" group_replication_start_on_boot=off group_replication_local_address= "s3:33061" group_replication_group_seeds= "s1:33061,s2:33061,s3:33061" EOF

2.启动服务器并连接到实例,创建复制用户。

mysql -uroot -p SET SQL_LOG_BIN=0; CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY 'Ro0t@2022'; GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%'; GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%'; FLUSH PRIVILEGES; SET SQL_LOG_BIN=1; CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user', SOURCE_PASSWORD='Ro0t@2022' FOR CHANNEL 'group_replication_recovery';

3.启动复制

START GROUP_REPLICATION USER='rpl_user', PASSWORD='Ro0t@2022'; SELECT * FROM performance_schema.replication_group_members;

image_7x.png

此时,服务器 s3 已引导并运行,再次确认数据同步情况。

SHOW DATABASES LIKE 'test'; SELECT * FROM test.t1; SHOW BINLOG EVENTS;

image_8x.png

5. 错误记录

MY-011735

在第 2 个实例上执行“START GROUP_REPLICATION;”命令后,报出 IP 地址不匹配的信息。

前台错误信息:

ERROR 3096 (HY000): The START GROUP_REPLICATION command failed as there was an error when initializing the group communication layer.

log 错误信息:

[ERROR] [MY-011735] [Repl] Plugin group_replication reported: '[GCS] There is no local IP address matching the one configured for the local node (s2:33061).'

解决方法:

检查 /etc/hosts 或 IP 地址信息填写是否正确,修改正确后可正常执行。

MY-010897

报错描述:

在第 2 个实例上执行“START GROUP_REPLICATION;”命令后,日志报出密码认证相关错误信息。

[Warning] [MY-010897] [Repl] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.

解决方法:

本文档所使用 MySQL 版本为 8.0.28,因此语法需参考下方写法。

Or if you are providing user credentials for distributed recovery on the START GROUP_REPLICATION statement (which you can from MySQL 8.0.21): mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='password';

MY-010584

[ERROR] [MY-010584] [Repl] Slave I/O for channel 'group_replication_recovery': error connecting to master 'rpl_user@s1:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061

image_9x.png
解决方法:

  1. 修改 user 属性(MySQL 安装后)
ALTER USER 'rpl_user'@'%' IDENTIFIED WITH mysql_native_password BY 'Ro0t@2022'; select user,host,plugin from mysql.user;

image_10x.png

  1. 创建 rpl_user 用户时直接加上属性参数(MySQL 安装后)
CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY 'Ro0t@2022';
  1. MySQL 安装前在配置文件中加上对应的参数(MySQL 安装前)
--/etc/my.cnf # # Authentication # default_authentication_plugin=mysql_native_password

MY-011516

第 3 个实例启动组复制失败,后台提示 server_uuid 有冲突。

-- 前台 mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='Ro0t@2022'; ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log. -- 日志 [ERROR] [MY-011516] [Repl] Plugin group_replication reported: 'There is already a member with server_uuid c2e82b68-8737-11ec-bb84-0800278bd58e. The member will now exit the group.'

由于第 3 个虚机是以镜像方式拷贝来的,因此 server_uuid 信息一致,需处理。

解决方法:

在$datadir 路径中删除或重命名 auto.cnf 文件,重启数据库后会自动生成新的文件,此时 server_uuid 冲突的问题就不会出现了。

cd /var/lib/mysql mv auto.cnf auto.cnf.bak systemctl restart mysqld

image_11x.png

上图为再次执行“START GROUP_REPLICATION …”之后日志的输出,已无异常信息。

参考

[1] MySQL :: MySQL 8.0 Reference Manual :: 18 Group Replication

[2] MySQL 8.0 高可用之MGR(组复制)介绍

[3] MySQL8.0登录提示caching_sha2_password问题


Tank 2022.2.23

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论