您好,登錄后才能下訂單哦!
這篇文章主要介紹了CentOS7如何搭建MySQL5.7高可用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
數據庫架構:一主兩從
master:192.168.8.57
slave1:192.168.8.58
slave2:192.168.8.59
manager:192.168.8.60
MHA工具包:
mha4mysql-manager-0.58.tar.gz
mha4mysql-node-0.58.tar.gz
Manager工具包主要包括以下幾個工具:
masterha_check_ssh 檢查MHA的SSH配置狀況
masterha_check_repl 檢查MySQL復制狀況
masterha_manger 啟動MHA
masterha_check_status 檢測當前MHA運行狀態
masterha_master_monitor 檢測master是否宕機
masterha_master_switch 控制故障轉移(自動或者手動)
masterha_conf_host 添加或刪除配置的服務器信息
Node工具包(這些工具通常由MHA Manager的腳本觸發,無需人為操作)主要包括以下幾個工具:
save_binary_logs 保存和復制master的二進制日志
apply_diff_relay_logs 識別差異的中繼日志事件并將其差異的事件應用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用這個工具)
purge_relay_logs 清除中繼日志(不會阻塞SQL線程)
一、配置MySQL5.7
1.在配置文件添加,三臺服務器一樣,只需修改server-id和log-bin。
注意:binlog-do-db 和 replicate-ignore-db 設置必須相同。 MHA 在啟動時候會檢測過濾規則,如果過濾規則不同,MHA 不啟動監控和故障轉移,這里沒有設置。
server-id=57 log-bin=mysql-bin gtid_mode = on #開啟gtid,必須主從全開 enforce_gtid_consistency = 1 log_slave_updates = 1 #開啟半同步復制 否則自動切換主從的時候會報主鍵錯誤 plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" loose_rpl_semi_sync_master_enabled = 1 loose_rpl_semi_sync_slave_enabled = 1 loose_rpl_semi_sync_master_timeout = 5000
2.啟動服務器,配置主從 (這里簡寫,百度很多)
MySQL5.7會默認加載validate_password 模塊,是來控制密碼長度和規則的,可以在配置文件里面關閉該模塊 加上validate_password = off ,或者在mysql命令行執行set global validate_password_policy=0;來臨時取消密碼規則。
在三臺服務器上配置復制用戶和監控用戶,三臺服務器都要添加。
添加復制用戶
grant replication slave on *.* to 'repl'@'192.168.8.%' identified by 'mysql';
這里注意一定要用repl用戶,否則后邊會報找不到復制用戶
加監控用戶
grant all privileges on *.* to 'root'@'192.168.8.%' identified by 'mysql';
在slave1和slave2上執行
change master to master_host='192.168.8.57', master_port=3306, master_user='repl', master_password='mysql', master_auto_position=1;
設置從服務器只讀,不要在配置文件里寫,重點!
set global read_only=1
二、配置MHA
1.安裝依賴包(所有節點)
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
2.安裝 manager
tar xf mha4mysql-manager-0.58.tar.gz cd mha4mysql-manager-0.58 perl Makefile.PL make && make install
安裝node (四臺服務器都安裝)
tar xf mha4mysql-node-0.58.tar.gz cd mha4mysql-node-0.58 perl Makefile.PL make && make install
安裝完成后會在/usr/local/bin目錄下面生成相應的腳本,復制相關腳本到/usr/local/bin目錄(manager 服務器執行)
cp -ra /root/mha4mysql-manager-0.57/samples/scripts/* /usr/local/bin
master_ip_failover
#自動切換時vip管理的腳本,不是必須,如果我們使用keepalived的,我們可以自己編寫腳本完成對vip的管理,比如監控mysql,如果mysql異常,我們停止keepalived就行,這樣vip就會自動漂移
master_ip_online_change
#在線切換時vip的管理,不是必須,同樣可以可以自行編寫簡單的shell完成
power_manager
#故障發生后關閉主機的腳本,不是必須
send_report
#因故障切換后發送報警的腳本,不是必須,可自行編寫簡單的shell完成。
3.配置SSH登錄無密碼驗證
master
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
slave1
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
slave2
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.60
manager
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.57 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.58 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.8.59
4.配置MHA
修改/usr/local/bin/master_ip_failover,這里使用腳本管理vip
將如下代碼全部復制進去,根據自己的實際情況進行修改
#!/usr/bin/env perl # Copyright (C) 2011 DeNA Co.,Ltd. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## Note: This is a sample script and is not complete. Modify the script based on your environment. use strict; use warnings FATAL => 'all'; use Getopt::Long; use MHA::DBHelper; my ( $command, $ssh_user, $orig_master_host, $orig_master_ip, $orig_master_port, $new_master_host, $new_master_ip, $new_master_port, $new_master_user, $new_master_password ); my $vip = '192.168.8.88/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig enp0s3:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig enp0s3:$key down"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, 'new_master_user=s' => \$new_master_user, 'new_master_password=s' => \$new_master_password, ); exit &main(); sub main { if ( $command eq "stop" || $command eq "stopssh" ) { # $orig_master_host, $orig_master_ip, $orig_master_port are passed. # If you manage master ip address at global catalog database, # invalidate orig_master_ip here. my $exit_code = 1; eval { # updating global catalog, etc $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { # all arguments are passed. # If you manage master ip address at global catalog database, # activate new_master_ip here. # You can also grant write access (create user, set read_only=0, etc) here. my $exit_code = 10; eval { my $new_master_handler = new MHA::DBHelper(); # args: hostname, port, user, password, raise_error_or_not $new_master_handler->connect( $new_master_ip, $new_master_port, $new_master_user, $new_master_password, 1 ); ## Set read_only=0 on the new master $new_master_handler->disable_log_bin_local(); print "Set read_only=0 on the new master.\n"; $new_master_handler->disable_read_only(); ## Creating an app user on the new master print "Creating app user on the new master..\n"; FIXME_xxx_create_user( $new_master_handler->{dbh} ); $new_master_handler->enable_log_bin_local(); $new_master_handler->disconnect(); ## Update master ip on the catalog database, etc # FIXME_xxx; $exit_code = 0; }; if ($@) { warn $@; # If you want to continue failover, exit 10. exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { # do nothing exit 0; } else { &usage(); exit 1; } } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
5.修改MHA配置文件
mkdir -p /etc/masterha cp mha4mysql-manager-0.58/samples/conf/app1.cnf /etc/masterha/
[server default] manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/manager.log master_binlog_dir=/mysql/data master_ip_failover_script= /usr/local/bin/master_ip_failover master_ip_online_change_script= /usr/local/bin/master_ip_online_change password=mysql user=root ping_interval=1 remote_workdir=/tmp repl_password=mysql repl_user=repl report_script=/usr/local/bin/send_report secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.8.58 -s 192.168.8.59 shutdown_script="" ssh_user=root [server1] hostname=192.168.8.57 port=3306 [server2] hostname=192.168.8.58 port=3306 candidate_master=1 check_repl_delay=0 [server3] hostname=192.168.8.59 port=3306
6.測試MHA
測試ssh的連接情況
masterha_check_ssh –conf=/etc/masterha/app1.cnf
測試mysq集群的連接情況
masterha_check_repl –conf=/etc/masterha/app1.cnf
檢查MHA的狀態
masterha_check_status --conf=/etc/masterha/app1.cnf
啟動MHA
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
–remove_dead_master_conf 該參數代表當發生主從切換后,老的主庫的ip將會從配置文件中移除。
–manger_log 日志存放位置
–ignore_last_failover 在缺省情況下,如果MHA檢測到連續發生宕機,且兩次宕機間隔不足8小時的話,則不會進行Failover,之所以這樣限制是為了避免ping-pong效應。該參數代表忽略上次MHA觸發切換產生的文件,默認情況下,MHA發生切換后會在日志目錄,也就是上面我設置的/data產生app1.failover.complete文件,下次再次切換的時候如果發現該目錄下存在該文件將不允許觸發切換,除非在第一次切換后收到刪除該文件,為了方便,這里設置為–ignore_last_failover。
為了方便啟停MHA,創建以下腳本
cat masterha_start.sh nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & cat masterha_stop.sh masterha_stop --conf=/etc/masterha/app1.cnf
7.檢查MHA的啟動狀態
tail -f /var/log/masterha/app1/manager.log
如果最后一行是
[info] Ping(SELECT) succeeded, waiting until MySQL doesn’t respond..
表明啟動成功
8.master添加vip
在master上執行
/sbin/ifconfig enp0s3:1 192.168.8.88/24
[ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.8.57 netmask 255.255.255.0 broadcast 192.168.8.255 inet6 fe80::5198:593b:cdc5:1f90 prefixlen 64 scopeid 0x20<link> ether 08:00:27:c0:45:0d txqueuelen 1000 (Ethernet) RX packets 72386 bytes 9442794 (9.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 24221 bytes 2963104 (2.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp0s3:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.8.88 netmask 255.255.255.0 broadcast 192.168.8.255 ether 08:00:27:c0:45:0d txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 84 bytes 9492 (9.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 84 bytes 9492 (9.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:f4:55:bb txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
感謝你能夠認真閱讀完這篇文章,希望小編分享的“CentOS7如何搭建MySQL5.7高可用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。