溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL數(shù)據(jù)庫---MHA高可用群集架構(gòu)

發(fā)布時(shí)間:2020-06-15 02:28:23 來源:網(wǎng)絡(luò) 閱讀:309 作者:caozhengtao1213 欄目:系統(tǒng)運(yùn)維

MHA概述

日本DeNA公司youshimaton (現(xiàn)就職于Facebook公司) 開發(fā)
一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件

MHA的組成

MHA Manager (管理節(jié)點(diǎn))
MHA Node (數(shù)據(jù)節(jié)點(diǎn))

MHA特點(diǎn)

自動(dòng)故障切換過程中,MHA試圖從宕機(jī)的主服務(wù)器上保存二進(jìn)制日志,最大程度的保證數(shù)據(jù)的不丟失
使用MySQL 5.5的半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險(xiǎn)

實(shí)驗(yàn)思路

1、MHA架構(gòu)

(1)數(shù)據(jù)庫安裝
(2)一主兩從
(3)MHA搭建

2、故障模擬

(1)主庫失效
(2)備選主庫成為主庫
(3)從庫2將備選主庫指向?yàn)橹鲙?/code>

部署環(huán)境

1、實(shí)驗(yàn)環(huán)境
服務(wù)器角色 IP地址 服務(wù)軟件包
master 192.168.142.130 mha4mysql-node
slave1 192.168.142.131 mha4mysql-node
slave2 192.168.142.132 mha4mysql-node
manager 192.168.142.133 mha4mysql-manager、 mha4mysql-node

2、實(shí)驗(yàn)要求

本案例要求通過MHA監(jiān)控MySQL 數(shù)據(jù)庫在故障時(shí)進(jìn)行自動(dòng)切換,不影響業(yè)務(wù)。

3、實(shí)現(xiàn)思路

(1)安裝MySQL數(shù)據(jù)庫
(2)配置MySQL一主兩從
(3)安裝MHA軟件
(4)配置無密碼認(rèn)證
(5)配置MySQL MHA高可用
(6)模擬master 故障切換

第一步:在三臺(tái)MySQL節(jié)點(diǎn)上分別安裝數(shù)據(jù)庫

(MySOL版本請(qǐng)使用5.6.36, cmake版本請(qǐng)使用2.8.6)

1、安裝編譯依賴的環(huán)境

yum install -y install ncurses-devel gcc gcc-c++ perl-Module-Install

2、遠(yuǎn)程掛載

mkdir /abc
mount.cifs //192.168.1421/mha /abc/

3、安裝gmake編譯軟件

cd /abc/mha/
tar zxvf cmake-2.8.6.tar.gz -C /opt/

cd /opt/cmake-2.8.6/
./configure
gmake && gmake install

4、安裝MySQL數(shù)據(jù)庫

cd /abc/mha/
tar zxvf mysql-5.6.36.tar.gz -C /opt/

cd /opt/mysql-5.6.36/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc

make && make install

cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

useradd -M -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql

/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql

5、修改master的主配置文件/etc/my.cnf文件,三臺(tái)服務(wù)器的server-id不能一樣

vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true

第二步:配置從服務(wù)器1

修改 mysql 的主配置文件

#在/etc/my.cnf中修改或者增加下面內(nèi)容。
[mysqld]
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

第三步:配置從服務(wù)器2

1.修改 mysql 的主配置文件:/etc/my.cnf

vim /etc/my.cnf
[mysql]
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

2.在master、slave1、slave2上分別做兩個(gè)軟連接

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

3.master、slave1、slave2上啟動(dòng)mysql,并查看開啟狀況

#啟動(dòng)mysql
/usr/local/mysql/bin/mysqld_safe --user=mysql &

#查看服務(wù)端口狀態(tài)
netstat -ntap | grep 3306

#關(guān)閉防火墻和安全功能
systemctl stop firewalld.service 
setenforce 0

第四步:配置MySQL一主兩從

1.mysq主從配置相對(duì)比較簡單需要注意的是授權(quán),在所有數(shù)據(jù)庫節(jié)點(diǎn)上授權(quán)兩個(gè)用戶,一個(gè)是從庫同步使用用戶myslave,另一個(gè)是manager使用監(jiān)控用戶mha

grant replication slave on *.* to 'myslave'@'192.168.142.%' identified by '123';
grant all privileges on *.* to 'mha'@'192.168.142.%' identified by 'manager';
flush privileges;

2.下面三條授權(quán)按理論是不用添加的,但是做案例實(shí)驗(yàn)環(huán)境時(shí)候通過MHA檢查mysql主從有報(bào)錯(cuò),
報(bào)兩個(gè)從庫通過主機(jī)名連接不上主庫,所以所有數(shù)據(jù)庫加上下面的授權(quán)

grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';

#刷新數(shù)據(jù)庫
flush privileges;

3.在master主機(jī)上查看二進(jìn)制文件和同步點(diǎn)

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1292 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+

4.在slave1、slave2上分別執(zhí)行同步

change master to master_host='192.168.142.130',master_user='myslave',master_password='123',master_log_file='masterbin.000001',master_log_pos=1292;
start slave;
#開啟slave

5.查看IO和SQL線程都是yes代表代表同步正常

show slave status\G;

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
#必須設(shè)置兩個(gè)從庫為只讀模式

#設(shè)置兩個(gè)從庫為只讀模式
set global read_only=1;

#刷新數(shù)據(jù)庫
flush privileges;

第五步:安裝MHA軟件(所有服務(wù)器上都安裝MHA依賴的環(huán)境,首先安裝epel源)


#關(guān)閉防火墻和安全功能
systemctl stop firewalld.service 
setenforce 0

#安裝MHA依賴的環(huán)境
yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN

#安裝node(在所有服務(wù)器上安裝node)
tar zxvf /abc/rpm/MHA/mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57/
perl Makefile.PL
make && make install

第六步:在MHA-manager上安裝manager組件(注意:先安裝node組件才能安裝manager組件)

tar zxvf /abc/rpm/MHA/mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57/
perl Makefile.PL 
make
make install

manager安裝后會(huì)在/usr/local/bin下面生成幾個(gè)工具:

masterha_conf_host           
#添加或刪除配置的server信息

masterha_stop                   
#關(guān)閉manager

masterha_manager            
#啟動(dòng)manager腳本

masterha_check_repl         
#檢查mysql復(fù)制情況

masterha_master_monitor 
#檢查master是否宕機(jī)

masterha_check_ssh          
#檢查MHA的SSH配置狀況

masterha_master_switch    
#控制故障轉(zhuǎn)移(自動(dòng)或者手動(dòng))

masterha_check_status      
#檢測(cè)當(dāng)前MHA運(yùn)行狀態(tài)

node安裝后也會(huì)在/usr/local/bin下面生成幾個(gè)腳本(這些工具通常由MHA Manager的腳本出發(fā),無需人為陳操作)

apply_diff_relay_logs             
#識(shí)別差異的中繼日志事件并將其差異的事件應(yīng)用與其他的slave

filter_mysqlbinlog             
#去除不必要的ROLLBACK事件(MHA已不再使用這個(gè)工具)

purge_relay_logs            
#清除中繼日志(不會(huì)阻塞SQL線程)

save_binary_logs            
#保存和復(fù)制master的二進(jìn)制日志

第七步:配置無密碼驗(yàn)證

(1)在manager上配置到所有數(shù)據(jù)庫節(jié)點(diǎn)的無密碼認(rèn)證

#因?yàn)槭菬o密碼驗(yàn)證,所以一路按回車鍵
ssh-keygen -t rsa         
ssh-copy-id 192.168.142.130
ssh-copy-id 192.168.142.131
ssh-copy-id 192.168.142.132

(2)在master上配置到數(shù)據(jù)庫節(jié)點(diǎn)slave1和slave2的無密碼驗(yàn)證

ssh-keygen -t rsa
ssh-copy-id 192.168.142.131
ssh-copy-id 192.168.142.132

(3)在slave1上配置到數(shù)據(jù)庫節(jié)點(diǎn)master和slave2的無密碼認(rèn)證

ssh-keygen -t rsa
ssh-copy-id 192.168.142.130
ssh-copy-id 192.168.142.132

(4)在slave2上配置到數(shù)據(jù)庫節(jié)點(diǎn)master和slave1的無密碼認(rèn)證

ssh-keygen -t rsa
ssh-copy-id 192.168.142.130
ssh-copy-id 192.168.142.131

第八步:配置MHA

1.在manager節(jié)點(diǎn)上復(fù)制相關(guān)腳本到/usr/local/bin目錄

cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
#拷貝后會(huì)有四個(gè)執(zhí)行文件

#查看目錄權(quán)限
ll /usr/local/bin/scripts/

-rwxr-xr-x. 1 1001 1001  3648 May 31  2015 master_ip_failover            #自動(dòng)切換時(shí)VIP管理的腳本
-rwxr-xr-x. 1 1001 1001  9870 May 31  2015 master_ip_online_change      #在線切換時(shí)VIP的管理
-rwxr-xr-x. 1 1001 1001 11867 May 31  2015 power_manager           #故障發(fā)生后關(guān)閉主機(jī)的腳本
-rwxr-xr-x. 1 1001 1001  1360 May 31  2015 send_report             #因故障切換后發(fā)送警報(bào)的腳本

2.復(fù)制上述的自動(dòng)切換時(shí)VIP管理的腳本到/usr/local/bin目錄,這里使用腳本管理VIP

cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin

3.重新編寫 master_ip_failover 腳本:(刪除原有內(nèi)容,直接寫入下述內(nèi)容)

vim /usr/local/bin/master_ip_failover 

#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);

#添加內(nèi)容部分
my $vip = '192.168.142.200';
my $brdc = '192.168.142.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
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,
);

exit &main();

sub main {

print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

if ( $command eq "stop" || $command eq "stopssh" ) {

my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {

my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

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";
}

4.創(chuàng)建MHA軟件目錄并拷貝配置文件

mkdir /etc/masterha
cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
vim /etc/masterha/app1.cnf 

[server default]
#manager配置文件
manager_log=/var/log/masterha/app1/manager.log     

#manager日志
manager_workdir=/var/log/masterha/app1

#master保存binlog的位置,這里的路徑要與master里配置的bilog的相同
master_binlog_dir=/usr/local/mysql/data

#設(shè)置自動(dòng)failover時(shí)候的切換腳本。也就是上邊的那個(gè)腳本
master_ip_failover_script=/usr/local/bin/master_ip_failover

#設(shè)置手動(dòng)切換時(shí)候的切換腳本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change

#這個(gè)密碼是前文中創(chuàng)建監(jiān)控用戶的那個(gè)密碼
password=manager
remote_workdir=/tmp

#設(shè)置復(fù)制用戶密碼
repl_password=123

#設(shè)置復(fù)制用戶的用戶
repl_user=myslave

#設(shè)置發(fā)生切換后發(fā)生報(bào)警的腳本
reporl_script=/usr/local/send_report
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.45.130 -s 192.168.45.134

#設(shè)置故障發(fā)生關(guān)閉故障腳本主機(jī)
shutdown_script=""

#設(shè)置ssh的登錄用戶名
ssh_user=root

#設(shè)置監(jiān)控用戶
user=mha

[server1]
hostname=192.168.142.
port=3306

[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.142.
port=3306

[server3]
hostname=192.168.142.
port=3306

5.測(cè)試ssh無密碼認(rèn)證

masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf

#注意:第一次配置需要去master上手動(dòng)開啟虛擬IP
/sbin/ifconfig ens33:1 192.168.142.200/24

6.啟動(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 &

7.查看MHA狀態(tài),可以看到當(dāng)前的master是mysql1節(jié)點(diǎn)

masterha_check_status --conf=/etc/masterha/app1.cnf

8.查看MHA日志,也可以看到當(dāng)前的master是192.168.142.130

cat /var/log/masterha/app1/manager.log 

第九步:故障模擬

1、啟動(dòng)監(jiān)控觀察日志記錄

tailf /var/log/masterha/app1/manager.log

2、查看地址變化

pkill -9 mysql        
#宕掉mysql服務(wù)      

VIP地址不會(huì)因?yàn)閙anager節(jié)點(diǎn)停止MHA服務(wù)而消失,VIP地址會(huì)轉(zhuǎn)移到slave1上

#從服務(wù)器查看vip地址轉(zhuǎn)移
ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.142.131  netmask 255.255.255.0  broadcast 192.168.142.255
        inet6 fe80::b81a:9df:a960:45ac  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:97:8e:66  txqueuelen 1000  (Ethernet)
        RX packets 1687418  bytes 1157627305 (1.0 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1376468  bytes 170996461 (163.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.142.200  netmask 255.255.255.0  broadcast 192.168.142.255
        ether 00:0c:29:97:8e:66  txqueuelen 1000  (Ethernet)

3、在mha-manager上開啟另外一個(gè)新的終端,直接yum安裝一個(gè)mysql

 yum install mysql -y

#在slave1上賦予權(quán)限,要不然mha-manager這邊是進(jìn)不到數(shù)據(jù)庫的:
grant all on *.* to 'root'@'%' identified by 'abc123';

#在mh-manager上進(jìn)行登錄:
 mysql -h 192.168.142.200 -uroot -p
Enter password:             
#輸入密碼

(1)創(chuàng)建個(gè)數(shù)據(jù)庫school,并創(chuàng)建個(gè)表info,寫一下簡單的內(nèi)容

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MySQL [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> use school;
Database changed
MySQL [school]> create table info (id int);
Query OK, 0 rows affected (0.01 sec)

(2)創(chuàng)建好以后再slave1上的數(shù)據(jù)庫中查看,會(huì)同步數(shù)據(jù)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+

(3)因?yàn)閟lave1和slave2之間是相互同步的,所以在slave2上數(shù)據(jù)也應(yīng)該同步

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+

謝謝閱讀!!!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI