1. 程式人生 > >mysql5.7 MGR集群搭建

mysql5.7 MGR集群搭建

解決方案 mysql 數據庫 文章 而且 mysql集群搭建

技術分享

mysql5.7 MGR集群搭建部署

此文章由隊員(諄諄)擬寫

此文章來自 烏龜運維 官網 wuguiyunwei.com

QQ群 602183872

最近看了一下mysql5.7的MGR集群挺不錯的,有單主和多主模式,於是乎搭建測試了一下效果還不錯,我指的不錯是搭建和維護方面都比較簡單。網上絕大多數都是單主模式,當然我這裏也是,為了加深印象,特意記錄一下搭建過程,等以後再去嘗試多主模式,相信大家現在數據庫的瓶頸基本都是在寫,讀寫分離雖然是一種可行的解決方案,但是如果數據量很大,寫一樣會有問題,雖然有些解決方案能部署多個主節點,能同時進行讀寫,但是腦裂又是一個嚴重的問題,所以這裏MGR集群內置了自動化腦裂防護機制又得到了很多人的青睞,這裏MGR簡稱MySQL Group Replication是MySQL官方於2016年12月推出的一個全新的高可用與高擴展的解決方案。註意本文這裏不再闡述原理性的東西。

註意:我這裏采用編譯安裝的方式,如果想簡單直接yum安裝mysql5.7也行,mysql編譯安裝需要的磁盤空間還是比較大的,一般在7G左右,所以要提前規劃好,用三個節點比較接近生產環境,而且更直接清晰。
詳細部署信息如下:

主機名IP地址安裝軟件用途
apache192.168.2.25cmake、boost、mysql節點
nginx192.168.2.26cmake、boost、mysql節點
kibana192.168.2.30cmake、boost、mysql節點

1、三臺機器準備工作

  1. rpm -qa mysql mariadb

如果有則卸載即可!
寫入hosts文件映射關系,集群用得到
192.168.2.25 apache

192.168.2.26 nginx
192.168.2.30 kibana
2、安裝依賴包

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

3、安裝cmake,下載地址:https://cmake.org/download/

  1. tar zxvf cmake-3.7.2.tar.gz

  2. cd make-3.7.2

  3. ./configure

  4. gmake && gmake install

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

5、安裝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

以上步驟在三臺機器上都需要執行

6、開始搭建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 [email protected]%’;

  3. grant replication slave on *.* to [email protected]%’ 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,說明正常,這就是主節點,再搭建兩個從節點。

7、第二個節點加入集群,復制剛剛的第一個節點的主配置文件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 [email protected]%’;

  3. grant replication slave on *.* to [email protected]%’ 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主機是主節點。

測試步驟:
1、在主庫上創建一個庫,然後創建表,在兩個從庫上查詢數據是否同步?
2、兩個從庫只能執行查詢操作?
2、手動關閉主庫,確認兩個從庫其中一個是否會變成主庫?而且是MEMBER_ID第一個字母按優先級排列的接管主庫?

日常維護步驟:
1、如果從庫某一節點關閉

  1. start group_replication;

2、如果所有的庫都關閉後,第一個庫作為主庫首先執行

  1. set global group_replication_bootstrap_group=ON;

  2. start group_replication;

剩下的庫直接執行即可!

  1. start group_replication;

3、如果主庫故障,會自動從兩個從庫選出一個主庫,主庫啟動後再次執行如下命令後會變成從庫

  1. start group_replication;


mysql5.7 MGR集群搭建