一、原起:
之前也有寫過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
----