Mysql组复制配置MGR(单主)

MySQL组复制配置(单主)

– MySQL组复制 mysql5.7.17,组内N/2+1个节点收到relay log,master才commit
– 2018年7月29日,18:00:42

-- 1 部署
-- 1.1 安装(此处用的是二进制包安装)
-- Windows单机部署3个实例(服务端口3306,3307,3308),版本:MySQL 5.7.17

-- 1.2 创建数据目录及日志目录:
mkdir -p D:\mysql\mgr\3306\data
mkdir -p D:\mysql\mgr\3306\logs
mkdir -p D:\mysql\mgr\3307\data
mkdir -p D:\mysql\mgr\3307\logs
mkdir -p D:\mysql\mgr\3308\data
mkdir -p D:\mysql\mgr\3308\logs

mkdir -p /data/mysql57/mysql3306
mkdir -p /data/mysql57/mysql3307
mkdir -p /data/mysql57/mysql3308
mkdir -p /data/mysql57/logs/3306
mkdir -p /data/mysql57/logs/3307
mkdir -p /data/mysql57/logs/3308

-- 2.添加第一台实例:
-- 2.1 添加3306实例的配置文件:
-- windows
[mysql]
mysql = "(\u@\h) [\d]> "

[mysqld]
server-id = 3306
user = root
port = 3306
socket = D:/mysql/mgr/3306/logsmysql3306.sock
basedir = D:/mysql/mysql-5.7.17-winx64
datadir = D:/mysql/mgr/3306/data
tmpdir = D:/mysql/mgr/3306/tmp
bind-address = 0.0.0.0

log-error = D:/mysql/mgr/3306/logs/error-log
pid-file = D:/mysql/mgr/3306/data/3306.pid

log-bin = D:/mysql/mgr/3306/logs/binlog
log-bin-index = D:/mysql/mgr/3306/logs/binlog.index
relay-log = D:/mysql/mgr/3306/logs/relaylog
relay-log-index = D:/mysql/mgr/3306/logs/relaylog.index

master_info_repository = TABLE
relay_log_info_repository = TABLE
log_slave_updates = ON
binlog_checksum = NONE

binlog_format = ROW
transaction_isolation = READ-COMMITTED
gtid_mode = ON
enforce_gtid_consistency = ON
# GR 配置项 其中loose前缀表示若Group Replication plugin未加载 mysql server仍继续启动
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"    # 组名,此处可拿select uuid();生成
loose-group_replication_start_on_boot = off    # 在mysqld启动时不自动启动组复制
loose-group_replication_local_address = "127.0.0.1:24901"
loose-group_replication_group_seeds = "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"   
loose-group_replication_bootstrap_group = off   #插件不自动引导组
loose-group_replication_single_primary_mode = ON  #关闭单主模式的参数
loose-group_replication_enforce_update_everywhere_checks = OFF #开启多主模式的参数

-- 2.2 初始化3306实例
mysqld -- defaults-file=D:/mysql/mgr/3306/my.ini -- initialize-insecure

-- 2.3 配置并启动3306实例(服务):
mysqld -- install mysql3306 -- defaults-file=D:/mysql/mgr/3306/my.ini

-- 2.4 通过MySQL Client进入第一个实例(密码为空)
mysql -uroot -p -P3306

--可修改密码
mysqladmin -uroot -p -P3306 password 123asd

-- 2.5 创建复制用户与授权,并让其作为group的第一个成员:
SET SQL_LOG_BIN=0;
CREATE USER repl@'%';
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;

CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';

-- 2.6 安装GR插件:(root@localhost) [(none)]> 
INSTALL PLUGIN group_replication SONAME 'group_replication.dll';
--Linux下安装插件:group_replication.so

-- 可以检查一下插件是否安装成功:
show plugins;

SELECT * FROM information_schema.plugins WHERE PLUGIN_NAME LIKE '%group%'\G

*************************** 1. row ***************************
           PLUGIN_NAME: group_replication
        PLUGIN_VERSION: 1.0
         PLUGIN_STATUS: ACTIVE
           PLUGIN_TYPE: GROUP REPLICATION
   PLUGIN_TYPE_VERSION: 1.1
        PLUGIN_LIBRARY: group_replication.so
PLUGIN_LIBRARY_VERSION: 1.7
         PLUGIN_AUTHOR: Oracle
    PLUGIN_DESCRIPTION: Group Replication (1.0.0)
        PLUGIN_LICENSE: GPL
           LOAD_OPTION: ON
1 row in set (0.00 sec)

-- 2.7 开启第一个组复制:(root@localhost) [(none)]> 
-- 仅第一个成员启用GLOBAL group_replication_bootstrap_group=ON选项
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;

-- 启动组复制之后将group_replication_bootstrap_group设置为OFF
SET GLOBAL group_replication_bootstrap_group=OFF;

-- 检查一下组复制成员,其中member_id就是@@server_uuid的值
SELECT * FROM performance_schema.replication_group_members;

+-------------------------- -+---------- -+-------------+-------------+-------------- +
| CHANNEL_NAME              | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+-------------------------- -+---------- -+-------------+-------------+-------------- +
| group_replication_applier |           |             |        NULL | OFFLINE      |
+-------------------------- -+---------- -+-------------+-------------+-------------- +
1 row in set (0.00 sec)

+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| CHANNEL_NAME             | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y     | 3306        | ONLINE       |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
1 row in set (0.00 sec)

-- 添加测试数(任意创建表并添加一些数据,此处略):
CREATE DATABASE test;

USE test;
CREATE TABLE tb_test(id int PRIMARY KEY, name varchar(20)) CHARACTER SET utf8;
...
SELECT * FROM test.tb_test;


-- 3 添加第二个实例(3307)
-- 3.1 修改3307配置文件,将配置文件中的3306改成3307即可,并且将loose-group_replication_local_address的端口从3306改成3307:

-- 3.2 初始化3307实例:
mysqld -- defaults-file=D:/mysql/mgr/3307/my.ini -- initialize-insecure

-- 3.3 配置并启动3307实例(服务):
mysqld -- install mysql3307 -- defaults-file=D:/mysql/mgr/3307/my.ini

-- 3.4 通过MySQL Client进入3307实例:
mysql -uroot -p -P3307

mysqladmin -uroot -p -P3307 password 123asd

-- 3.5 重复在3306实例的操作:
SET SQL_LOG_BIN=0;
CREATE USER repl@'%';
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';

-- 3.6 在3307实例上安装GR插件:
INSTALL PLUGIN group_replication SONAME 'group_replication.dll';

-- 3.7 加入组复制(注意,此处与上面不同,笔者学习时没有注意,掉了大坑):
SET GLOBAL group_replication_allow_local_disjoint_gtids_join=ON;
START GROUP_REPLICATION;

-- 检查一下成员状态:
SELECT * FROM performance_schema.replication_group_members;

+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| CHANNEL_NAME             | MEMBER_ID                           | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y     | 3306        | ONLINE       |
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y     | 3307        | RECOVERING   |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
2 rows in set (0.00 sec)

-- 过了一阵子再检查,就变成 ONLINE了。

-- 有时状态不对,重新启动组复制:
STOP GROUP_REPLICATION;
START GROUP_REPLICATION;

-- 在3307上检查一下同步状态:
SELECT * FROM test.tb_test;


-- 4 添加3308实例:
-- 4.1 修改3308配置文件:同上,3306改为3308

-- 4.2 然后初始化3308实例:
mysqld -- defaults-file=D:/mysql/mgr/3308/my.ini -- initialize-insecure

-- 4.3 配置并启动3308实例(服务):
mysqld -- install mysql3308 -- defaults-file=D:/mysql/mgr/3308/my.ini

-- 4.4 同样进入3308实例:
mysql -uroot -p -P3308

mysqladmin -uroot -p -P3308 password 123asd

-- 4.5 在3308实例上重复操作:
SET SQL_LOG_BIN=0;
CREATE USER repl@'%';
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';


-- 4.6 在3308实例上安装GR插件,
INSTALL PLUGIN group_replication SONAME 'group_replication.dll';

-- 4.7 加入组复制:
SET GLOBAL group_replication_allow_local_disjoint_gtids_join=ON;
START GROUP_REPLICATION;

-- 最后再检查一下组复制成员的状态:
SELECT * FROM performance_schema.replication_group_members;

+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y     | 3306        | ONLINE       |
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y     | 3307        | ONLINE       |
| group_replication_applier | 6ce8c980-ed4a-11e6-bf00-000c297f23b7 | sAno1y     | 3308        | ONLINE       |
+-------------------------- -+-------------------------------------- +-------------+-------------+-------------- +
3 rows in set (0.00 sec)


-- 4.8 当然在3308实例上也已将3306的事务apply过来了:
SELECT * FROM test.tb_test;

--自己在测试库中创建更多的表、增删改数据,测试MGR...

--5 MGR一些常用检查
-- 成员read_only和super-read-only值
SELECT @@read_only, @@super_read_only;

-- 查看组复制成员
SELECT * FROM performance_schema.replication_group_members;

-- 查看确定哪一个成员是primary,可以在三个成员中的任意一个执行:
SELECT 
    b.member_id, b.member_host, b.member_port
FROM performance_schema.global_status a
INNER JOIN performance_schema.replication_group_members b
    ON a.variable_value = b.member_id
WHERE a.variable_name= 'group_replication_primary_member';

你可能感兴趣的:(mysql,组复制)