這篇文章主要介紹了CentOS7如何搭建MySQL5.7高可用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作與策劃設(shè)計(jì),交城網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:交城等地區(qū)。交城做網(wǎng)站價(jià)格咨詢:18980820575
數(shù)據(jù)庫(kù)架構(gòu):一主兩從
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工具包主要包括以下幾個(gè)工具:
masterha_check_ssh 檢查MHA的SSH配置狀況
masterha_check_repl 檢查MySQL復(fù)制狀況
masterha_manger 啟動(dòng)MHA
masterha_check_status 檢測(cè)當(dāng)前MHA運(yùn)行狀態(tài)
masterha_master_monitor 檢測(cè)master是否宕機(jī)
masterha_master_switch 控制故障轉(zhuǎn)移(自動(dòng)或者手動(dòng))
masterha_conf_host 添加或刪除配置的服務(wù)器信息
Node工具包(這些工具通常由MHA Manager的腳本觸發(fā),無(wú)需人為操作)主要包括以下幾個(gè)工具:
save_binary_logs 保存和復(fù)制master的二進(jìn)制日志
apply_diff_relay_logs 識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用這個(gè)工具)
purge_relay_logs 清除中繼日志(不會(huì)阻塞SQL線程)
一、配置MySQL5.7
1.在配置文件添加,三臺(tái)服務(wù)器一樣,只需修改server-id和log-bin。
注意:binlog-do-db 和 replicate-ignore-db 設(shè)置必須相同。 MHA 在啟動(dòng)時(shí)候會(huì)檢測(cè)過(guò)濾規(guī)則,如果過(guò)濾規(guī)則不同,MHA 不啟動(dòng)監(jiān)控和故障轉(zhuǎn)移,這里沒(méi)有設(shè)置。
server-id=57 log-bin=mysql-bin gtid_mode = on #開啟gtid,必須主從全開 enforce_gtid_consistency = 1 log_slave_updates = 1 #開啟半同步復(fù)制 否則自動(dòng)切換主從的時(shí)候會(huì)報(bào)主鍵錯(cuò)誤 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.啟動(dòng)服務(wù)器,配置主從 (這里簡(jiǎn)寫,百度很多)
MySQL5.7會(huì)默認(rèn)加載validate_password 模塊,是來(lái)控制密碼長(zhǎng)度和規(guī)則的,可以在配置文件里面關(guān)閉該模塊 加上validate_password = off ,或者在mysql命令行執(zhí)行set global validate_password_policy=0;來(lái)臨時(shí)取消密碼規(guī)則。
在三臺(tái)服務(wù)器上配置復(fù)制用戶和監(jiān)控用戶,三臺(tái)服務(wù)器都要添加。
添加復(fù)制用戶
grant replication slave on *.* to 'repl'@'192.168.8.%' identified by 'mysql';
這里注意一定要用repl用戶,否則后邊會(huì)報(bào)找不到復(fù)制用戶
加監(jiān)控用戶
grant all privileges on *.* to 'root'@'192.168.8.%' identified by 'mysql';
在slave1和slave2上執(zhí)行
change master to master_host='192.168.8.57', master_port=3306, master_user='repl', master_password='mysql', master_auto_position=1;
設(shè)置從服務(wù)器只讀,不要在配置文件里寫,重點(diǎn)!
set global read_only=1
二、配置MHA
1.安裝依賴包(所有節(jié)點(diǎn))
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 (四臺(tái)服務(wù)器都安裝)
tar xf mha4mysql-node-0.58.tar.gz cd mha4mysql-node-0.58 perl Makefile.PL make && make install
安裝完成后會(huì)在/usr/local/bin目錄下面生成相應(yīng)的腳本,復(fù)制相關(guān)腳本到/usr/local/bin目錄(manager 服務(wù)器執(zhí)行)
cp -ra /root/mha4mysql-manager-0.57/samples/scripts/* /usr/local/bin
master_ip_failover
#自動(dòng)切換時(shí)vip管理的腳本,不是必須,如果我們使用keepalived的,我們可以自己編寫腳本完成對(duì)vip的管理,比如監(jiān)控mysql,如果mysql異常,我們停止keepalived就行,這樣vip就會(huì)自動(dòng)漂移
master_ip_online_change
#在線切換時(shí)vip的管理,不是必須,同樣可以可以自行編寫簡(jiǎn)單的shell完成
power_manager
#故障發(fā)生后關(guān)閉主機(jī)的腳本,不是必須
send_report
#因故障切換后發(fā)送報(bào)警的腳本,不是必須,可自行編寫簡(jiǎn)單的shell完成。
3.配置SSH登錄無(wú)密碼驗(yàn)證
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
將如下代碼全部復(fù)制進(jìn)去,根據(jù)自己的實(shí)際情況進(jìn)行修改
#!/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.測(cè)試MHA
測(cè)試ssh的連接情況
masterha_check_ssh –conf=/etc/masterha/app1.cnf
測(cè)試mysq集群的連接情況
masterha_check_repl –conf=/etc/masterha/app1.cnf
檢查MHA的狀態(tài)
masterha_check_status --conf=/etc/masterha/app1.cnf
啟動(dòng)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 該參數(shù)代表當(dāng)發(fā)生主從切換后,老的主庫(kù)的ip將會(huì)從配置文件中移除。
–manger_log 日志存放位置
–ignore_last_failover 在缺省情況下,如果MHA檢測(cè)到連續(xù)發(fā)生宕機(jī),且兩次宕機(jī)間隔不足8小時(shí)的話,則不會(huì)進(jìn)行Failover,之所以這樣限制是為了避免ping-pong效應(yīng)。該參數(shù)代表忽略上次MHA觸發(fā)切換產(chǎn)生的文件,默認(rèn)情況下,MHA發(fā)生切換后會(huì)在日志目錄,也就是上面我設(shè)置的/data產(chǎn)生app1.failover.complete文件,下次再次切換的時(shí)候如果發(fā)現(xiàn)該目錄下存在該文件將不允許觸發(fā)切換,除非在第一次切換后收到刪除該文件,為了方便,這里設(shè)置為–ignore_last_failover。
為了方便啟停MHA,創(chuàng)建以下腳本
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的啟動(dòng)狀態(tài)
tail -f /var/log/masterha/app1/manager.log
如果最后一行是
[info] Ping(SELECT) succeeded, waiting until MySQL doesn’t respond..
表明啟動(dòng)成功
8.master添加vip
在master上執(zhí)行
/sbin/ifconfig enp0s3:1 192.168.8.88/24
[ifconfig enp0s3: flags=4163mtu 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 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 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 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 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 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
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“CentOS7如何搭建MySQL5.7高可用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!