mysql5.7 MGR集群搭建
mysql5.7 MGR集群搭建部署
此文章由隊員(諄諄)擬寫
此文章來自 烏龜運維 官網 wuguiyunwei.com
QQ群 602183872
最近看了一下mysql5.7的MGR集群挺不錯的,有單主和多主模式,於是乎搭建測試了一下效果還不錯,我指的不錯是搭建和維護方面都比較簡單。網上絕大多數都是單主模式,當然我這裏也是,為了加深印象,特意記錄一下搭建過程,等以後再去嘗試多主模式,相信大家現在數據庫的瓶頸基本都是在寫,讀寫分離雖然是一種可行的解決方案,但是如果數據量很大,寫一樣會有問題,雖然有些解決方案能部署多個主節點,能同時進行讀寫,但是腦裂又是一個嚴重的問題,所以這裏MGR集群內置了自動化腦裂防護機制又得到了很多人的青睞,這裏MGR簡稱MySQL Group Replication是MySQL官方於2016年12月推出的一個全新的高可用與高擴展的解決方案。註意本文這裏不再闡述原理性的東西。
詳細部署信息如下:
主機名 | 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 | 節點 |
1、三臺機器準備工作
rpm -qa mysql mariadb
如果有則卸載即可!
寫入hosts文件映射關系,集群用得到
192.168.2.25 apache
192.168.2.30 kibana
2、安裝依賴包
yum install gcc gcc-c++ ncurses-devel -y
3、安裝cmake,下載地址:https://cmake.org/download/
tar zxvf cmake-3.7.2.tar.gz
cd make-3.7.2
./configure
gmake && gmake install
4、安裝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
5、安裝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
以上步驟在三臺機器上都需要執行
6、開始搭建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 [email protected]%’;
grant replication slave on *.* to [email protected]%’ 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,說明正常,這就是主節點,再搭建兩個從節點。
7、第二個節點加入集群,復制剛剛的第一個節點的主配置文件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 [email protected]%’;
grant replication slave on *.* to [email protected]%’ 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主機是主節點。
測試步驟:
1、在主庫上創建一個庫,然後創建表,在兩個從庫上查詢數據是否同步?
2、兩個從庫只能執行查詢操作?
2、手動關閉主庫,確認兩個從庫其中一個是否會變成主庫?而且是MEMBER_ID第一個字母按優先級排列的接管主庫?
日常維護步驟:
1、如果從庫某一節點關閉
start group_replication;
2、如果所有的庫都關閉後,第一個庫作為主庫首先執行
set global group_replication_bootstrap_group=ON;
start group_replication;
剩下的庫直接執行即可!
start group_replication;
3、如果主庫故障,會自動從兩個從庫選出一個主庫,主庫啟動後再次執行如下命令後會變成從庫
start group_replication;
mysql5.7 MGR集群搭建