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