2020-07-29
MGR多主模式搭建
MGR是MySQL推出的新的MySQL集群模式,它解决了原来主从复制模式存在的问题,具备故障快速转移能力,集群中的每个server都拥有集群的全部数据。MGR分为单主模式和多主模式,本次要搭建的是后者。
搭建环境:
Windows 10
MySQL版本:8.0.17
1.创建配置文件
=================my.ini============
[client]
port = 3306
socket = D:/MySQL/group/data1/mysql/run/mysql.sock
[mysqld]
port = 3306
socket = D:/MySQL/group/data1/mysql/run/mysql.sock
pid_file = D:/MySQL/group/data1/mysql/run/mysql.pid
datadir = D:/MySQL/group/data1/mysql/data
basedir= "D:/MySQL/MySQL Server 8.0/"
relay-log = relay-log
default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535
#skip-name-resolve # 注释掉是为了允许localhost登录
skip-host-cache
lower_case_table_names=1
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 2048M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0
key_buffer_size = 64M
log-error = D:/MySQL/group/data1/mysql/log/mysql_error.log
log-bin = D:/MySQL/group/data1/mysql/binlogs/mysql-bin
slow_query_log = 1
slow_query_log_file = D:/MySQL/group/data1/mysql/log/mysql_slow_query.log
long_query_time = 5
tmp_table_size = 32M
max_heap_table_size = 32M
#query_cache_type = 0
#query_cache_size = 0
secure-file-priv= NULL
#MGR
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
#每个id都必须唯一的
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.dll
plugin_load_add=mysql_clone.dll
transaction_write_set_extraction=XXHASH64
#必须是一个有效的UUID, 可以使用数据库的SELECT UUID()获取一个有效的UUID
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
#是否启动时就启动集群复制
group_replication_start_on_boot=OFF
group_replication_local_address= "127.0.0.1:33066"
group_replication_group_seeds= "127.0.0.1:33066,127.0.0.1:33067,127.0.0.1:33068,127.0.0.1:33069"
group_replication_ip_whitelist="127.0.0.1"
#指示插件是否引导组
group_replication_bootstrap_group=OFF
#多组模式
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
#插件配置
mysqlx_port = 33064
mysqlx_socket=D:/MySQL/group/data1/mysql/run/xplugin.sock
=================my.ini end============
2.初始化数据库
bin\mysqld.exe --defaults-file="D:\MySQL\group\data1\mysql\etc\my.ini" --initialize --user=mysql
3.启动数据库服务
bin\mysqld.exe --defaults-file="D:\MySQL\group\data1\mysql\etc\my.ini"
4.使用临时密码登录数据库
mysql -S D:\MySQL\group\data1\mysql\run\mysql.sock -uroot -P3306 -p
#初始化数据库后可以在error日志行“A temporary password is generated for ... ”下找到自动创建临时的root密码。
5.首次登录必须修改root密码后才能使用数据库
ALTER USER USER() IDENTIFIED BY '123456';
6.创建一个用于同步复制的user
#所有server上都执行
#如果SQL_LOG_BIN=1则记录操作过程,自动同步到其它节点
#查看系统变量:show variables like '%log_bin%';
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='123456' FOR CHANNEL 'group_replication_recovery';
mysql> SET SQL_LOG_BIN=1;
7.配置MGR
#如果配置文件中没有配置需要安装的插件,需要手动安装插件
#plugin_load_add=group_replication.so
#plugin_load_add=mysql_clone.so
#连接到服务执行:INSTALL PLUGIN group_replication SONAME 'group_replication.so';
#检查插件安装
#mysql> SHOW PLUGINS;
#第一个主节点,引导创建组
#mysql> reset master;
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
#剩余其它节点
#mysql> reset master;
mysql> START GROUP_REPLICATION;
#查询组状态
mysql> SELECT * FROM performance_schema.replication_group_members;
#查当前节点通信地址
mysql> SELECT @@group_replication_local_address;
#创建测试表
mysql> CREATE DATABASE test;
mysql> USE test;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
mysql> INSERT INTO t1 VALUES (1, 'Luis');
mysql> SELECT * FROM t1;
mysql> SHOW BINLOG EVENTS;
mysql> SHOW DATABASES LIKE 'test';
ps: 每个server都安装上述的步骤执行,只是需要区别配置文件,并且第一个server作为引导启动的区别,即只有第一个引导server需要设置group_replication_bootstrap_group变量。
--===========================================================--
搭建注意事项:
1.若配置文件my.ini中设置了参数: skip-name-resolve,请将其注释掉。否则登录时会报类似错误:“ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server,从而导致使用“localhost”不能连接到数据库。
2.Windows下配置文件中要加载的插件改为:group_replication.dll,mysql_clone.dll。
3.若非第一个节点启动组同步复制时,报“This member has more executed transactions than those present in the group”的错误,需要执行“reset master”重置事务id。
新的server加入集群组报“error connecting to master 'rpl_user@hostname:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection. Error_code: MY-002061”的错误,要么安装插件支持,要么执行:
UPDATE mysql.user set host='%' where user='rpl_user';
FLUSH PRIVILEGES;
ALTER USER 'rpl_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password';集群同步复制搭建成功需要所有的server的同步复制用户的加密方式保持一致,所有的操作在每个server上都有权限正确执行。
6.group_replication_group_seeds的配置可以只配置部分集群成员,配置的成员的有效性是在执行START GROUP_REPLICATION时才会去验证。当前的服务会按顺序的尝试连接到列表中的每一个成员,直到成功加入集群(实际上并不是按顺序连接到第一个)。因此配置的seeds必须至少有一个是有效的,否则加入集群会失败。
As no ONLINE member has the missing data for recovering in its binary logs, this member will use cloning as its recovery strategy.
参考:https://dev.mysql.com/doc/refman/8.0/en/group-replication.html