Mysql Group Replication(MGR) 组复制

基于Mysql 5.7.23版本,建立MGR组复制, 默认的一主多从模式

 

0、配置信息

#操作系统版本 #
[root@hostmysql-s ~]# more /etc/redhat-release
CentOS release 6.7 (Final)


#Mysql数据库版本#
mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.23-log |
+------------+
1 row in set (0.00 sec)


#三台测试服务器,ip和tcp端口及主机名如下#
服务器1:192.168.56.57:5506  hostmysql-m
服务器2:192.168.56.58:5506  hostmysql-s
服务器3:192.168.56.59:5506  hostmysql-s2


#/etc/hosts文件中加入以上三个服务器的ip和hostname 来解析#
[root@hostmysql-s ~]# vi /etc/hosts
192.168.56.57  hostmysql-m
192.168.56.58  hostmysql-s
192.168.56.59  hostmysql-s2



1、参数设置,红色为 MGR必须设置的参数
--以下参数三个服务器相同设置,注意 server_id和loose_group_replication_local_address参数要填写各自服务器信息

# basic settings #
user = mysql
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
autocommit = 0
server_id = 1        #不同服务器 设置不同#
character_set_server=utf8mb4
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
max_allowed_packet = 128M
event_scheduler = 1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
basedir=/usr
tmpdir=/tmp
port = 3306


# connection #
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 1024
max_user_connections = 1000
max_connect_errors = 1000000
back_log = 500



# table cache performance settings #
table_open_cache = 4096
table_definition_cache = 4096
table_open_cache_instances = 64



# session memory settings #
read_buffer_size = 16M
read_rnd_buffer_size = 32M
sort_buffer_size = 32M
tmp_table_size = 64M
join_buffer_size = 128M
thread_cache_size = 64



# log settings #
log_error_verbosity = 2
slow_query_log_file=/var/lib/mysql/hostmysql-m-slow.log
long_query_time = 2
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 10
log_slow_admin_statements = 1
min_examined_row_limit = 100
log_slow_slave_statements = 1
log_bin=/var/lib/mysql/mysql-bin
log_bin_index=/var/lib/mysql/mysql-bin.index
binlog_cache_size = 2M
expire_logs_days = 9
log_bin_trust_function_creators = 1




# innodb settings #
innodb_page_size = 16384
innodb_buffer_pool_size = 3G
innodb_buffer_pool_instances = 3
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 4096
innodb_lock_wait_timeout = 5
innodb_io_capacity = 10000
innodb_io_capacity_max = 20000
innodb_flush_method = O_DIRECT
#innodb_undo_logs = 128
#innodb_undo_tablespaces = 3
innodb_flush_neighbors = 0
innodb_log_file_size = 64M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 8M
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 0
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 128M
innodb_write_io_threads = 16
innodb_read_io_threads = 16
innodb_file_per_table = 1
innodb_stats_persistent = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_online_alter_log_max_size=128M
innodb_open_files=4096
innodb_max_dirty_pages_pct = 75
innodb_adaptive_flushing = 1
innodb_support_xa = 1
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 16
#innodb_undo_log_truncate = 1
#innodb_max_undo_log_size = 128M
#innodb_purge_rseg_truncate_frequency = 128




# replication settings #
gtid_mode = on                    
enforce_gtid_consistency = 1        
sync_binlog = 1
log_slave_updates = 1
binlog_format = ROW
binlog_rows_query_log_events = 1
relay_log = relay.log
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = 1
slave_skip_errors = ddl_exist_errors
slave_rows_search_algorithms = 'INDEX_SCAN,HASH_SCAN'
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers = 16
slave_preserve_commit_order = 1
slave_transaction_retries = 128




# password plugin #
validate_password_policy = STRONG
validate-password = FORCE_PLUS_PERMANENT




# perforamnce_schema settings #
performance-schema-instrument='memory/%=COUNTED'
performance_schema_digests_size = 40000
performance_schema_max_table_instances = 40000
performance_schema_max_sql_text_length = 4096
performance_schema_max_digest_length = 4096
performance_schema=1



# group replication settings #
transaction_write_set_extraction = XXHASH64
binlog_checksum = NONE # only for group replication
loose_group_replication_group_name = "d9c27b4a-1319-11e9-a475-08002733c0ea"                                  #查看UUID,用来设置组名字#
loose_group_replication_start_on_boot = 0         
loose_group_replication_bootstrap_group = 0                                                                                                 
loose_group_replication_local_address= "192.168.56.57:5506"                                                                        #设置成员的本地地址,不同的服务器 需要设置本地地址和端口#
loose_group_replication_group_seeds= "192.168.56.57:5506,192.168.56.58:5506,192.168.56.59:5506"      #设置种子成员的地址,所有服务器同一份数据#
loose_group_replication_ip_whitelist = "192.168.56.57,192.168.56.58,192.168.56.59,192.168.56.60"          #设置白名单,只有名单里面才能加入到组中#
loose_group_replication_single_primary_mode = 1                                                                                            #一主多从模式

#plugin-load = "group_replication.so;validate_password.so;semisync_master.so;semisync_slave.so"
# report_host = 127.0.0.1 # optional for group replication
#loose_group_replication = FORCE_PLUS_PERMANENT
#loose_group_replication_compression_threshold = 100
#loose_group_replication_flow_control_mode = 0
#loose_group_replication_single_primary_mode = 0
#loose_group_replication_enforce_update_everywhere_checks = 1
#loose_group_replication_transaction_size_limit = 10485760
#loose_group_replication_unreachable_majority_timeout = 120




2、在192.168.56.57:5506 服务器1上建立组

#查看UUID,用来设置组名字#
mysql> select uuid();
+--------------------------------------+
| uuid()                               |
+--------------------------------------+
| d9c27b4a-1319-11e9-a475-08002733c0ea |
+--------------------------------------+
1 row in set (0.00 sec)

#加载插件#
mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.06 sec)


#关闭binlog#
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

#创建复制用户#
mysql> create user mgruser@'%' identified by 'Mgruser@123$';
Query OK, 0 rows affected (0.00 sec)

#赋权#
mysql> grant replication slave on *.* to mgruser@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

#开启binlog#
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

#配置复制所使用的用户#
mysql>  change master to  master_user='mgruser', master_password='Mgruser@123$'  for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

#初始化特有参数,在启用第一个成员时需要做初始化工作,不要在参数文件中配置此参数,并且在初始化后设置为off#
mysql> set global group_replication_bootstrap_group=on;
Query OK, 0 rows affected (0.00 sec)

#启动组复制#
mysql> start group_replication;
Query OK, 0 rows affected (2.07 sec)

#初始化后设置为off#
mysql>  set global group_replication_bootstrap_group=off;
Query OK, 0 rows affected (0.00 sec)


--实例1的uuid
mysql>  SHOW GLOBAL VARIABLES LIKE 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 8100117b-a99d-11e8-b4d4-08002733c0ea |
+---------------+--------------------------------------+
1 row in set (0.01 sec)


--监控状态
mysql> select * from performance_schema.replication_group_members \G
*************************** 1. row ***************************
CHANNEL_NAME: group_replication_applier
   MEMBER_ID: 8100117b-a99d-11e8-b4d4-08002733c0ea
MEMBER_HOST: hostmysql-m
MEMBER_PORT: 3306
MEMBER_STATE: ONLINE
1 row in set (0.00 sec)

mysql> select * from performance_schema.replication_group_member_stats \G
*************************** 1. row ***************************
                      CHANNEL_NAME: group_replication_applier
                           VIEW_ID: 15469338165454750:1
                         MEMBER_ID: 8100117b-a99d-11e8-b4d4-08002733c0ea
       COUNT_TRANSACTIONS_IN_QUEUE: 0
        COUNT_TRANSACTIONS_CHECKED: 0
          COUNT_CONFLICTS_DETECTED: 0
COUNT_TRANSACTIONS_ROWS_VALIDATING: 0
TRANSACTIONS_COMMITTED_ALL_MEMBERS: d9c27b4a-1319-11e9-a475-08002733c0ea:1
    LAST_CONFLICT_FREE_TRANSACTION:
1 row in set (0.00 sec)

mysql> select * from performance_schema.replication_connection_status \G
*************************** 1. row ***************************
             CHANNEL_NAME: group_replication_applier
               GROUP_NAME: d9c27b4a-1319-11e9-a475-08002733c0ea
              SOURCE_UUID: d9c27b4a-1319-11e9-a475-08002733c0ea
                THREAD_ID: NULL
            SERVICE_STATE: ON
COUNT_RECEIVED_HEARTBEATS: 0
LAST_HEARTBEAT_TIMESTAMP: 0000-00-00 00:00:00
RECEIVED_TRANSACTION_SET: d9c27b4a-1319-11e9-a475-08002733c0ea:1
        LAST_ERROR_NUMBER: 0
       LAST_ERROR_MESSAGE:
     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00
1 row in set (0.00 sec)

mysql> select * from performance_schema.replication_applier_status \G
*************************** 1. row ***************************
              CHANNEL_NAME: group_replication_applier
             SERVICE_STATE: ON
           REMAINING_DELAY: NULL
COUNT_TRANSACTIONS_RETRIES: 0
1 row in set (0.00 sec)





--服务器1生成测试数据,在服务器2和3加入复制组后会自动同步binlog数据
mysql> create database mgrdb;
Query OK, 1 row affected (0.01 sec)

mysql> use mgrdb;
Database changed

mysql> create table mgrtb(c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO mgrtb VALUES (1, 'Luis');
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)



3、在192.168.56.58:5506(服务器2)和192.168.56.59:5506(服务器3) 上加入上面建立好的组中,服务器分别执行

mysql> install plugin group_replication soname 'group_replication.so';
Query OK, 0 rows affected (0.07 sec)

mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> create user mgruser@'%' identified by 'Mgruser@123$';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave  on *.* to mgruser@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)

mysql>  change master to  master_user='mgruser', master_password='Mgruser@123$'  for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start group_replication;
Query OK, 0 rows affected (5.80 sec)


--在服务器2和3上查询之前服务器1上建立的数据库和表,查询成功 说明自动拉取同步了服务器1的binlog数据
mysql> use mgrdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from mgrtb;
+----+------+
| c1 | c2   |
+----+------+
|  1 | Luis |
+----+------+
1 row in set (0.00 sec)


--实例2的uuid
mysql> SHOW GLOBAL VARIABLES LIKE 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 3d522e72-a9c1-11e8-9a70-080027db1ef2 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

--实例3的uuid
mysql> SHOW GLOBAL VARIABLES LIKE 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 348ebe16-0fe9-11e9-bfd1-080027995635 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)



4、查看组复制状态

#查看组复制状态#
mysql> select * from performance_schema.replication_group_members ;
+---------------------------+--------------------------------------+--------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST  | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+--------------+-------------+--------------+
| group_replication_applier | 348ebe16-0fe9-11e9-bfd1-080027995635 | hostmysql-s2 |        3306 | ONLINE       |
| group_replication_applier | 3d522e72-a9c1-11e8-9a70-080027db1ef2 | hostmysql-s  |        3306 | ONLINE       |
| group_replication_applier | 8100117b-a99d-11e8-b4d4-08002733c0ea | hostmysql-m  |        3306 | ONLINE       |
+---------------------------+--------------------------------------+--------------+-------------+--------------+
3 rows in set (0.00 sec)



附1:从服务器2克隆出来的服务器3的虚机,uuid一致报错及解决方案

#查看错误日志,发现uuid有重复,因为是克隆的虚机 会有这一现象#
[root@hostmysql-s2 ~]# tail -1000f /var/log/mysqld.log
2019-01-04T06:20:36.752166Z 0 [ERROR] Plugin group_replication reported: 'There is already a member with server_uuid 3d522e72-a9c1-11e8-9a70-080027db1ef2. The member will now exit the group.'

--解决方案:
把现有的uuid文件重命名
mv /var/lib/mysql/auto.cnf /var/lib/mysql/auto.cnfbak

然后重新自动生成一个auto.cnf


附2:重置master信息,测试环境(生产禁用)

mysql>  show master status;
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                                                                   |
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
| mysql-bin.000111 |      230 |              |                  | 8100117b-a99d-11e8-b4d4-08002733c0ea:1-2,
fa5aac11-0fd1-11e9-a83c-08002733c0ea:1-25 |
+------------------+----------+--------------+------------------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.03 sec)

mysql>  show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      150 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

 

参考:https://dev.mysql.com/doc/refman/5.7/en/group-replication.html

         《MySQL运维内参MySQL、Galera、Inception核心原理与最佳实践》

你可能感兴趣的:(Mysql,复制技术,Mysql—基础功能学习)