前言:最近出去玩了一段时间,博客很久没有更新了,感觉有点荒废了,接下来会给大家带来一系列的MGR相关的文章,欢迎大家阅读指点。
一、安装MySQL
标准化安装MySQL,这里就不多说了,可以参考本人之前的文章,注意一下参数文件这里有些不一样了。
#my.cnf
[client]
port = 3306
socket = /tmp/mysql3306.sock
[mysql]
prompt="\\u@\\h:\\p [\\d]>
#pager="less -i -n -S"
#tee=/home/mysql/query.log
no-auto-rehash
[mysqld]
#misc
user = mysql
basedir = /usr/local/mysql
datadir = /data/mgr/mysql3306/data
port = 3306
socket = /tmp/mysql3306.sock
event_scheduler = 0
tmpdir=/data/mgr/mysql3306/tmp
#timeout
interactive_timeout = 300
wait_timeout = 300
#character set
character-set-server = utf8
open_files_limit = 65535
max_connections = 100
max_connect_errors = 100000
#
explicit_defaults_for_timestamp
#logs
log-output=file
slow_query_log = 1
slow_query_log_file = slow.log
log-error = error.log
log_error_verbosity=3
pid-file = mysql.pid
long_query_time = 1
#log-slow-admin-statements = 1
#log-queries-not-using-indexes = 1
log-slow-slave-statements = 1
#binlog
binlog_format = row
log-bin = /data/mgr/mysql3306/logs/mysql-bin
binlog_cache_size = 1M
max_binlog_size = 200M
max_binlog_cache_size = 2G
sync_binlog = 0
expire_logs_days = 10
#group replication
server_id=1013306
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
binlog_format=ROW
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="3db33b36-0e51-409f-a61d-c99756e90155"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address= "172.28.128.102:23306"
loose-group_replication_group_seeds= "172.28.128.101:23306,172.28.128.102:23306,172.28.128.103:23306"
loose-group_replication_bootstrap_group= off
loose-group_replication_single_primary_mode=off
loose-group_replication_enforce_update_everywhere_checks=on
#relay log
skip_slave_start = 1
max_relay_log_size = 500M
relay_log_purge = 1
relay_log_recovery = 1
#slave-skip-errors=1032,1053,1062
#buffers & cache
table_open_cache = 2048
table_definition_cache = 2048
table_open_cache = 2048
max_heap_table_size = 96M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 256
query_cache_size = 0
query_cache_type = 0
query_cache_limit = 256K
query_cache_min_res_unit = 512
thread_stack = 192K
tmp_table_size = 96M
key_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 32M
#myisam
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
#innodb
innodb_buffer_pool_size = 100M
innodb_buffer_pool_instances = 1
innodb_data_file_path = ibdata1:100M:autoextend
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_file_per_table = 1
innodb_rollback_on_timeout
innodb_status_file = 1
innodb_io_capacity = 2000
transaction_isolation = READ-COMMITTED
innodb_flush_method = O_DIRECT
这里尤其需要注意的是group replication部分的参数
server_id=1013306
设置每个节点的serverid
gtid_mode=ON
开启gtid模式
enforce_gtid_consistency=ON
强制gtid复制一致性
master_info_repository=TABLE
复制信息保存在innodb表里
relay_log_info_repository=TABLE
binlog_checksum=NONE
mgr不支持binlog_checksum
log_slave_updates=ON
开启slave复制记录binlog
binlog_format=ROW
binlog模式设置为row
loose-group_replication_group_name=”3db33b36-0e51-409f-a61d-c99756e90155”
初始mgr group name
loose-group_replication_start_on_boot=off
实例启动时开启group replication
loose-group_replication_local_address= “172.28.128.102:23306”
本地group replication地址
loose-group_replication_group_seeds= “172.28.128.101:23306,172.28.128.102:23306,172.28.128.103:23306”
设置group_seeds
loose-group_replication_bootstrap_group= off
只在第一个节点设置,集群启动后需要将该参数关闭
loose-group_replication_single_primary_mode=off #开启多主模式
二、初始化第一个节点
/usr/local/mysql/bin/mysqld --defaults-file=/data/mgr/mysql3306/my3306.cnf --user=mysql &
set sql_log_bin=0;
alter user user() identified by 'xuclxucl';
create user 'repl'@'%' identified by 'repl4slave';
grant replication slave on *.* to 'repl'@'%';
set sql_log_bin=1;
change master to master_user='repl',master_password='repl4slave' for channel 'group_replication_recovery';
install plugin group_replication soname 'group_replication.so';
set global group_replication_bootstrap_group=on;
start group_replication;
这里要注意的是group replication的channel name是固定的
查看集群状态
root@localhost [performance_schema]>select * from replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 948335e8-64ad-11e8-86e7-080027de0e0e | zst1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
可以看到节点1已经正常
三、初始化节点2、节点3
节点2、节点3初始化步骤跟节点1初始化步骤唯一的区别就是不需要把group_replication_bootstrap_group设置为on
#第二个节点初始化
set sql_log_bin=0;
alter user user() identified by 'xuclxucl';
create user 'repl'@'%' identified by 'repl4slave';
grant replication slave on *.* to 'repl'@'%';
set sql_log_bin=1;
change master to master_user='repl',master_password='repl4slave' for channel 'group_replication_recovery';
install plugin group_replication soname 'group_replication.so';
start group_replication;
select * from p_s.replication_group_members;
初始化后查看集群状态
root@localhost [performance_schema]>select * from replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 948335e8-64ad-11e8-86e7-080027de0e0e | zst1 | 3306 | ONLINE |
| group_replication_applier | c516f6d9-64af-11e8-9952-080027de0e0e | zst2 | 3306 | ONLINE |
| group_replication_applier | c8bbdc5f-64af-11e8-b469-080027de0e0e | zst3 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
可以看到节点2节点3成功加入到集群当中
我们看下节点3的日志
2018-05-31T09:25:20.056511Z 4 [Note] Plugin group_replication reported: 'Group communication SSL configuration: group_replication_ssl_mode: "DISABLED"'
2018-05-31T09:25:20.056546Z 4 [Note] Plugin group_replication reported: '[GCS] Added automatically IP ranges 10.0.2.15/24,127.0.0.1/8,172.28.128.103/24 to the whitelist'
2018-05-31T09:25:20.056546Z 4 [Note] Plugin group_replication reported: '[GCS] SSL was not enabled'
2018-05-31T09:25:20.056546Z 4 [Note] Plugin group_replication reported: 'Initialized group communication with configuration: group_replication_group_name: "3db33b36-0e51-409f-a61d-c99756e90155"; group_replication_local_address: "172.28.128.103:23306"; group_replication_group_seeds: "172.28.128.101:23306,172.28.128.102:23306,172.28.128.103:23306"; group_replication_bootstrap_group: false; group_replication_poll_spin_loops: 0; group_replication_compression_threshold: 1000000; group_replication_ip_whitelist: "AUTOMATIC"'
2018-05-31T09:25:20.056546Z 4 [Note] Plugin group_replication reported: 'Member configuration: member_id: 1013306; member_uuid: "c8bbdc5f-64af-11e8-b469-080027de0e0e"; single-primary mode: "false"; group_replication_auto_increment_increment: 7; '
2018-05-31T09:25:20.060811Z 6 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_applier' executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 4, master_bind=''.
2018-05-31T09:25:20.068823Z 9 [Note] Slave SQL thread for channel 'group_replication_applier' initialized, starting replication in log 'FIRST' at position 0, relay log './zst3-relay-bin-group_replication_applier.000001' position: 4
2018-05-31T09:25:20.069006Z 4 [Note] Plugin group_replication reported: 'Group Replication applier module successfully initialized!'
2018-05-31T09:25:20.069016Z 4 [Note] Plugin group_replication reported: 'auto_increment_increment is set to 7'
2018-05-31T09:25:20.069018Z 4 [Note] Plugin group_replication reported: 'auto_increment_offset is set to 1013306'
2018-05-31T09:25:20.069077Z 0 [Note] Plugin group_replication reported: 'state 0 action xa_init'
2018-05-31T09:25:20.094279Z 0 [Note] Plugin group_replication reported: 'Successfully bound to 0.0.0.0:23306 (socket=63).'
2018-05-31T09:25:20.094279Z 0 [Note] Plugin group_replication reported: 'Successfully set listen backlog to 32 (socket=63)!'
2018-05-31T09:25:20.094279Z 0 [Note] Plugin group_replication reported: 'Successfully unblocked socket (socket=63)!'
2018-05-31T09:25:20.094291Z 0 [Note] Plugin group_replication reported: 'Ready to accept incoming connections on 0.0.0.0:23306 (socket=63)!'
2018-05-31T09:25:20.094336Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094432Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 64'
2018-05-31T09:25:20.094537Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094558Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 66'
2018-05-31T09:25:20.094759Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094784Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 61'
2018-05-31T09:25:20.094841Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094858Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 69'
2018-05-31T09:25:20.094912Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094929Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 71'
2018-05-31T09:25:20.094981Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.103 23306'
2018-05-31T09:25:20.094998Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.103 23306 fd 73'
2018-05-31T09:25:20.095053Z 0 [Note] Plugin group_replication reported: 'connecting to 172.28.128.101 23306'
2018-05-31T09:25:20.096464Z 0 [Note] Plugin group_replication reported: 'client connected to 172.28.128.101 23306 fd 75'
2018-05-31T09:25:21.102554Z 0 [Note] Plugin group_replication reported: 'state 4337 action xa_snapshot'
2018-05-31T09:25:21.102792Z 0 [Note] Plugin group_replication reported: 'new state x_recover'
2018-05-31T09:25:21.102797Z 0 [Note] Plugin group_replication reported: 'state 4357 action xa_complete'
2018-05-31T09:25:21.102861Z 0 [Note] Plugin group_replication reported: 'new state x_run'
2018-05-31T09:25:22.110292Z 12 [Note] Plugin group_replication reported: 'Establishing group recovery connection with a possible donor. Attempt 1/10'
2018-05-31T09:25:22.110978Z 0 [Note] Plugin group_replication reported: 'Group membership changed to zst1:3306, zst2:3306, zst3:3306 on view 15277565398765121:5.'
2018-05-31T09:25:22.117168Z 12 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_recovery' executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='zst2', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''.
2018-05-31T09:25:22.128087Z 12 [Note] Plugin group_replication reported: 'Establishing connection to a group replication recovery donor c516f6d9-64af-11e8-9952-080027de0e0e at zst2 port: 3306.'
2018-05-31T09:25:22.128251Z 13 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-05-31T09:25:22.128631Z 14 [Note] Slave SQL thread for channel 'group_replication_recovery' initialized, starting replication in log 'FIRST' at position 0, relay log './zst3-relay-bin-group_replication_recovery.000001' position: 4
2018-05-31T09:25:22.129166Z 13 [Note] Slave I/O thread for channel 'group_replication_recovery': connected to master 'repl@zst2:3306',replication started in log 'FIRST' at position 4
2018-05-31T09:25:22.146993Z 12 [Note] Plugin group_replication reported: 'Terminating existing group replication donor connection and purging the corresponding logs.'
2018-05-31T09:25:22.147900Z 13 [Note] Slave I/O thread killed while reading event for channel 'group_replication_recovery'
2018-05-31T09:25:22.147912Z 13 [Note] Slave I/O thread exiting for channel 'group_replication_recovery', read up to log 'mysql-bin.000004', position 1406
2018-05-31T09:25:22.153890Z 12 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_recovery' executed'. Previous state master_host='zst2', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='<NULL>', master_port= 0, master_log_file='', master_log_pos= 4, master_bind=''.
2018-05-31T09:25:22.161932Z 0 [Note] Plugin group_replication reported: 'This server was declared online within the replication group'
四、集群测试
node1:
root@localhost [performance_schema]>create database xucl;
Query OK, 1 row affected (0.01 sec)
root@localhost [performance_schema]>use xucl;
Database changed
root@localhost [xucl]>create table t(id int primary key);
Query OK, 0 rows affected (0.01 sec)
root@localhost [xucl]>insert into t values(1);
Query OK, 1 row affected (0.01 sec)
root@localhost [xucl]>insert into t values(2);
Query OK, 1 row affected (0.00 sec)
node2:
root@localhost [performance_schema]>select * from xucl.t;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
node3:
root@localhost [performance_schema]>select * from xucl.t;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
看到复制没有问题,本次MGR搭建已完成
五、错误处理
在初始化节点2的时候出现了,节点2状态一直处于recovering的状态,因为我的集群是新的集群,一直处于recovering的状态肯定是不正常的,在节点1看error日志看不出什么异常,但是节点2的error日志就给出了一些提示
root@localhost [performance_schema]>select * from replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 948335e8-64ad-11e8-86e7-080027de0e0e | zst1 | 3306 | ONLINE |
| group_replication_applier | c516f6d9-64af-11e8-9952-080027de0e0e | zst2 | 3306 | RECOVERING |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
2018-05-31T09:08:20.681617Z 0 [Note] Plugin group_replication reported: 'Group membership changed to zst1:3306, zst2:3306 on view 15277565398765121:2.'
2018-05-31T09:08:20.686706Z 13 [Note] 'CHANGE MASTER TO FOR CHANNEL 'group_replication_recovery' executed'. Previous state master_host='', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''. New state master_host='zst1', master_port= 3306, master_log_file='', master_log_pos= 4, master_bind=''.
2018-05-31T09:08:20.695737Z 13 [Note] Plugin group_replication reported: 'Establishing connection to a group replication recovery donor 948335e8-64ad-11e8-86e7-080027de0e0e at zst1 port: 3306.'
2018-05-31T09:08:20.695874Z 14 [Warning] Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2018-05-31T09:08:20.696181Z 15 [Note] Slave SQL thread for channel 'group_replication_recovery' initialized, starting replication in log 'FIRST' at position 0, relay log './zst2-relay-bin-group_replication_recovery.000001' position: 4
2018-05-31T09:08:20.709183Z 14 [ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'repl@zst1:3306' - retry-time: 60 retries: 1, Error_code: 2005
2018-05-31T09:08:20.709205Z 14 [Note] Slave I/O thread for channel 'group_replication_recovery' killed while connecting to master
2018-05-31T09:08:20.709214Z 14 [Note] Slave I/O thread exiting for channel 'group_replication_recovery', read up to log 'FIRST', position 4
2018-05-31T09:08:20.709375Z 13 [ERROR] Plugin group_replication reported: 'There was an error when connecting to the donor server. Please check that group_replication_recovery channel credentials and all MEMBER_HOST column values of performance_schema.replication_group_members table are correct and DNS resolvable.'
2018-05-31T09:08:20.709383Z 13 [ERROR] Plugin group_replication reported: 'For details please check performance_schema.replication_connection_status table and error log messages of Slave I/O for channel group_replication_recovery.'
看错误提示猜测是因为本地host没有配置的问题,本地host配置后重启group replication,复制正常了。
127.0.0.1 zst2 zst2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.28.128.101 zst1
172.28.128.102 zst2
172.28.128.103 zst3