Group Replication:https://dev.mysql.com/doc/refman/8.0/en/group-replication.html
Single-Primary Mode:https://dev.mysql.com/doc/refman/8.0/en/group-replication-single-primary-mode.html
Multi-Primary Mode:https://dev.mysql.com/doc/refman/8.0/en/group-replication-multi-primary-mode.html
Identifier Case Sensitivity:https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.html
MySQL Yum Repository:https://dev.mysql.com/downloads/repo/yum/
[ContOS] MySQL安装部署:https://blog.csdn.net/u011424614/article/details/94555816
[Windows] MySQL安装部署:https://blog.csdn.net/u011424614/article/details/102466819
CentOS7安装部署MySQL80:https://blog.csdn.net/u011424614/article/details/132418916
CentOS基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916
机器名 | IP | 端口 |
---|---|---|
sys-cluster-01 | 192.168.249.131 | 3306、33061 |
sys-cluster-02 | 192.168.249.132 | 3306、33061 |
sys-cluster-03 | 192.168.249.133 | 3306、33061 |
# 131 执行
hostnamectl set-hostname sys-cluster-01 --static
hostnamectl set-hostname sys-cluster-01 --transient
# 132 执行
hostnamectl set-hostname sys-cluster-02 --static
hostnamectl set-hostname sys-cluster-02 --transient
# 133 执行
hostnamectl set-hostname sys-cluster-03 --static
hostnamectl set-hostname sys-cluster-03 --transient
# 查询状态
hostnamectl status
cat > /etc/hosts <
#--关闭防火墙
systemctl stop firewalld.service
#--开机禁用防火墙
systemctl disable firewalld.service
#--防火墙状态
systemctl status firewalld.service
在3 台服务器上安装MySQL
/usr/local/mysql
/var/lib/mysql
/var/log/mysql
/etc/mysql
root
密码:Root@#123456
在3 台服务器上修改配置文件
cat > /etc/my.cnf <
systemctl restart mysqld
lower_case_table_names=1
,具体报错请查看 三、报错列表 - 报错1MySQL 8.0 创建用户时,默认使用 caching_sha2_password 插件,如果未指定 SSL 密钥,启动MGR时,会导致从节点将无法连接到主节点;这里有两个解决方案:
方案一:创建用户时,改用 mysql_native_password 插件
方案二:my.cnf 配置 group_replication_recovery_get_public_key=1,指定 MySQL 使用服务器公钥进行通信
mysql -uroot -p
# 查询插件,这里关注:group_replication
# (备用)INSTALL PLUGIN group_replication SONAME 'group_replication.so';
SHOW PLUGINS;
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY 'Root@#123456';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Root@#123456' FOR CHANNEL 'group_replication_recovery';
# 备用:DELETE FROM mysql.user WHERE User='rpl_user';
# 备用:use mysql;
select user,host,plugin from mysql.user;
vim /etc/my.cnf
# 指定 MySQL 使用服务器公钥进行通信
group_replication_recovery_get_public_key=1
mysql -uroot -p
# 查询插件,这里关注:group_replication
# (备用)INSTALL PLUGIN group_replication SONAME 'group_replication.so';
SHOW PLUGINS;
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY 'Root@#123456';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Root@#123456' FOR CHANNEL 'group_replication_recovery';
# 备用:DELETE FROM mysql.user WHERE User='rpl_user';
# 备用:use mysql;
select user,host,plugin from mysql.user;
MySQL :: MySQL 8.0 Reference Manual :: 18.1.3.1 Single-Primary Mode
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION USER='rpl_user', PASSWORD='Root@#123456';
SET GLOBAL group_replication_bootstrap_group=OFF;
# 如果出现 报错3 时使用:reset master;
START GROUP_REPLICATION USER='rpl_user', PASSWORD='Root@#123456';
SELECT * FROM performance_schema.replication_group_members;
MySQL :: MySQL 8.0 Reference Manual :: 18.1.3.2 Multi-Primary Mode
stop group_replication;
set global group_replication_single_primary_mode=OFF;
set global group_replication_enforce_update_everywhere_checks=ON;
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;
select group_replication_switch_to_single_primary_mode();
create database demo;
use demo;
CREATE TABLE `sys_log` (
`id` varchar(32) NOT NULL,
`log_type` int(2) DEFAULT NULL COMMENT '日志类型(1登录日志,2操作日志)',
`log_content` varchar(1000) DEFAULT NULL COMMENT '日志内容',
`operate_type` int(2) NULL DEFAULT NULL COMMENT '操作类型',
`userid` varchar(32) DEFAULT NULL COMMENT '操作用户账号',
`username` varchar(100) DEFAULT NULL COMMENT '操作用户名称',
`ip` varchar(100) DEFAULT NULL COMMENT 'IP',
`method` varchar(500) DEFAULT NULL COMMENT '请求java方法',
`request_url` varchar(255) DEFAULT NULL COMMENT '请求路径',
`request_param` longtext COMMENT '请求参数',
`request_type` varchar(10) DEFAULT NULL COMMENT '请求类型',
`cost_time` bigint(20) DEFAULT NULL COMMENT '耗时',
`create_by` varchar(32) DEFAULT NULL COMMENT '创建人',
`create_time` datetime(0) DEFAULT NULL COMMENT '创建时间',
`update_by` varchar(32) DEFAULT NULL COMMENT '更新人',
`update_time` datetime(0) DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_sl_userid`(`userid`) USING BTREE,
INDEX `idx_sl_log_type`(`log_type`) USING BTREE,
INDEX `idx_sl_operate_type`(`operate_type`) USING BTREE,
INDEX `idx_sl_create_time`(`create_time`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统日志表' ROW_FORMAT = Dynamic;
3.写入测试数据
INSERT INTO `sys_log`(`id`, `log_type`, `log_content`, `operate_type`, `userid`, `username`, `ip`, `method`, `request_url`, `request_param`, `request_type`, `cost_time`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1588063517844226055', 2, '日志-添加', 2, 'test', '测试', '192.168.249.111', 'org.test.controller.SystemController.add()', NULL, '[{\"createBy\":\"test\",\"createTime\":1667458822057,\"id\":1"}]', NULL, 45, NULL, '2023-09-23 15:00:22', NULL, NULL);
# 查询全部数据
show databases;
# 查询数据
# 备用:use demo;
select * from demo.sys_log;
# 主服务器
STOP GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION USER='rpl_user', PASSWORD='root@#123';
SET GLOBAL group_replication_bootstrap_group=OFF;
# 从服务器
STOP GROUP_REPLICATION;
START GROUP_REPLICATION USER='rpl_user', PASSWORD='root@#123';
# 查询复制组
SELECT * FROM performance_schema.replication_group_members;
# 卸载
yum -y remove mysql mysql-server
# 删除数据目录
rm -rf /var/lib/mysql
# 安装
yum -y install mysql mysql-server
# 启动 MySQL
systemctl start mysqld
# 初始化 MySQL(配置大小写不敏感)
mysqld --initialize-insecure --lower_case_table_names=1 --user=mysql
# 不需要登陆密码
mysql -u root
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@#123456';
# 允许远程
use mysql;
update user set host='%' where user='root' and host='localhost';
flush privileges;
# (可选) 清除 MySQL 历史命令
TRUNCATE mysql.general_log;
TRUNCATE mysql.slow_log;
读写分离 :: ShardingSphere (apache.org)
ShardingSphere:ShardingSphere 是一款强大的数据库中间件,提供了多个数据库分库分表的解决方案,同时支持读写分离
mysql 8.0.34 Different lower_case_table_names settings for server ('0') and data dictionary ('1').
MySQL :: MySQL 8.0 Reference Manual :: 9.2.3 Identifier Case Sensitivity
卸载重装,初始化时,设置 lower_case_table_names=1
具体操作查看本文章 5.MySQL重置
[ERROR] [MY-011735] [Repl] Plugin group_replication reported: '[GCS] There is no local IP address matching the one configured for the local node (sys-cluster-01:33061).'
检查 my.cnf 配置文件的 loose-group_replication_local_address
检查 /etc/hosts 文件
检查主机名
[System] [MY-010597] [Repl] 'CHANGE REPLICATION SOURCE TO FOR CHANNEL 'group_replication_applier' executed'. Previous state source_host='', source_port= 3306, source_log_file='', source_log_pos= 4, source_bind=''. New state source_host='', source_port= 0, source_log_file='', source_log_pos= 4, source_bind=''.
[ERROR] [MY-011526] [Repl] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 42581b52-4d3d-11ee-8b11-246e968cedf5:1-2 > Group transactions: 3d7fdcc5-4d3d-11ee-b1ae-246e968fdebd:1-2, fd9ad78a-be73-f9cb-1e3d-a10b2c77c74e:1-2'
[ERROR] [MY-011522] [Repl] Plugin group_replication reported: 'The member contains transactions not present in the group. The member will now exit the group.'
[System] [MY-011503] [Repl] Plugin group_replication reported: 'Group membership changed to sys-cluster-01:3306, sys-cluster-02:3306 on view 16940693663611257:2.'
[System] [MY-011504] [Repl] Plugin group_replication reported: 'Group membership changed: This member has left the group.'
# 重置 bin-log 文件,会导致数据丢失,请谨慎使用
reset master;
[ERROR] [MY-011735] [Repl] Plugin group_replication reported: '[GCS] The group communication engine failed to test connectivity to the local group communication engine on sys-cluster-01:33061. This may be due to one or more invalid configuration settings. Double-check your group replication local address, firewall, SE Linux and TLS configurations and try restarting Group Replication on this server.'
[ERROR] [MY-011735] [Repl] Plugin group_replication reported: '[GCS] The member was unable to join the group. Local port: 33061'
[ERROR] [MY-011640] [Repl] Plugin group_replication reported: 'Timeout on wait for view after joining group'
[ERROR] [MY-011735] [Repl] Plugin group_replication reported: '[GCS] The member is leaving a group without being on one.'
[System] [MY-011566] [Repl] Plugin group_replication reported: 'Setting super_read_only=OFF.'
#方案一:开放通讯端口(推荐)
yum install -y policycoreutils-python
semanage port -a -t mysqld_port_t -p tcp 33061
# 方案二:关闭SELinux(不推荐)
setenforce 0
[System] [MY-013587] [Repl] Plugin group_replication reported: 'Plugin 'group_replication' is starting.'
[System] [MY-011565] [Repl] Plugin group_replication reported: 'Setting super_read_only=ON.'
[System] [MY-010597] [Repl] 'CHANGE REPLICATION SOURCE TO FOR CHANNEL 'group_replication_applier' executed'. Previous state source_host='', source_port= 0, source_log_file='', source_log_pos= 4, source_bind=''. New state source_host='', source_port= 0, source_log_file='', source_log_pos= 4, source_bind=''.
[ERROR] [MY-011516] [Repl] Plugin group_replication reported: 'There is already a member with server_uuid d2e1ef31-5501-11ee-b408-000c29006d91. The member will now exit the group.'
[System] [MY-011504] [Repl] Plugin group_replication reported: 'Group membership changed: This member has left the group.'
[System] [MY-011566] [Repl] Plugin group_replication reported: 'Setting super_read_only=OFF.'
select uuid();
vim /var/lib/mysql/auto.cnf
[auto]
server-uuid=53756d89-567c-11ee-9764-000c292be40a
systemctl restart mysqld
[System] [MY-010597] [Repl] 'CHANGE REPLICATION SOURCE TO FOR CHANNEL 'group_replication_applier' executed'. Previous state source_host='', source_port= 0, source_log_file='', source_log_pos= 5783, source_bind=''. New state source_host='', source_port= 0, source_log_file='', source_log_pos= 4, source_bind=''.
[Warning] [MY-013737] [Repl] Plugin group_replication reported: 'This member joined a group on which all members do not support member actions, as such it did reset its member configuration to the default one.'
[Warning] [MY-013783] [Repl] This member joined a group on which all members do not support replication failover channels integration on Group Replication, as such it did reset its replication failover channels configuration to the default one.
[ERROR] [MY-011529] [Repl] Plugin group_replication reported: 'The member configuration is not compatible with the group configuration. Variables such as group_replication_single_primary_mode or group_replication_enforce_update_everywhere_checks must have the same value on every server in the group. (member configuration option: [group_replication_single_primary_mode], group configuration option: [group_replication_enforce_update_everywhere_checks]).'
[System] [MY-011503] [Repl] Plugin group_replication reported: 'Group membership changed to sys-cluster-02:3306, sys-cluster-01:3306 on view 16951362302170083:2.'
[System] [MY-011504] [Repl] Plugin group_replication reported: 'Group membership changed: This member has left the group.'
报错原因是,当前MySQL未开启多主模式
set global group_replication_single_primary_mode=OFF;
set global group_replication_enforce_update_everywhere_checks=ON;
命令 | 功能 |
---|---|
systemctl start mysqld | 启动服务 |
systemctl enable mysqld | 开机自启动 |
systemctl status mysqld | 服务状态 |
systemctl restart mysqld | 重启服务 |
systemctl stop mysqld | 停止服务 |
mysql -uroot -p | 客户端登录 |
stop group_replication; | 停止复制组 |
# 创建存储目录
mkdir /opt/openssl-mysql-key
# 创建证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /opt/openssl-mysql-key/server-key.pem -out /opt/openssl-mysql-key/server-cert.pem