mysql 5.7 MGR

最近看了一下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

  1. rpm -qa mysql mariadbmysql

若是有则卸载便可!
写入hosts文件映射关系,集群用获得
192.168.2.25    apache
192.168.2.26    nginx
192.168.2.30    kibana
二、安装依赖包nginx

  1. yum install gcc gcc-c++ ncurses-devel -yc++

三、安装cmake,下载地址:https://cmake.org/download/sql

  1. tar zxvf cmake-3.7.2.tar.gz数据库

  2. cd make-3.7.2apache

  3. ./configurebootstrap

  4. gmake && gmake installsocket

四、安装boost,由于mysql5.7须要,注意这里下载版本是1_59_0和mysql版本是对应的,若是你的MySQL版本和个人不同,不添加-DWITH_BOOST这个参数时它会报错告诉你须要下载boost的哪一个版本。

  1. tar zxvf boost_1_59_0.tar.gz

  2. cp -r boost_1_59_0 /usr/local/boost

五、安装mysql5.7.17及初始化操做

  1. groupadd mysql

  2. useradd -M -s /sbin/nologin mysql -g mysql

  3. tar zxvf mysql-5.7.17.tar.gz

  4. cd mysql-5.7.17

  5. 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

  6. make

  7. make install

  8. chown -R mysql.mysql /data/mysql

  9. mv /etc/my.cnf /etc/my.cnf.default

  10. cp /data/mysql/support-files/my-default.cnf /etc/my.cnf

  11. /data/mysql/bin/mysqld –initialize –user=mysql –basedir=/data/mysql –datadir=/data/mysql/data                 //注意初始化会生成一个随机的密码,请牢记

  12. echo “PATH=$PATH:/data/mysql/bin” >> /etc/profile

  13. source /etc/profile

  14. cp /data/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

  15. chmod +x /etc/rc.d/init.d/mysqld

以上步骤在三台机器上都须要执行

六、开始搭建MGR集群环境,修改第一个节点的my.cnf文件,内容以下:

  1. # For advice on how to change settings please see

  2. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

  3. # *** DO NOT EDIT THIS FILE. It’s a template which will be copied to the

  4. # *** default location during install, and will be replaced if you

  5. # *** upgrade to a newer version of MySQL.


  6. [mysqld]


  7. # Remove leading # and set to the amount of RAM for the most important data

  8. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

  9. # innodb_buffer_pool_size = 128M


  10. # Remove leading # to turn on a very important data integrity option: logging

  11. # changes to the binary log between backups.

  12. # log_bin


  13. # These are commonly set, remove the # and set as required.

  14. basedir = /data/mysql

  15. datadir = /data/mysql/data

  16. port = 3306

  17. socket = /data/mysql/data/mysql.sock

  18. log-error = /data/mysql/data/mysqld.log

  19. pid-file = /data/mysql/data/mysqld.pid


  20. # Remove leading # to set options mainly useful for reporting servers.

  21. # The server defaults are faster for transactions and fast SELECTs.

  22. # Adjust sizes as needed, experiment to find the optimal values.

  23. # join_buffer_size = 128M

  24. # sort_buffer_size = 2M

  25. # read_rnd_buffer_size = 2M


  26. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


  27. # Group Replication

  28. server_id = 1

  29. gtid_mode = ON

  30. enforce_gtid_consistency = ON

  31. master_info_repository = TABLE

  32. relay_log_info_repository = TABLE

  33. binlog_checksum = NONE

  34. log_slave_updates = ON

  35. log_bin = binlog

  36. binlog_format= ROW


  37. transaction_write_set_extraction = XXHASH64

  38. loose-group_replication_group_name = ‘ce9be252-2b71-11e6-b8f4-00212844f856’

  39. loose-group_replication_start_on_boot = off

  40. loose-group_replication_local_address = ‘192.168.2.25:33061’

  41. loose-group_replication_group_seeds =’192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061′

  42. loose-group_replication_bootstrap_group = off


  43. [client]

  44. socket = /data/mysql/data/mysql.sock

启动mysql服务
/etc/init.d/mysqld start

  1. set sql_log_bin=0;

  2. create user rpl_user@’%’;

  3. grant replication slave on *.* to rpl_user@’%’ identified by ‘rpl_pass’;

  4. flush privileges;

  5. set sql_log_bin=1;

  6. change master to master_user=’rpl_user’,master_password=’rpl_pass’ for channel ‘group_replication_recovery’;

  7. install PLUGIN group_replication SONAME ‘group_replication.so’;

  8. set global group_replication_bootstrap_group=ON;

  9. start group_replication;

  10. set global group_replication_bootstrap_group=OFF;

  11. select * from performance_schema.replication_group_members;

显示结果以下:

若是出现ONLINE,说明正常,这就是主节点,再搭建两个从节点。

七、第二个节点加入集群,复制刚刚的第一个节点的主配置文件my.cnf,只须要修改两个地方就行,已经用红色标注

  1. # For advice on how to change settings please see

  2. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

  3. # *** DO NOT EDIT THIS FILE. It’s a template which will be copied to the

  4. # *** default location during install, and will be replaced if you

  5. # *** upgrade to a newer version of MySQL.


  6. [mysqld]


  7. # Remove leading # and set to the amount of RAM for the most important data

  8. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

  9. # innodb_buffer_pool_size = 128M


  10. # Remove leading # to turn on a very important data integrity option: logging

  11. # changes to the binary log between backups.

  12. # log_bin


  13. # These are commonly set, remove the # and set as required.

  14. basedir = /data/mysql

  15. datadir = /data/mysql/data

  16. port = 3306

  17. socket = /data/mysql/data/mysql.sock

  18. log-error = /data/mysql/data/mysqld.log

  19. pid-file = /data/mysql/data/mysqld.pid


  20. # Remove leading # to set options mainly useful for reporting servers.

  21. # The server defaults are faster for transactions and fast SELECTs.

  22. # Adjust sizes as needed, experiment to find the optimal values.

  23. # join_buffer_size = 128M

  24. # sort_buffer_size = 2M

  25. # read_rnd_buffer_size = 2M


  26. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


  27. # Group Replication

  28. server_id = 2

  29. gtid_mode = ON

  30. enforce_gtid_consistency = ON

  31. master_info_repository = TABLE

  32. relay_log_info_repository = TABLE

  33. binlog_checksum = NONE

  34. log_slave_updates = ON

  35. log_bin = binlog

  36. binlog_format= ROW


  37. transaction_write_set_extraction = XXHASH64

  38. loose-group_replication_group_name = ‘ce9be252-2b71-11e6-b8f4-00212844f856’

  39. loose-group_replication_start_on_boot = off

  40. loose-group_replication_local_address = ‘192.168.2.26:33061’

  41. loose-group_replication_group_seeds =’192.168.2.25:33061,192.168.2.26:33061,192.168.2.30:33061′

  42. loose-group_replication_bootstrap_group = off


  43. [client]

  44. socket = /data/mysql/data/mysql.sock

第二个节点执行以下命令:

  1. set sql_log_bin=0;

  2. create user rpl_user@’%’;

  3. grant replication slave on *.* to rpl_user@’%’ identified by ‘rpl_pass’;

  4. set sql_log_bin=1;

  5. change master to master_user=’rpl_user’,master_password=’rpl_pass’ for channel ‘group_replication_recovery’;

  6. install plugin group_replication SONAME ‘group_replication.so’;

  7. set global group_replication_allow_local_disjoint_gtids_join=ON;

  8. start group_replication;

显示结果以下:

同理第三个节点加入操做方法也和第二个节点同样。
截图以下:

查询哪一个是主节点:

从上图来看很明显apache主机是主节点。

测试步骤:
一、在主库上建立一个库,而后建立表,在两个从库上查询数据是否同步?
二、两个从库只能执行查询操做?
二、手动关闭主库,确认两个从库其中一个是否会变成主库?并且是MEMBER_ID第一个字母按优先级排列的接管主库?

平常维护步骤:
一、若是从库某一节点关闭

  1. start group_replication;

二、若是全部的库都关闭后,第一个库做为主库首先执行

  1. set global group_replication_bootstrap_group=ON;

  2. start group_replication;

剩下的库直接执行便可!

  1. start group_replication;

三、若是主库故障,会自动从两个从库选出一个主库,主库启动后再次执行以下命令后会变成从库

  1. start group_replication;

相关文章
相关标签/搜索