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;

相關文章
相關標籤/搜索