基于MySQL 8.0安装部署MGR集群(单组模式)

一、环境介绍

虚拟机:Oracle VM VirtualBox 5.0.24
操作系统:CentOS Linux release 7.7.1908 (Core)
MySQL服务器:8.0.11 MySQL Community Server - GPL
软件下载地址
master_ip:192.168.56.101
slave1_ip:192.168.56.103
slave2_ip:192.168.56.105
master主机名:mysql-101
slave1主机名:mysql-103
slave2主机名:mysql-105
master配置文件:/etc/my.cnf

[client]
default-character-set=utf8
socket=/tmp/mysql.sock

[mysqld]
user = mysql 
port = 3306
basedir=/usr/local/mysql/mysql-8.0.11/
datadir=/data/mysql/
socket=/tmp/mysql.sock
#pid-file/tmp/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names = 1

server-id = 183306 
log-bin = mysql-bin
log-bin-index = binlogs.index
binlog_format = row
binlog_rows_query_log_events = on
binlog_checksum = none

slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 4
slave_preserve_commit_order = 1

#GITD
gtid_mode = on
enforce_gtid_consistency = 1
log-slave-updates = 1
binlog_gtid_simple_recovery = 1

relay_log = /data/mysql/relay.log
relay-log-index = /data/mysql/relay.index
master_info_repository = table
relay_log_info_repository = table

#MGR
#GR配置项 基中loose前缀表示若group Replication plugin未加载 mysql server仍明治维新启动
plugin_load="group_replication=group_replication.so"
transaction_write_set_extraction = XXHASH64 #对每个事务获取write set,并且用XXHASH64算法获取hash值
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #组名,此处可拿select.uuid()生成
loose-group_replication_start_on_boot = off #在mysqld启动时不自动启动组复制
loose-group_replication_local_address = "192.168.56.101:33006" #本节点的IP地址和端口,注意该端口是组内成员之间通信的端口,而不是MySQL对外提供服务的端口
#种子节点的IP和端口号,新成员加入到集群的时候需要联系种子节点,启动集群的节点不使用下面该选项
#loose-group_replication_group_seeds = "192.168.56.101:33006,192.168.56.103:33006,192.168.56.105:33006" 
loose-group_replication_bootstrap_group = off #关闭,如果打开会造成脑裂 #是否启动集群,注意,该选项任何时候只能用于一个节点,通常情况下启动集群的时候使用,启动之后需要关闭该选项
loose-group_replication_member_weight = 50 #权重选择

skip-host-cache
skip-name-resolve
skip-external-locking
character-set-server=utf8
lower_case_table_names=1
event_scheduler=on
log_bin_trust_function_creators=on
#max_connections = 3000  
external-locking = FALSE  
#max_allowed_packet = 32M  
#sort_buffer_size = 8M  
#join_buffer_size = 2M   
thread_cache_size = 300 
#query_cache_size = 64M 
#query_cache_limit = 4M  
#read_rnd_buffer_size = 8M
#innodb_buffer_pool_size = 100M
#innodb_log_file_size = 128M
innodb_log_files_in_group = 2
#innodb_log_buffer_size = 2M
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1

[mysqld_safe]
log-error=/data/mysql/mysqld.log

slave1 配置文件:/etc/my.cnf

[client]
default-character-set=utf8
socket=/tmp/mysql-8-0-11.sock

[mysqld]
user = mysql 
port = 3306
basedir=/data/mysql/mysql-8.0.11/
datadir=/data/mysql/mysql8.0_data/
socket=/tmp/mysql-8-0-11.sock
#pid-file/tmp/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names = 1
server-id = 183307
log-bin = mysql-bin
log-bin-index = binlogs.index
binlog_format = row
binlog_rows_query_log_events = on
binlog_checksum = none

slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 4
slave_preserve_commit_order = 1

#GITD
gtid_mode = on
enforce_gtid_consistency = 1
log-slave-updates = 1
binlog_gtid_simple_recovery = 1

relay_log = /data/mysql/mysql8.0_data/relay.log
relay-log-index = /data/mysql/mysql8.0_data/relay.index
master_info_repository = table
relay_log_info_repository = table

#MGR
#GR配置项 基中loose前缀表示若group Replication plugin未加载 mysql server仍明治维新启动
plugin_load="group_replication=group_replication.so"
transaction_write_set_extraction = XXHASH64 #对每个事务获取write set,并且用XXHASH64算法获取hash值
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #组名,此处可拿select.uuid()生成
loose-group_replication_start_on_boot = off #在mysqld启动时不自动启动组复制
loose-group_replication_local_address = "192.168.56.103:33006" #本节点的IP地址和端口,注意该端口是组内成员之间通信的端口,而不是MySQL对外提供服务的端口
#种子节点的IP和端口号,新成员加入到集群的时候需要联系种子节点,启动集群的节点不使用下面该选项
loose-group_replication_group_seeds = "192.168.56.101:33006,192.168.56.103:33006,192.168.56.105:33006" 
loose-group_replication_bootstrap_group = off #关闭,如果打开会造成脑裂 #是否启动集群,注意,该选项任何时候只能用于一个节点,通常情况下启动集群的时候使用,启动之后需要关闭该选项
loose-group_replication_member_weight = 50 #权重选择

skip-host-cache
skip-name-resolve
skip-external-locking
character-set-server=utf8
lower_case_table_names=1
event_scheduler=on
log_bin_trust_function_creators=on
#max_connections = 3000  
external-locking = FALSE  
#max_allowed_packet = 32M  
#sort_buffer_size = 8M  
#join_buffer_size = 2M   
thread_cache_size = 300 
#query_cache_size = 64M 
#query_cache_limit = 4M  
#read_rnd_buffer_size = 8M
#innodb_buffer_pool_size = 100M
#innodb_log_file_size = 128M
innodb_log_files_in_group = 2
#innodb_log_buffer_size = 2M
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1

[mysqld_safe]
log-error=/data/mysql/mysql8.0_data/mysqld.log

slave2 配置文件:/etc/my.cnf

[client]
default-character-set=utf8
socket=/tmp/mysql-8-0-11.sock

[mysqld]
user = mysql 
port = 3306
basedir=/data/mysql/mysql-8.0.11/
datadir=/data/mysql/mysql8.0_data/
socket=/tmp/mysql-8-0-11.sock
#pid-file/tmp/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names = 1
server-id = 183308
log-bin = mysql-bin
log-bin-index = binlogs.index
binlog_format = row
binlog_rows_query_log_events = on
binlog_checksum = none
slave-parallel-type = LOGICAL_CLOCK
slave-parallel-workers = 4
slave_preserve_commit_order = 1

#GITD
gtid_mode = on
enforce_gtid_consistency = 1
log-slave-updates = 1
binlog_gtid_simple_recovery = 1
relay_log = /data/mysql/mysql8.0_data/relay.log
relay-log-index = /data/mysql/mysql8.0_data/relay.index
master_info_repository = table
relay_log_info_repository = table

#MGR
#GR配置项 基中loose前缀表示若group Replication plugin未加载 mysql server仍明治维新启动
plugin_load="group_replication=group_replication.so"
transaction_write_set_extraction = XXHASH64 #对每个事务获取write set,并且用XXHASH64算法获取hash值
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #组名,此处可拿select.uuid()生成
loose-group_replication_start_on_boot = off #在mysqld启动时不自动启动组复制
loose-group_replication_local_address = "192.168.56.105:33006" #本节点的IP地址和端口,注意该端口是组内成员之间通信的端口,而不是MySQL对外提供服务的端口
#种子节点的IP和端口号,新成员加入到集群的时候需要联系种子节点,启动集群的节点不使用下面该选项
loose-group_replication_group_seeds = "192.168.56.101:33006,192.168.56.103:33006,192.168.56.105:33006" 
loose-group_replication_bootstrap_group = off #关闭,如果打开会造成脑裂 #是否启动集群,注意,该选项任何时候只能用于一个节点,通常情况下启动集群的时候使用,启动之后需要关闭该选项
loose-group_replication_member_weight = 50 #权重选择

skip-host-cache
skip-name-resolve
skip-external-locking
character-set-server=utf8
lower_case_table_names=1
event_scheduler=on
log_bin_trust_function_creators=on
#max_connections = 3000  
external-locking = FALSE  
#max_allowed_packet = 32M  
#sort_buffer_size = 8M  
#join_buffer_size = 2M   
thread_cache_size = 300 
#query_cache_size = 64M 
#query_cache_limit = 4M  
#read_rnd_buffer_size = 8M
#innodb_buffer_pool_size = 100M
#innodb_log_file_size = 128M
innodb_log_files_in_group = 2
#innodb_log_buffer_size = 2M
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1

[mysqld_safe]
log-error=/data/mysql/mysql8.0_data/mysqld.log

二、关闭防火墙

执行服务器:所有Linux服务器

1、关闭防火墙服务

① 查看防火墙状态命令

systemctl status firewalld.service

注:如下面绿色字体 active (running) 表示防火墙是开启状态
在这里插入图片描述

② 关闭防火墙并查看防火墙状态

systemctl stop firewalld.service 
systemctl status firewalld.service

可以看到,inactive (dead) 的字样,说明防火墙已经关闭
基于MySQL 8.0安装部署MGR集群(单组模式)_第1张图片
注:前面的方法,一旦重启操作系统,防火墙就自动开启。

③ 禁止防火墙服务重启(永久关闭)

systemctl disable firewalld.service

执行成功,返回结果
在这里插入图片描述

2、关闭防火墙SELinux

① 修改selinux配置文件

使用vim /etc/sysconfig/selinux,将SELINUX=enforcing改为SELINUX=disabled。

② 修改完成后生效

重启计算机 reboot 或 init 6

③ 查看修改后状态

[root@wangbin ~]# sestatus
SELinux status:                 disabled

注:SELinux status 的值为 disabled 时表示 selinux 防火墙关闭。

三、Linux环境准备

三台Linux服务器都必须进行下面的环境准备

1、配置IP与主机名映射

编辑三台Linux服务器 /etc/hosts 文件,添加内容如下:

192.168.56.101 mysql-101
192.168.56.103 mysql-103
192.168.56.105 mysql-105

四、部署MGR集群

1、主库(192.168.56.101 mysql-101)执行

① 创建用于复制的用户
set sql_log_bin=0;
create user repuser@'%' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'%';
create user repuser@'127.0.0.1' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'127.0.0.1';
create user repuser@'localhost' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'localhost';
set sql_log_bin=1;
② 建个群(官方点的说法就是初始化一个复制组)
set global group_replication_bootstrap_group=on;
start group_replication;
set global group_replication_bootstrap_group=off;

③ 查看复制配置状态

select * from performance_schema.replication_group_member_stats\G
select * from performance_schema.replication_group_members;

2、从库执行

以下步骤在所有从主机上的mysql中执行

① 从库Linux服务器远程连接一次主库MySQL

我部署主从和部署MGR集群的时候这一步必须执行,不然会报错

mysql -urepuser -prepuser123 -h 192.168.56.101 -P 3306
② 创建用于复制的用户
set sql_log_bin=0;
create user repuser@'%' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'%';
create user repuser@'127.0.0.1' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'127.0.0.1';
create user repuser@'localhost' identified by 'repuser123';
grant replication slave,replication client on *.* to repuser@'localhost';
set sql_log_bin=1;
③ 节点数据同步,并坚持同步状态

如果数据量比较大的话这一步时间会比较长

change master to master_user='repuser',master_password='repuser123' for channel 'group_replication_recovery';
show slave status for channel 'group_replication_recovery';
④ 启动组复制
reset master;
start group_replication;
⑤ 查看组复制状态:
select * from performance_schema.replication_group_member_stats\G
select * from  performance_schema.replication_group_members\G

五、检查只读状态:read_only的值。

主库为OFF,从库为ON:

show variables like 'read_only';

你可能感兴趣的:(MySQL)