############################CentOS7.4 MySQL8.0.26-MGR配置############################
测试环境:
CentOS7.4 (2GB内存)
MySQL8.0.26 三台
hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.202 Master Centos-7.4-Master
192.168.100.203 Slave1 Centos-7.4-Slave1
192.168.100.204 Slave2 Centos-7.4-Slave2
my.cnf ##此为cnf样版,请根据各主机做对应的server_id、group_replication_group_name、group_replication_group_seeds等修改正确
#
## my.cnf for 8.0版本
## 重要:本次配置文件适用虚拟机2GB内存而配置 请勿照搬至生产环境 后果自负
## 注意:个别建议可能需要根据实际情况作调整,请自行判断或联系我,本人不对这些建议结果负相应责任
## 本配置文件主要适用于MySQL 8.0版本
#
[client]
port = 3362
socket = /usr/local/mysql/mysql.sock
[mysql]
#prompt = "\u@mysqldb \R:\m:\s [\d]> "
no_auto_rehash
[mysqld]
user = mysql
port = 3362
#主从复制或MGR集群中,server_id记得要不同
#另外,实例启动时会生成 auto.cnf,里面的 server_uuid 值也要不同
#server_uuid的值还可以自己手动指定,只要符合uuid的格式标准就可以
server_id = 3362
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /usr/local/mysql/mysql.sock
pid_file = Centos-7.4-Master.pid
character_set_server = UTF8MB4
skip_name_resolve = 1
#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"
#performance setttings
lock_wait_timeout = 3600
open_files_limit = 65535
back_log = 1024
max_connections = 512
max_connect_errors = 1000000
table_open_cache = 1024
table_definition_cache = 1024
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 768
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
#log settings
log_timestamps = SYSTEM
log_error = /usr/local/mysql/log/error/error.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
slow_query_log_file = /usr/local/mysql/log/slow/slow.log
long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_bin = /usr/local/mysql/log/mybin/mybinlog
binlog_format = ROW
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_expire_logs_seconds = 604800
#MySQL 8.0.22前,想启用MGR的话,需要设置binlog_checksum=NONE才行
binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE
#myisam settings
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
#replication settings
relay_log_recovery = 1
slave_parallel_type = LOGICAL_CLOCK
#可以设置为逻辑CPU数量的2倍
slave_parallel_workers = 64
binlog_transaction_dependency_tracking = WRITESET
slave_preserve_commit_order = 1
slave_checkpoint_period = 2
#mgr settings
loose-plugin_load_add = 'mysql_clone.so'
loose-plugin_load_add = 'group_replication.so'
loose-group_replication_group_name = "7ebc323b-2a6e-11ec-b342-000c29191da1"
#MGR本地节点IP:PORT,请自行替换
loose-group_replication_local_address = "192.168.100.202:33062"
#MGR集群所有节点IP:PORT,请自行替换
loose-group_replication_group_seeds = "192.168.100.202:33062,192.168.100.203:33063,192.168.100.204:33064"
loose-group_replication_start_on_boot = OFF
loose-group_replication_bootstrap_group = OFF
loose-group_replication_exit_state_action = READ_ONLY
loose-group_replication_flow_control_mode = "DISABLED"
loose-group_replication_single_primary_mode = ON
#innodb settings
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 1434M
innodb_buffer_pool_instances = 4
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_open_files = 65535
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_print_ddl_logs = 1
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log_error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864
#innodb monitor settings
innodb_monitor_enable = "module_innodb"
innodb_monitor_enable = "module_server"
innodb_monitor_enable = "module_dml"
innodb_monitor_enable = "module_ddl"
innodb_monitor_enable = "module_trx"
innodb_monitor_enable = "module_os"
innodb_monitor_enable = "module_purge"
innodb_monitor_enable = "module_log"
innodb_monitor_enable = "module_lock"
innodb_monitor_enable = "module_buffer"
innodb_monitor_enable = "module_index"
innodb_monitor_enable = "module_ibuf_system"
innodb_monitor_enable = "module_buffer_page"
innodb_monitor_enable = "module_adaptive_hash"
#pfs settings
performance_schema = 1
#performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'
[mysqldump]
quick
以下为官档开启MGR必要参数做参照
=============================================================================
复制框架
以下设置根据 MySQL 组复制要求配置复制。
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
组复制设置
此时,选项文件确保服务器已配置并被指示在给定配置下实例化复制基础结构。以下部分为服务器配置组复制设置。
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "s1:33061" ##此处端口不能与mysql的端口一致 需不同
group_replication_group_seeds= "s1:33061,s2:33061,s3:33061"
group_replication_bootstrap_group=off
============================================================================
上述配置文件my.cnf中已包含加载了克隆、组复制插件
loose-plugin_load_add = 'mysql_clone.so'
loose-plugin_load_add = 'group_replication.so'
# 手动加载安装MGR插件
mysql> INSTALL PLUGIN clone SONAME 'mysql_clone.so';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
一、启动三台数据库,安装MGR插件,设置复制账号(所有节点执行,默认三台安装好已设置好远程登陆)
##以上配置文件已默认加载了相应的克隆插件和组复制插件 可以执行查看确认 组复制没有要求一定要装clone有待确认改进
# 手动加载安装MGR插件
mysql>INSTALL PLUGIN group_replication SONAME 'group_replication.so';
# 检查确认
mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME in ('clone','group_replication');
+-------------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+-------------------+---------------+
| clone | ACTIVE |
| group_replication | ACTIVE |
+-------------------+---------------+
2 rows in set (0.00 sec)
# 设置复制账号 三台都操作
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER group_repl@'%' IDENTIFIED WITH mysql_native_password BY 'xxxxxx';
mysql> GRANT REPLICATION SLAVE ON *.* TO group_repl@'%' ;
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='group_repl', MASTER_PASSWORD='xxxxxx' FOR CHANNEL 'group_replication_recovery';
# 检查确认账号权限
mysql> show grants for group_repl;
+----------------------------------------------------+
| Grants for group_repl@% |
+----------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `group_repl`@`%` | |
+----------------------------------------------------+
2 rows in set (0.00 sec)
#查看账号
select user,host,authentication_string,plugin from mysql.user;
二、单主模式启动
# 配置文件中此参数已默认配置为on group_replication_single_primary_mode = ON
mysql> show variables like '%single_primary_mode';
+---------------------------------------+-------+
| Variable_name | Value |
+---------------------------------------+-------+
| group_replication_single_primary_mode | ON |
+---------------------------------------+-------+
1 row in set (0.00 sec)
## 重要:仅在其中一台上选择运行 [个人理解等同于决定了哪台为Matser]
# group_replication_bootstrap_group=ON 确保只有一个成员实际引导组,用完即关
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 查看MGR组信息
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | c967301a-2a06-11ec-8c2b-000c29191da1 | Centos-7.4-Master | 3362 | ONLINE | PRIMARY | 8.0.26 |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
1 row in set (0.00 sec)
## 两台成员上操作
mysql> START GROUP_REPLICATION;
遇到的问题案例:
##遇到最多的问题是从库事务与主库不一致 此时 reset master 如有必要则重新change master to
mysql> START GROUP_REPLICATION;
ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.
查看数据库的err日志提示如下:
2021-12-28T14:32:09.417612+08:00 0 [ERROR] [MY-011526] [Repl] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 0c8a7ec0-5998-11ec-96ca-000c294f8331:1-6, 24683f98-5998-11ec-b167-000c293c912b:1-32, 7ebc323b-2a6e-11ec-b342-000c29191da1:1-56 > Group transactions: 0c8a7ec0-5998-11ec-96ca-000c294f8331:1-6, 7ebc323b-2a6e-11ec-b342-000c29191da1:1-59'
2021-12-28T14:32:09.417706+08:00 0 [ERROR] [MY-011522] [Repl] Plugin group_replication reported: 'The member contains transactions not present in the group. The member will now exit the group.'
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (10.42 sec)
##最后查看
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 869c8920-2a35-11ec-b00e-000c298f3302 | Centos-7.4-Slave2 | 3364 | RECOVERING | SECONDARY | 8.0.26 |
| group_replication_applier | c967301a-2a06-11ec-8c2b-000c29191da1 | Centos-7.4-Master | 3362 | ONLINE | PRIMARY | 8.0.26 |
| group_replication_applier | dcd55ae1-2a36-11ec-91bd-000c298a8193 | Centos-7.4-Slave1 | 3363 | RECOVERING | SECONDARY | 8.0.26 |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
二、多主切换
# 停止组复制(所有节点执行):
mysql> stop group_replication;
mysql> set global group_replication_single_primary_mode=OFF;
mysql> set global group_replication_enforce_update_everywhere_checks=ON;
# 随便选择某个节点执行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 其他节点执行
mysql> START GROUP_REPLICATION;
# 查看组信息,所有节点的 MEMBER_ROLE 都为 PRIMARY
mysql> SELECT * FROM performance_schema.replication_group_members;
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | e8ba8690-2c35-11ec-a8a5-000c29191da1 | Centos-7.4-Master | 3362 | ONLINE | PRIMARY | 8.0.26 |
| group_replication_applier | e93d1234-2c35-11ec-adc0-000c298a8193 | Centos-7.4-Slave1 | 3363 | ONLINE | PRIMARY | 8.0.26 |
| group_replication_applier | e94f713c-2c35-11ec-9b4c-000c298f3302 | Centos-7.4-Slave2 | 3364 | ONLINE | PRIMARY | 8.0.26 |
+---------------------------+--------------------------------------+-------------------+-------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
三、切回单主模式
# 所有节点执行
stop group_replication;
set global group_replication_enforce_update_everywhere_checks=OFF;
set global group_replication_single_primary_mode=ON;
# 主节点
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# 从节点 执行
START GROUP_REPLICATION;
# 查看MGR组信息
mysql> SELECT * FROM performance_schema.replication_group_members;
# 单主模式下 从库默认开启只读
mysql> show variables like '%read_only%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | ON |
| transaction_read_only | OFF |
+-----------------------+-------+
4 rows in set (0.00 sec)