mysql group replication 安装&配置详解

1、原起:html

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

mysql-5.7.20 以前的版本的mgr都有着各类各样的问题、感受像是一个半成品、可是5.7.20这个版本的mgr已经基本linux

可用了。因此接下来打算把整个mgr系列写完。git

 

 

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

  主名        ip地址      在mgr中的角色sql

  mtls17      10.186.19.17      primary数据库

  mtls18      10.186.19.18      secondebootstrap

  mtls19      10.186.19.19      secondeapp

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

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

 

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

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

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

 

3、下载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

 

4、安装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

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

 

 

5、建立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*

 

  

 6、增长mysql的配置文件:

  因为三台主机上都要安装、因此每台主机上都要加

touch /etc/my.cnf

  一、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

   二、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

  三、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:端口信息

 

 

7、初始化mysql数据库:

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

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

 

 

8、配置mysql与systemd结合:

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

  下配置文件。因为三台主机上的mysql都要开机启动、因此三台主机上都要执行以下的操做。

  一、增长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

  二、设置mysql开机启动

systemctl enable mysql

   三、若是你使用的是redhat-6.x 那么上面的两步能够用如下命令来完成

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

#启动mysql服务
service mysqld start

 

 

9、启动mysql:

systemctl start mysql

 

 

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

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

source /etc/profile

 

 

11、配置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)

 

 

12、配置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)

 

 

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

  逻辑上第二个结点与第3、第4、第五 ... 等等结点有着同样的逻辑角色、就也是说它们都不是群主;因此它们的配置

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

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

[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)

 

 

十4、验证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的安装成功了。

 

 

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

  一、官方说mgr是与mysql replication 彻底不一样的一种数据同步技术、为何还要加一个复制用户?

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

  答:

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

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

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

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

 

  二、为何要安装插件

  答:

      由于mgr功能是一个插件实现的。

 

 

十6、更多:

  一、若是你感受手动安装配置mgr比较费事、我作了一个mysql dba的工具、它能完成myql-group-replication的自动

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

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

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

 

 

 

 

 

 ----

相关文章
相关标签/搜索