MySQL Group Replication

Posted Jandison

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL Group Replication相关的知识,希望对你有一定的参考价值。

group replication是一种全新的高可用,高扩张的mysql集群服务。
高一致性,基于原生复制及paxos协议的组复制技术,以插件方式提供一致数据安全保证;
高容错性,大多数服务正常就可继续工作,自动不同节点检测资源征用冲突,按顺序优先处理,内置自动防脑裂机制;
高扩展性,自动添加移除节点,并更新组信息;
高灵活性,单主模式和多主模式。单主模式自动选主,所有更新操作在主进行;多主模式,所有server同时更新。

0,修改hosts
vim /etc/hosts
localhost 127.0.0.1

1,实验环境
M1:127.0.0.1 3307
M2:127.0.0.1 3308

2,安装mysql
/usr/local/mysql57/bin/mysqld --initialize-insecure --user=dba --basedir=/usr/local/mysql57 --datadir=/data1/mysql3307
/usr/local/mysql57/bin/mysqld --initialize-insecure --user=dba --basedir=/usr/local/mysql57 --datadir=/data1/mysql3308

3,配置
M1:
[mysqld]

# GENERAL con#
user = dba
port = 3307
default_storage_engine = InnoDB
socket = /tmp/mysql3307.sock
pid_file = /data1/mysql3307/mysql.pid

# SAFETY #
max_allowed_packet = 64M
max_connect_errors = 1000000

# DATA STORAGE #binlog-format
datadir = /data1/mysql3307/

# BINARY LOGGING #
log_bin = /data1/mysql3307/3307-binlog
expire_logs_days = 10
#sync_binlog = 1
relay-log= /data1/mysql3307/3307-relaylog
#replicate-wild-do-table=hostility_url.%
#replicate-wild-do-table=guards.%

# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 1
query_cache_size = 0
max_connections = 5000
#max_user_connections = 200
thread_cache_size = 512
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 4096
wait_timeout=7500
interactive_timeout=7500
binlog-format=row
character-set-server=utf8
skip-name-resolve
skip-character-set-client-handshake
back_log=1024

# INNODB #
#innodb_flush_method = O_DIRECT
innodb_data_home_dir = /data1/mysql3307/
#innodb_data_file_path = ibdata1:1G:autoextend
innodb_log_group_home_dir=/data1/mysql3307/
innodb_log_files_in_group = 3
innodb_log_file_size = 1G
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_file_format=Barracuda
innodb_support_xa=0


innodb_io_capacity=500
innodb_max_dirty_pages_pct=90
innodb_read_io_threads=16
innodb_write_io_threads=8
innodb_buffer_pool_instances=4
innodb_thread_concurrency=0

#GTID

#gtid_mode = on
#enforce_gtid_consistency = on


# LOGGING #
log_error = /data1/mysql3307/error.log
#log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /data1/mysql3307/mysql-slow.log
long_query_time=0.05
server_id=3307
innodb_buffer_pool_size = 1G
report-host=localhost #此处的localhost一定要存在与hosts文件里,即:localhost 127.0.0.1
report-port=3307
log_slave_updates = on

#group replication
gtid_mode = on
enforce_gtid_consistency = on
master_info_repository = table
relay_log_info_repository = table
binlog_checksum=NONE
log_slave_updates=ON

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "localhost:24901"

#该参数可动态修改: set global group_replication_group_seeds="localhost:24901,localhost:24902,localhost:24093",后续写入配置文件
loose-group_replication_group_seeds= "localhost:24901,localhost:24902" 
loose-group_replication_single_primary_mode = true
loose-group_replication_bootstrap_group= off
loose-group_replication_enforce_update_everywhere_checks = false


### mysql_version=mysql57
[mysql]
prompt = \\u@\\h:\\p [\\d]>

4,启动mysql
nohup /usr/local/mysql57/bin/mysqld_safe --defaults-file=/data1/mysql3307/my3307.cnf 2>/dev/null &
mysql -uroot -S /tmp/mysql3307.sock -p123456

set sql_log_bin=0;
create user rpl_user@\'%\';
grant replication slave on *.* to rpl_user@\'%\' identified by \'rpl_pass\';
flush privileges;
set sql_log_bin=1;
change master to master_user=\'rpl_user\',master_password=\'rpl_pass\' for channel \'group_replication_recovery\';

5,启动group replication
install plugin group_replication soname \'group_replication.so\';
show plugins;
set global group_replication_bootstrap_group=on; //只需在一个实例上执行
start group_replication;
set global group_replication_bootstrap_group=off;
select * from performance_schema.replication_group_members;

6,测试(创建一个数据库,新建一个实例加入到group replication中后会自动同步到新实例)
create database test;
use test;
create table t(id int primary key auto_increment,name text);
insert into t values(1,\'Lucas\');
show binlog evnets;

7,添加一个新实例
M2:

[mysqld]

# GENERAL con#
user = dba
port = 3308
default_storage_engine = InnoDB
socket = /tmp/mysql3308.sock
pid_file = /data1/mysql3308/mysql.pid

# SAFETY #
max_allowed_packet = 64M
max_connect_errors = 1000000

# DATA STORAGE #binlog-format
datadir = /data1/mysql3308/

# BINARY LOGGING #
log_bin = /data1/mysql3308/3308-binlog
expire_logs_days = 10
#sync_binlog = 1
relay-log= /data1/mysql3308/3308relaylog
#replicate-wild-do-table=hostility_url.%
#replicate-wild-do-table=guards.%

# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 1
query_cache_size = 0
max_connections = 5000
#max_user_connections = 200
thread_cache_size = 512
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 4096
wait_timeout=7500
interactive_timeout=7500
binlog-format=row
character-set-server=utf8
skip-name-resolve
skip-character-set-client-handshake
back_log=1024

# INNODB #
#innodb_flush_method = O_DIRECT
innodb_data_home_dir = /data1/mysql3308/
#innodb_data_file_path = ibdata1:1G:autoextend
innodb_log_group_home_dir=/data1/mysql3308/
innodb_log_files_in_group = 3
innodb_log_file_size = 1G
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_file_format=Barracuda
innodb_support_xa=0


innodb_io_capacity=500
innodb_max_dirty_pages_pct=90
innodb_read_io_threads=16
innodb_write_io_threads=8
innodb_buffer_pool_instances=4
innodb_thread_concurrency=0

#GTID

#gtid_mode = on
#enforce_gtid_consistency = on


# LOGGING #
log_error = /data1/mysql3308/error.log
#log_queries_not_using_indexes = 1
slow_query_log = 1
slow_query_log_file = /data1/mysql3308/mysql-slow.log
long_query_time=0.05
server_id=3307
innodb_buffer_pool_size = 1G
report-host=localhost #此处的localhost一定要存在与hosts文件里,即:localhost 127.0.0.1
report-port=3307
log_slave_updates = on

#group replication
gtid_mode = on
enforce_gtid_consistency = on
master_info_repository = table
relay_log_info_repository = table
binlog_checksum=NONE
log_slave_updates=ON

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "localhost:24901"
loose-group_replication_group_seeds= "localhost:24901,localhost:24902"
loose-group_replication_single_primary_mode = true
loose-group_replication_bootstrap_group= off
loose-group_replication_enforce_update_everywhere_checks = false


### mysql_version=mysql57
[mysql]
prompt = \\u@\\h:\\p [\\d]>

8,启动实例 & 配置group replication
nohup /usr/local/mysql57/bin/mysqld_safe --defaults-file=/data1/mysql3308/my3308.cnf 2>/dev/null &
mysql -uroot -S /tmp/mysql3308.sock -p123456

set sql_log_bin=0;
create user rpl_user@\'%\';
grant replication slave on *.* to rpl_user@\'%\' identified by \'rpl_pass\';
flush privileges;
set sql_log_bin=1;
change master to master_user=\'rpl_user\',master_password=\'rpl_pass\' for channel \'group_replication_recovery\';

9,启动group replication
install plugin group_replication soname \'group_replication.so\';
show plugins;

set global group_replication_allow_local_disjoint_gtids_join=ON;

start group_replication;

select * from performance_schema.replication_group_members;

 

10,测试同步是否正常
/usr/local/mysql57/bin/mysql -uroot -p123456 -S /tmp/mysql3308.sock

show databases;

 

 


注意:之前在新加实例的过程中新实例一直报recovering,主要是mysql的配置中report-host未配置,或者是report-host配置的值不在系统hosts里面

 默认情况下:只有最开始配置的那台instance会设置super_read_only=off,即可以写入数据,其他后加入的都会默认为super_read_only=on,即只能读。

group replication分single-primary和multi-primary两种模式。

multi-primary模式不支持SERIALIZABLE隔离级别和外键约束。

这些限制可以通过 group_replication_enforce_update_everywhere_checks 参数来开启或关闭,single-primary模式下,该参数必须为OFF。该参数只能在group replication停止的情况下修改

下面介绍这两种模式

1,single primary

该模式下,只能有一个实例可接受写操作,组内其他成员会自动被设置为只读模式(super_read_only=on),若组内原主offline,会自动提升组内一个成员为primary,并设置super_read_only=off,接受读写操作,后续offline的主再加入进来后,会被设置成只读模式。整个切换过程全自动,无需人工干预。

选主规则:根据group中UUID字典排序;当主挂掉后,根据UUID 字典排序,选第排在最前面的UUID做新主。

2,multi primary

 

single primary 模式下,若原主挂掉,需要找出新主,如何确认primary?

SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME= \'group_replication_primary_member\';

 

以上是关于MySQL Group Replication的主要内容,如果未能解决你的问题,请参考以下文章

mysql group replication

Mysql8 group replication组复制集群单主多主模式切换

MySQL group replication介绍

MySQL Group Replication如何获取主节点ip地址

Mysql Group Replication 简析

mysql-group-replication 测试环境的搭建与排错