mysql8 MGR集群部署

服务器环境

系统 主机名 IP
CentOS 7.9 mgr-node01 192.168.200.31
CentOS 7.9 mgr-node02 192.168.200.32
CentOS 7.9 mgr-node03 192.168.200.33

mysql8安装

tar zxvf mysql-8.0.19-1.el7.x86_64.rpm-bundle.tar.gz

rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64

rpm -ivh mysql-community-common-8.0.19-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-libs-8.0.19-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-client-8.0.19-1.el7.x86_64.rpm --nodeps --force
rpm -ivh mysql-community-server-8.0.19-1.el7.x86_64.rpm --nodeps --force


mkdir -p /data/mysql/data /data/mysql/mysql-files /data/mysql/mysql-keyring
chown -R mysql:mysql /data/mysql
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql-files
rm -rf /var/lib/mysql-keyring
ln -s /data/mysql/data /var/lib/mysql
ln -s /data/mysql/mysql-files /var/lib/mysql-files
ln -s /data/mysql/mysql-keyring /var/lib/mysql-keyring

修改配置文件my.cnf

192.168.200.31

cat > /etc/my.cnf << EOF
[mysqld]
port=3308
default-time-zone='+08:00' 
server-id=31
log-bin=mysql-bin
binlog_format=row
default-authentication-plugin=mysql_native_password

skip-name-resolve # 禁用DNS解析,加快IP方式的连接

auto-increment-increment=1
auto-increment-offset=1

plugin_dir=/usr/lib64/mysql/plugin/

gtid-mode=on # 开启gtid模式
enforce-gtid-consistency=1 #开启gtid安全限制
report_host=192.168.200.31

#MGR使用乐观锁,所以官网建议隔离级别是RC,减少锁粒度
transaction_isolation = READ-COMMITTED
#因为集群会在故障恢复时互相检查binlog的数据,

#所以需要记录下集群内其他服务器发过来已经执行过的binlog,按GTID来区分是否执行过.
log-slave-updates = 1

#binlog校验规则,5.6之后的高版本是CRC32,低版本都是NONE,但是MGR要求使用NONE
binlog_checksum = NONE

#基于安全的考虑,MGR集群要求复制模式要改成slave记录记录到表中,不然就报错
master_info_repository = TABLE

#同上配套
relay_log_info_repository = TABLE

#组复制设置#记录事务的算法,官网建议设置该参数使用 XXHASH64 算法
transaction_write_set_extraction = XXHASH64

#primary节点到secondary节点的请求是否为基于RSA密钥对的密码交换
loose-group_replication_recovery_get_public_key=on

# 组恢复时应用ssl
loose-group_replication_recovery_use_ssl=on

#相当于此GROUP的名字,是UUID值,不能和集群内其他GTID值的UUID混用,可用uuidgen来生成一个新的,
#主要是用来区分整个内网里边的各个不同的GROUP,而且也是这个group内的GTID值的UUID
loose-group_replication_group_name = '2d837649-b362-4ed3-9aa8-9f3292b2812f'

#IP地址白名单,默认只添加127.0.0.1,不会允许来自外部主机的连接,按需安全设置
loose-group_replication_ip_whitelist = '127.0.0.1/8,192.168.200.0/24'

#是否随服务器启动而自动启动组复制,不建议直接启动,怕故障恢复时有扰乱数据准确性的特殊情况
loose-group_replication_start_on_boot = OFF

#本地MGR的IP地址和端口,host:port,是MGR的端口,不是数据库的端口
loose-group_replication_local_address = '192.168.200.31:24901'

#需要接受本MGR实例控制的服务器IP地址和端口,是MGR的端口,不是数据库的端口
loose-group_replication_group_seeds = '192.168.200.31:24901,192.168.200.32:24901,192.168.200.33:24901'

#开启引导模式,添加组成员,用于第一次搭建MGR或重建MGR的时候使用,只需要在集群内的其中一台开启,
loose-group_replication_bootstrap_group = OFF

#是否启动单主模式,如果启动,则本实例是主库,提供读写,其他实例仅提供读,如果为off就是多主模式了
loose-group_replication_single_primary_mode = ON

#多主模式下,强制检查每一个实例是否允许该操作,如果不是多主,可以关闭
loose-group_replication_enforce_update_everywhere_checks = off


datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

log-error=/var/lib/mysql/mysqld.log
slow_query_log = 0
long_query_time = 2
slow_query_log_file = /var/lib/mysql/mysql-slow.log


character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'

max_connections = 1024
max_connect_errors = 6000

open_files_limit = 10000
table_open_cache = 128

max_allowed_packet = 200M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 64
key_buffer_size = 4M
ft_min_word_len = 4

performance_schema = 0
explicit_defaults_for_timestamp=true
lower_case_table_names=1
skip-external-locking 
default-storage-engine = InnoDB 
innodb_file_per_table = 1

innodb_open_files = 500

innodb_buffer_pool_size = 6G
innodb_buffer_pool_chunk_size = 128M
innodb_buffer_pool_instances = 16

innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120 
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 7200
wait_timeout = 86400

[client]
default-character-set = utf8mb4
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set = utf8mb4

EOF

192.168.200.32

cat > /etc/my.cnf << EOF
[mysqld]
port=3308
default-time-zone='+08:00' 
server-id=32
log-bin=mysql-bin
binlog_format=row
default-authentication-plugin=mysql_native_password

skip-name-resolve # 禁用DNS解析,加快IP方式的连接

auto-increment-increment=1
auto-increment-offset=1

plugin_dir=/usr/lib64/mysql/plugin/

gtid-mode=on # 开启gtid模式
enforce-gtid-consistency=1 #开启gtid安全限制
report_host=192.168.200.32

#MGR使用乐观锁,所以官网建议隔离级别是RC,减少锁粒度
transaction_isolation = READ-COMMITTED
#因为集群会在故障恢复时互相检查binlog的数据,

#所以需要记录下集群内其他服务器发过来已经执行过的binlog,按GTID来区分是否执行过.
log-slave-updates = 1

#binlog校验规则,5.6之后的高版本是CRC32,低版本都是NONE,但是MGR要求使用NONE
binlog_checksum = NONE

#基于安全的考虑,MGR集群要求复制模式要改成slave记录记录到表中,不然就报错
master_info_repository = TABLE

#同上配套
relay_log_info_repository = TABLE

#组复制设置#记录事务的算法,官网建议设置该参数使用 XXHASH64 算法
transaction_write_set_extraction = XXHASH64

#primary节点到secondary节点的请求是否为基于RSA密钥对的密码交换
loose-group_replication_recovery_get_public_key=on

# 组恢复时应用ssl
loose-group_replication_recovery_use_ssl=on

#相当于此GROUP的名字,是UUID值,不能和集群内其他GTID值的UUID混用,可用uuidgen来生成一个新的,
#主要是用来区分整个内网里边的各个不同的GROUP,而且也是这个group内的GTID值的UUID
loose-group_replication_group_name = '2d837649-b362-4ed3-9aa8-9f3292b2812f'

#IP地址白名单,默认只添加127.0.0.1,不会允许来自外部主机的连接,按需安全设置
loose-group_replication_ip_whitelist = '127.0.0.1/8,192.168.200.0/24'

#是否随服务器启动而自动启动组复制,不建议直接启动,怕故障恢复时有扰乱数据准确性的特殊情况
loose-group_replication_start_on_boot = OFF

#本地MGR的IP地址和端口,host:port,是MGR的端口,不是数据库的端口
loose-group_replication_local_address = '192.168.200.32:24901'

#需要接受本MGR实例控制的服务器IP地址和端口,是MGR的端口,不是数据库的端口
loose-group_replication_group_seeds = '192.168.200.31:24901,192.168.200.32:24901,192.168.200.33:24901'

#开启引导模式,添加组成员,用于第一次搭建MGR或重建MGR的时候使用,只需要在集群内的其中一台开启,
loose-group_replication_bootstrap_group = OFF

#是否启动单主模式,如果启动,则本实例是主库,提供读写,其他实例仅提供读,如果为off就是多主模式了
loose-group_replication_single_primary_mode = ON

#多主模式下,强制检查每一个实例是否允许该操作,如果不是多主,可以关闭
loose-group_replication_enforce_update_everywhere_checks = off


datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

log-error=/var/lib/mysql/mysqld.log
slow_query_log = 0
long_query_time = 2
slow_query_log_file = /var/lib/mysql/mysql-slow.log


character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'

max_connections = 1024
max_connect_errors = 6000

open_files_limit = 10000
table_open_cache = 128

max_allowed_packet = 200M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 64
key_buffer_size = 4M
ft_min_word_len = 4

performance_schema = 0
explicit_defaults_for_timestamp=true
lower_case_table_names=1
skip-external-locking 
default-storage-engine = InnoDB 
innodb_file_per_table = 1

innodb_open_files = 500

innodb_buffer_pool_size = 6G
innodb_buffer_pool_chunk_size = 128M
innodb_buffer_pool_instances = 16

innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120 
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 7200
wait_timeout = 86400

[client]
default-character-set = utf8mb4
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set = utf8mb4

EOF

192.168.200.33

cat > /etc/my.cnf << EOF
[mysqld]
port=3308
default-time-zone='+08:00' 
server-id=33
log-bin=mysql-bin
binlog_format=row
default-authentication-plugin=mysql_native_password

skip-name-resolve # 禁用DNS解析,加快IP方式的连接

auto-increment-increment=1
auto-increment-offset=1

plugin_dir=/usr/lib64/mysql/plugin/

gtid-mode=on # 开启gtid模式
enforce-gtid-consistency=1 #开启gtid安全限制
report_host=192.168.200.33

#MGR使用乐观锁,所以官网建议隔离级别是RC,减少锁粒度
transaction_isolation = READ-COMMITTED
#因为集群会在故障恢复时互相检查binlog的数据,

#所以需要记录下集群内其他服务器发过来已经执行过的binlog,按GTID来区分是否执行过.
log-slave-updates = 1

#binlog校验规则,5.6之后的高版本是CRC32,低版本都是NONE,但是MGR要求使用NONE
binlog_checksum = NONE

#基于安全的考虑,MGR集群要求复制模式要改成slave记录记录到表中,不然就报错
master_info_repository = TABLE

#同上配套
relay_log_info_repository = TABLE

#组复制设置#记录事务的算法,官网建议设置该参数使用 XXHASH64 算法
transaction_write_set_extraction = XXHASH64

#primary节点到secondary节点的请求是否为基于RSA密钥对的密码交换
loose-group_replication_recovery_get_public_key=on

# 组恢复时应用ssl
loose-group_replication_recovery_use_ssl=on

#相当于此GROUP的名字,是UUID值,不能和集群内其他GTID值的UUID混用,可用uuidgen来生成一个新的,
#主要是用来区分整个内网里边的各个不同的GROUP,而且也是这个group内的GTID值的UUID
loose-group_replication_group_name = '2d837649-b362-4ed3-9aa8-9f3292b2812f'

#IP地址白名单,默认只添加127.0.0.1,不会允许来自外部主机的连接,按需安全设置
loose-group_replication_ip_whitelist = '127.0.0.1/8,192.168.200.0/24'

#是否随服务器启动而自动启动组复制,不建议直接启动,怕故障恢复时有扰乱数据准确性的特殊情况
loose-group_replication_start_on_boot = OFF

#本地MGR的IP地址和端口,host:port,是MGR的端口,不是数据库的端口
loose-group_replication_local_address = '192.168.200.33:24901'

#需要接受本MGR实例控制的服务器IP地址和端口,是MGR的端口,不是数据库的端口
loose-group_replication_group_seeds = '192.168.200.31:24901,192.168.200.32:24901,192.168.200.33:24901'

#开启引导模式,添加组成员,用于第一次搭建MGR或重建MGR的时候使用,只需要在集群内的其中一台开启,
loose-group_replication_bootstrap_group = OFF

#是否启动单主模式,如果启动,则本实例是主库,提供读写,其他实例仅提供读,如果为off就是多主模式了
loose-group_replication_single_primary_mode = ON

#多主模式下,强制检查每一个实例是否允许该操作,如果不是多主,可以关闭
loose-group_replication_enforce_update_everywhere_checks = off


datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysqld.pid

log-error=/var/lib/mysql/mysqld.log
slow_query_log = 0
long_query_time = 2
slow_query_log_file = /var/lib/mysql/mysql-slow.log


character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'

max_connections = 1024
max_connect_errors = 6000

open_files_limit = 10000
table_open_cache = 128

max_allowed_packet = 200M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 64
key_buffer_size = 4M
ft_min_word_len = 4

performance_schema = 0
explicit_defaults_for_timestamp=true
lower_case_table_names=1
skip-external-locking 
default-storage-engine = InnoDB 
innodb_file_per_table = 1

innodb_open_files = 500

innodb_buffer_pool_size = 6G
innodb_buffer_pool_chunk_size = 128M
innodb_buffer_pool_instances = 16

innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120 
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 7200
wait_timeout = 86400

[client]
default-character-set = utf8mb4
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set = utf8mb4

EOF
启动mysql服务
systemctl start mysqld
systemctl enable mysqld

防火墙放行(所有节点操作)

firewall-cmd --zone=public --add-port={3308,24901}/tcp --permanent&&firewall-cmd --reload

修改mysql初始账号权限(所有节点操作)

alter user 'root'@'localhost' identified with mysql_native_password by "D4vh3s1#Hmb70ppcI2Q8";
flush privileges;

创建主从同步账号(所有节点操作)

create user 'repl'@'192.168.200.%' identified with mysql_native_password by 'DS6aWh2l9NcS!E#@sJEn';

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.200.%';

reset master;

安装插件(所有节点操作)

# mysql客户端操作
install PLUGIN group_replication SONAME 'group_replication.so';


#查看插件是否安装成功
show plugins;
select * from information_schema.plugins where plugin_name = 'group_replication'\G;

MGR单主模式

在主节点启动引导
SET GLOBAL group_replication_bootstrap_group = ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group = OFF;

# 查看组复制状态
SELECT * FROM performance_schema.replication_group_members;


在从节点执行
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='DS6aWh2l9NcS!E#@sJEn' FOR CHANNEL 'group_replication_recovery';

START GROUP_REPLICATION;

#查看组复制状态
SELECT * FROM performance_schema.replication_group_members;

主挂了如何恢复

# 第一次需要如此操作
STOP GROUP_REPLICATION;

CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='DS6aWh2l9NcS!E#@sJEn' FOR CHANNEL 'group_replication_recovery';

START GROUP_REPLICATION;

# 后续恢复执行

START GROUP_REPLICATION;
相关推荐
m0_7482448333 分钟前
StarRocks 排查单副本表
大数据·数据库·python
C++忠实粉丝43 分钟前
Redis 介绍和安装
数据库·redis·缓存
wmd131643067121 小时前
将微信配置信息存到数据库并进行调用
数据库·微信
是阿建吖!1 小时前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库
凡人的AI工具箱1 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
ClouGence1 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
m0_748236581 小时前
《Web 应用项目开发:从构思到上线的全过程》
服务器·前端·数据库
苏三说技术2 小时前
Redis 性能优化的18招
数据库·redis·性能优化
Tttian6222 小时前
基于Pycharm与数据库的新闻管理系统(2)Redis
数据库·redis·pycharm
做梦敲代码3 小时前
达梦数据库-读写分离集群部署
数据库·达梦数据库