mysql group replication 安装&配置详解

一、原起:

  之前也有写过mysql-group-replication (mgr) 相关的文章、那时也没有什么特别的动力要写好它、主要是因为在

mysql-5.7.20 之前的版本的mgr都有着各种各样的问题、感觉像是一个半成品、但是5.7.20这个版本的mgr已经基本

可用了。所以接下来打算把整个mgr系列写完。

 

 

二、mysql-group-replication 安装环境规划:

  主名        ip地址      在mgr中的角色

  mtls17      10.186.19.17      primary

  mtls18      10.186.19.18      seconde

  mtls19      10.186.19.19      seconde

  也就是说我打算在mtls17\18\19这三台主机上安装一套mgr数据集群环境、我打算用mtls17主机做主库(primary)

  另外两台主机做从库(seconde)

 

  别外、还有一件事我想了很久、就是要不要不mysql的安装过程也加到博客里面、如果加的话就会显得博客特别长、

  没有办法凸出重点;可是如果只写mgr相关内容的话、一来读者可能不一定能把环境搭建起来、二来影响博客的质量

  思量再三还是决定把mysql安装的相应步骤写进来。

 

三、下载mysql-5.7.20社区版:

  由于三台机器上都要安装、下面的命令要在三台主机上执行

cd /tmp/
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

 

四、安装mysql:

  由于三台主机上都要安装、下面的命令要在三台主机上执行

cd /tmp/
tar -xvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

cd /usr/local/
ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql

  注意这里并没有完成mysql的安装、更准确的说、这里只是把mysql安装包解压到了/usr/local而已 。要完成mysql

  的安装还有一系列的步骤。

 

 

五、创建mysql数据目录 与 增加系统用户mysql:

  由于三台主机上都要安装、下面的命令要在三台主机上执行

mkdir -p /database/mysql/data/3306

useradd mysql

chown -R mysql:mysql /database/mysql/data/3306
chown -R mysql:mysql /usr/local/mysql*

 

  

 六、增加mysql的配置文件:

  由于三台主机上都要安装、所以每台主机上都要加

touch /etc/my.cnf

  1、mtls17的配置文件内容如下

[mysqld]
basedir=/usr/local/mysql/
datadir=/database/mysql/data/3306
port=3306
socket=/tmp/mysql.sock

server_id=17
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=mysql-bin
binlog_format=row

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= "10.186.19.17:33060"
loose-group_replication_group_seeds= "10.186.19.17:33060,10.186.19.18:33060,10.186.19.19:33060"
loose-group_replication_bootstrap_group= off

   2、mtls18的配置文件的内容如下

[mysqld]
basedir=/usr/local/mysql/
datadir=/database/mysql/data/3306
port=3306
socket=/tmp/mysql.sock

server_id=18
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=mysql-bin
binlog_format=row

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= "10.186.19.18:33060"
loose-group_replication_group_seeds= "10.186.19.17:33060,10.186.19.18:33060,10.186.19.19:33060"
loose-group_replication_bootstrap_group= off

  3、mtls19的配置文件如下

[mysqld]
basedir=/usr/local/mysql/
datadir=/database/mysql/data/3306
port=3306
socket=/tmp/mysql.sock

server_id=19
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=mysql-bin
binlog_format=row

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= "10.186.19.19:33060"
loose-group_replication_group_seeds= "10.186.19.17:33060,10.186.19.18:33060,10.186.19.19:33060"
loose-group_replication_bootstrap_group= off

   在这里有一些技术细节要说明一下:

    上面的三个配置文件省略了所有不必要的配置项、但是看起来还是有点多、这些都是mgr环境要求的。

    server_id 每个实例都要不要样

    loose-group_replication_group_name:为mgr高可用组起一个名字,这个名字一定要是uuid格式的。

    loose-group_replication_local_address:mgr各实例之前都是要进行通信的、这个配置项设置的就是本

    实例所监听的ip:端口

    loose-group_replication_group_seeds:各mgr实例所监听的ip:端口信息

 

 

七、初始化mysql数据库:

  这个在三台主机上都要执行

cd /usr/local/mysql/
./bin/mysqld --defaults-file=/etc/my.cnf --datadir=/database/mysql/data/3306/ --user=mysql --initialize-insecure

 

 

八、配置mysql与systemd结合:

   在redhat-7.x 与服务启动相关的脚本、不在是之前的/etc/init.d/目录的下脚本。而是一个/usr/lib/systemd/system/目录

  下配置文件。由于三台主机上的mysql都要开机启动、所以三台主机上都要执行如下的操作。

  1、增加systemd相关的配置文件/usr/lib/systemd/system/mysql.service

touch /usr/lib/systemd/system/mysql.service

  mysql.service 的内容如下

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
#LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1

  2、设置mysql开机启动

systemctl enable mysql

   3、如果你使用的是redhat-6.x 那么上面的两步可以用以下命令来完成

#配置开机启动
cd /usr/local/mysql/
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig mysqld on

#启动mysql服务
service mysqld start

 

 

九、启动mysql:

systemctl start mysql

 

 

十、为了方便使用重新设置一下PATH环境变量:

echo 'PATH=/usr/local/mysql/bin/:$PATH' >>/etc/profile

source /etc/profile

 

 

十一、配置mgr的第一个结点:

  mgr中所有的结点都属于一个逻辑上的组、这个组就像是QQ群一样、是由群主建起来的、有了这个上组之后、

  其它的结点就可以加入到这个组中来了。 mtls17来建群

  以下步骤在mtls17主机上的mysql中执行

  第一步:创建用于复制的用户

set sql_log_bin=0;
    create user mgruser@'%' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'%';
    create user mgruser@'127.0.0.1' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'127.0.0.1';
    create user mgruser@'localhost' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'localhost';
set sql_log_bin=1;

  第二步:配置复制所使用的用户

change master to 
    master_user='mgruser',
    master_password='mtls@352'
    for channel 'group_replication_recovery';

  第三步:安装mysql group replication 这个插件

install plugin group_replication soname 'group_replication.so';

  第四步:建个群(官方点的说法就是初始化一个复制组)

set global group_replication_bootstrap_group=on;
start group_replication;
set global group_replication_bootstrap_group=off;

  以下是我完成这四步的过程:

[root@mtls17 mysql]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql>     create user mgruser@'%' identified by 'mtls@352';
Query OK, 0 rows affected (0.01 sec)

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

mysql>     create user mgruser@'127.0.0.1' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

mysql>     create user mgruser@'localhost' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

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

mysql> 
mysql> 
mysql> change master to 
    ->     master_user='mgruser',
    ->     master_password='mtls@352'
    ->     for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.10 sec)

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

mysql> 
mysql> 
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.11 sec)

mysql> set global group_replication_bootstrap_group=off;
Query OK, 0 rows affected (0.00 sec)

 

 

十二、配置mgr的第二个结点:

  第二个结点和第一个结点唯一的不同在于它不在要自己去建一个群了、它只要加入第一个结点建的群就可以了

  第一步:创建用于复制的用户

set sql_log_bin=0;
    create user mgruser@'%' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'%';
    create user mgruser@'127.0.0.1' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'127.0.0.1';
    create user mgruser@'localhost' identified by 'mtls@352';
    grant replication slave,replication client on *.* to mgruser@'localhost';
set sql_log_bin=1;

  第二步:配置复制所要的用户

change master to 
    master_user='mgruser',
    master_password='mtls@352'
    for channel 'group_replication_recovery';

  第三步:安装组复制插件

install plugin group_replication soname 'group_replication.so';

  第四步:加入前面创建好的复制组

start group_replication;

   以下是我完成这四步的过程

[root@mtsl18 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

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

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

mysql>     create user mgruser@'127.0.0.1' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

mysql>     create user mgruser@'localhost' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

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

mysql> 
mysql> 
mysql> change master to 
    ->     master_user='mgruser',
    ->     master_password='mtls@352'
    ->     for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

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

 

 

 十三、配置mgr的其它结点:

  逻辑上第二个结点与第三、第四、第五 ... 等等结点有着一样的逻辑角色、就也是说它们都不是群主;所以它们的配置

  方式和第二个结点是一样的。

  以下是我配置第三个结点时的过程

[root@mtls19 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql>     create user mgruser@'%' identified by 'mtls@352';
Query OK, 0 rows affected (0.01 sec)

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

mysql>     create user mgruser@'127.0.0.1' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

mysql>     create user mgruser@'localhost' identified by 'mtls@352';
Query OK, 0 rows affected (0.00 sec)

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

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

mysql> 
mysql> 
mysql> change master to 
    ->     master_user='mgruser',
    ->     master_password='mtls@352'
    ->     for channel 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.12 sec)

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

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

 

 

十四、验证mgr各个结点是否正常:

mysql> select * from performance_schema.replication_group_members ;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 34760575-c607-11e7-96e3-9a17854b700d | mtls17      |        3306 | ONLINE       |
| group_replication_applier | 8816fee3-c77d-11e7-832c-1e1b3511358e | mtsl18      |        3306 | ONLINE       |
| group_replication_applier | 8dfc74c1-c77d-11e7-9447-8a7c439b72d9 | mtls19      |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)

结论:3个结点的状态都是online 说明它们是正常的、进一步说明mgr的安装成功了。

 

 

十五、对于mgr配置过程中一些要点问题的回答:

  1、官方说mgr是与mysql replication 完全不同的一种数据同步技术、为什么还要加一个复制用户?

  (这个上问题针对的是第十二节的第一步&第二步)

  答:

      一个节点在加入mgr组时、这个加入的过程在逻辑上可以分成两个阶段、第一个阶段基于传统的gtid的复制

  方式把这个上结点落下的数据补上去;假设这个阶段用时30分钟、这30分钟内mgr集群还是可以接受数据写入的。

  那这30分钟的数据通过什么方式补呢?答案就是这30分钟的数据在第二阶段补、第二阶段就是用的mgr的方式同步

  的了、在把数据补上之后就个结点就成功的加入的mgr集群、并为online状态。

 

  2、为什么要安装插件

  答:

      因为mgr功能是一个插件实现的。

 

 

十六、更多:

  1、如果你感觉手动安装配置mgr比较费事、我做了一个mysql dba的工具、它能完成myql-group-replication的自动

  安装配置 工具的地址:https://github.com/Neeky/mysqltools#mysql-group-replication环境的安装

  2、这里只介绍了mgr的安装与配置、并没有对mgr的功能进行测试、是因为我已经写了一份关于mgr功能的测试的报告

   测试报告地址:http://www.cnblogs.com/JiangLe/p/7809229.html

 

 

 

 

 

 ----

posted on 2017-04-18 13:19  蒋乐兴的技术随笔  阅读(13502)  评论(15编辑  收藏  举报

导航