最近看了一下mysql5.7的MGR集群挺不错的,有单主和多主模式,因而乎搭建测试了一下效果还不错,我指的不错是搭建和维护方面都比较简单。网上绝大多数都是单主模式,固然我这里也是,为了加深印象,特地记录一下搭建过程,等之后再去尝试多主模式,相信你们如今数据库的瓶颈基本都是在写,读写分离虽然是一种可行的解决方案,可是若是数据量很大,写同样会有问题,虽然有些解决方案能部署多个主节点,能同时进行读写,可是脑裂又是一个严重的问题,因此这里MGR集群内置了自动化脑裂防御机制又获得了不少人的青睐,这里MGR简称MySQL Group Replication是MySQL官方于2016年12月推出的一个全新的高可用与高扩展的解决方案。注意本文这里再也不阐述原理性的东西。
注意:我这里采用编译安装的方式,若是想简单直接yum安装mysql5.7也行,mysql编译安装须要的磁盘空间仍是比较大的,通常在7G左右,因此要提早规划好,用三个节点比较接近生产环境,并且更直接清晰。
详细部署信息以下:css
主机名 | IP地址 | 安装软件 | 用途 |
apache | 192.168.2.25 | cmake、boost、mysql | 节点 |
nginx | 192.168.2.26 | cmake、boost、mysql | 节点 |
kibana | 192.168.2.30 | cmake、boost、mysql | 节点 |
一、三台机器准备工做html
rpm -qa mysql mariadbmysql
若是有则卸载便可!
写入hosts文件映射关系,集群用获得
192.168.2.25 apache
192.168.2.26 nginx
192.168.2.30 kibana
二、安装依赖包nginx
yum install gcc gcc-c++ ncurses-devel -yc++
三、安装cmake,下载地址:https://cmake.org/download/sql
tar zxvf cmake-3.7.2.tar.gz数据库
cd make-3.7.2apache
./configurebootstrap
gmake && gmake installsocket
四、安装boost,由于mysql5.7须要,注意这里下载版本是1_59_0和mysql版本是对应的,若是你的MySQL版本和个人不同,不添加-DWITH_BOOST这个参数时它会报错告诉你须要下载boost的哪一个版本。
tar zxvf boost_1_59_0.tar.gz
cp -r boost_1_59_0 /usr/local/boost
五、安装mysql5.7.17及初始化操做
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
tar zxvf mysql-5.7.17.tar.gz
cd mysql-5.7.17
cmake -DCMAKE_INSTALL_PREFIX=/data/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_BOOST=/usr/local/boost
make
make install
chown -R mysql.mysql /data/mysql
mv /etc/my.cnf /etc/my.cnf.default
cp /data/mysql/support-files/my-default.cnf /etc/my.cnf
/data/mysql/bin/mysqld –initialize –user=mysql –basedir=/data/mysql –datadir=/data/mysql/data //注意初始化会生成一个随机的密码,请牢记
echo “PATH=$PATH:/data/mysql/bin” >> /etc/profile
source /etc/profile
cp /data/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
以上步骤在三台机器上都须要执行
六、开始搭建MGR集群环境,修改第一个节点的my.cnf文件,内容以下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It’s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /data/mysql
datadir = /data/mysql/data
port = 3306
socket = /data/mysql/data/mysql.sock
log-error = /data/mysql/data/mysqld.log
pid-file = /data/mysql/data/mysqld.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# Group Replication
server_id = 1
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 = binlog
binlog_format= ROW
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = ‘ce9be252-2b71-11e6-b8f4-00212844f856’
loose-group_replication_start_on_boot = off
loose-group_replication_local_address = ‘192.168.2.25:33061’
loose-group_replication_group_seeds =’192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061′
loose-group_replication_bootstrap_group = off
[client]
socket = /data/mysql/data/mysql.sock
启动mysql服务
/etc/init.d/mysqld start
set sql_log_bin=0;
create user rpl_user@’%’;
grant replication slave on *.* to rpl_user@’%’ identified by ‘rpl_pass’;
flush privileges;
set sql_log_bin=1;
change master to master_user=’rpl_user’,master_password=’rpl_pass’ for channel ‘group_replication_recovery’;
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;
select * from performance_schema.replication_group_members;
显示结果以下:
若是出现ONLINE,说明正常,这就是主节点,再搭建两个从节点。
七、第二个节点加入集群,复制刚刚的第一个节点的主配置文件my.cnf,只须要修改两个地方就行,已经用红色标注
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It’s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /data/mysql
datadir = /data/mysql/data
port = 3306
socket = /data/mysql/data/mysql.sock
log-error = /data/mysql/data/mysqld.log
pid-file = /data/mysql/data/mysqld.pid
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# Group Replication
server_id = 2
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 = binlog
binlog_format= ROW
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = ‘ce9be252-2b71-11e6-b8f4-00212844f856’
loose-group_replication_start_on_boot = off
loose-group_replication_local_address = ‘192.168.2.26:33061’
loose-group_replication_group_seeds =’192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061′
loose-group_replication_bootstrap_group = off
[client]
socket = /data/mysql/data/mysql.sock
第二个节点执行以下命令:
set sql_log_bin=0;
create user rpl_user@’%’;
grant replication slave on *.* to rpl_user@’%’ identified by ‘rpl_pass’;
set sql_log_bin=1;
change master to master_user=’rpl_user’,master_password=’rpl_pass’ for channel ‘group_replication_recovery’;
install plugin group_replication SONAME ‘group_replication.so’;
set global group_replication_allow_local_disjoint_gtids_join=ON;
start group_replication;
显示结果以下:
同理第三个节点加入操做方法也和第二个节点同样。
截图以下:
查询哪一个是主节点:
从上图来看很明显apache主机是主节点。
测试步骤:
一、在主库上建立一个库,而后建立表,在两个从库上查询数据是否同步?
二、两个从库只能执行查询操做?
二、手动关闭主库,确认两个从库其中一个是否会变成主库?并且是MEMBER_ID第一个字母按优先级排列的接管主库?
平常维护步骤:
一、若是从库某一节点关闭
start group_replication;
二、若是全部的库都关闭后,第一个库做为主库首先执行
set global group_replication_bootstrap_group=ON;
start group_replication;
剩下的库直接执行便可!
start group_replication;
三、若是主库故障,会自动从两个从库选出一个主库,主库启动后再次执行以下命令后会变成从库
start group_replication;