MySQL架構——MHA部署

MHA 簡介

MHA(Master High Availability)

  • 目前在MySQL高可用方面是一個相對成熟的解決方案,它由日本DeNA公司youshimaton(現就職於Facebook公司)開發,是一套優秀的作爲MySQL高可用性環境下故障切換和主從提升的高可用軟件。在MySQL故障切換過程中,MHA能做到在0~30秒之內自動完成數據庫的故障切換操作,並且在進行故障切換的過程中,MHA能在最大程度上保證數據的一致性,以達到真正意義上的高可用。

該軟件由兩部分組成

  • MHA Manager(管理節點)
    • MHA Manager可以單獨部署在一臺單獨的機器上管理多個master-slave集羣,也可以部署在一臺slave節點上。
  • MHA Node(數據節點)
    • MHA Node運行在每臺MySQL服務器上,MHA Manager會定時探測集羣中的master節點,當master出現故障時,它可以自動將最新數據的slave提升爲新的master,然後將所有其他的slave重新指向新的master。整個故障轉移過程對應用程序完全透明。

工作原理

  • 在MHA自動故障切換過程中,MHA試圖從宕機的主服務器上保存二進制日誌,最大程度的保證數據的不丟失,但這並不總是可行的。例如,如果主服務器硬件故障或無法通過ssh訪問,MHA沒法保存二進制日誌,只進行故障轉移而丟失了最新的數據。使用MySQL 5.5的半同步複製,可以大大降低數據丟失的風險。MHA可以與半同步複製結合起來。如果只有一個slave已經收到了最新的二進制日誌,MHA可以將最新的二進制日誌應用於其他所有的slave服務器上,因此可以保證所有節點的數據一致性。
  • 順序
    • 從宕機崩潰的master保存二進制日誌事件(binlog events);
    • 識別含有最新更新的slave;
    • 應用差異的中繼日誌(relay log)到其他的slave;
    • 應用從master保存的二進制日誌事件(binlog events);
    • 提升一個slave爲新的master;
    • 使其他的slave連接新的master進行復制

部署MHA

實驗環境

  • master服務器IP地址:192.144.144.131
  • slave1服務器IP地址:192.144.144.140
  • slave2服務器IP地址:192.144.144.136
  • manager服務器IP地址:192.144.144.170

在master、slave1、slave2服務器安裝mysql

yum -y install ncurses-devel gcc-c++ perl-Module-Install    //安裝環境包
mount.cifs //192.144.100.8/shares /mnt/       //掛載軟件包目錄
cd /mnt      //進入掛載目錄
tar zxvf mysql-5.6.26.tar.gz -C /opt/      //解壓
cd /opt/mysql-5.6.26              //進入解壓目錄
cmake \                     //配置
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
make && make install         //編譯安裝
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql    //創建mysql用戶
chown -R mysql.mysql /usr/local/mysql
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   //使環境變量生效

修改 mysql 的主配置文件:/etc/my.cnf ,注意三臺服務器的 server-id 不能一樣

//master服務器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 1
log_bin = master-bin
log-slave-updates = true

//slave1服務器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

//slave2服務器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index

三臺服務器啓動 mysql 服務

ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/   //創建鏈接文件
systemctl stop firewalld.service      //關閉防火牆
setenforce 0                          //關閉selinux
/usr/local/mysql/bin/mysqld_safe --user=mysql &        //開啓服務
[root@s01 mysql-5.6.36]# netstat -natp | grep 3306    //查看端口是否正常開發
tcp6       0      0 :::3306                 :::*                    LISTEN      40105/mysqld 

配置 Mysql 主從同步(授權分別在master、slave1、slave2服務器上都要授權)

mysql -uroot -p      //進入數據庫
mysql> grant replication slave on *.* to 'myslave'@'192.144.144.%' identified by '123';   //添加授權
mysql> grant all privileges on *.* to 'mha'@'192.144.144.%' identified by 'manager';    //添加授權
mysql> flush privileges;  //刷新數據庫
mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';   //添加授權主機名
mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
mysql> show master status;    //在 Mysql 主服務器上查看二進制文件和同步點
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     1215 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

在slave1 和slave2 上分別執行同步

change master to master_host='192.168.144.131',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1215;

在兩臺從服務器上開啓slave,查看 IO 和 SQL 線程是否都爲 yes ,表示同步正常

mysql> start slave;
mysql> show slave status\G;
...
            raster user: mnysIave
            Master_ Port: 3306
          Connect_ Retry: 60
       Master Log_ File: master-bin .000001
    Read Master Log_ Pos: 1215
       Relay_ Log_ File: relay- log- bin.000002 
       Relay_ Log_ Pos: 284
  Relay_ Master Log_ File: master- bin .000001
      Slave_ IO_ Running: Yes
     Slave_ SQL_ Running: Yes
        Replicate DO DB:
...
mysql> set global read_only=1;    //設置兩個從服務器爲只讀模式

安裝MHA(所有服務器上都必須安裝 MHA 依賴的環境包)

yum install epel-release --nogpgcheck -y        //安裝epel
yum install -y perl-DBD-MYSQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
cd /mnt
tar zvxf mha4mysql-node-0.57.tar.gz -C /opt/     //解壓node組件包
cd /opt/mha4mysql-node-0.57/
perl Makefile.PL 
make && make install

在manager服務器上安裝 manager組件

cd /mnt
tar zvxf mha4mysql-manager-0.57.tar.gz -C /opt/
cd /opt/mha4mysql-manager-0.57/
perl Makefile.PL
make && make install

配置無密碼認證

//在 manager 上配置到所有數據庫節點的無密碼認證:
ssh-keygen -t rsa  //因爲是無密碼登錄,所以這步一直回車即可(會出現密鑰)
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
輸入“yes”;再輸入密碼即可

//在 master 上配置到數據庫節點slave1 和slave2 的無密碼認證:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
輸入“yes”;再輸入密碼即可

//在 slave1 上配置到數據庫節點master 和slave2 的無密碼認證:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.136
輸入“yes”;再輸入密碼即可

//在 slave2 上配置到數據庫節點master 和slave1 的無密碼認證:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.140
輸入“yes”;再輸入密碼即可

在 manager 節點上覆制相關腳本到 /usr/local/bin 目錄

cp -ra /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/
ls scripts/
master_ip_failover:        //自動切換時 VIP 管理的腳本;
master_ip_online_change:    //在線切換時 VIP 的管理;
power_manager:              //故障發生後關閉主機的腳本;
send_report:                //因故障切換後發送報警的腳本;
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/  //將自動切換時 VIP 管理的腳本複製到 /usr/local/bin/目錄下

重新編寫 master_ip_failover 腳本

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
);
#############################添加內容部分#########################################
my $vip = '192.168.114.100';
my $brdc = '192.168.114.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";
}

創建 MHA 軟件目錄並拷貝配置文件

mkdir /etc/masterha
cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
vim /etc/masterha/app1.cnf     //編輯配置文件
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/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=manager
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.114.140 -s 192.168.114.136
shutdown_script=""
ssh_user=root
user=mha

[server1]
hostname=192.168.114.131
port=3306

[server2]
candidate_master=1
hostname=192.168.114.140
check_repl_delay=0
port=3306

[server3]
hostname=192.168.114.136
port=3306

測試 ssh 無密碼認證

masterha_check_ssh -conf=/etc/masterha/app1.cnf            //驗證密鑰對
Mon Dec 16 10:32:49 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:32:49 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Starting SSH connection tests..
Mon Dec 16 10:32:50 2019 - [debug] 
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.131:22) to [email protected](192.168.144.136:22)..
Mon Dec 16 10:32:49 2019 - [debug]   ok.
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.131:22) to [email protected](192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [debug] 
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.140:22) to [email protected](192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.140:22) to [email protected](192.168.144.136:22)..
Mon Dec 16 10:32:51 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [debug] 
Mon Dec 16 10:32:49 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.136:22) to [email protected](192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:50 2019 - [debug]  Connecting via SSH from [email protected](192.168.144.136:22) to [email protected](192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug]   ok.
Mon Dec 16 10:32:51 2019 - [info] All SSH connection tests passed successfully.

驗證複製

masterha_check_repl -conf=/etc/masterha/app1.cnf      #驗證複製
Mon Dec 16 10:34:36 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:34:36 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] MHA::MasterMonitor version 0.57.
Creating directory /var/log/masterha/app1.. done.
Mon Dec 16 10:34:37 2019 - [info] GTID failover mode = 0
Mon Dec 16 10:34:37 2019 - [info] Dead Servers:
Mon Dec 16 10:34:37 2019 - [info] Alive Servers:
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.140(192.168.144.140:3306)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.136(192.168.144.136:3306)
Mon Dec 16 10:34:37 2019 - [info] Alive Slaves:
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.140(192.168.144.140:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info]     Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Dec 16 10:34:37 2019 - [info]   192.168.144.136(192.168.144.136:3306)  Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info]     Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Current Alive Master: 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Checking slave configurations..
Mon Dec 16 10:34:37 2019 - [warning]  relay_log_purge=0 is not set on slave 192.168.144.140(192.168.144.140:3306).
...
Checking the Status of the script.. OK 
Mon Dec 16 10:35:22 2019 - [info]  OK.
Mon Dec 16 10:35:22 2019 - [warning] shutdown_script is not defined.
Mon Dec 16 10:35:22 2019 - [info] Got exit code 0 (Not master dead).

在主服務器設置虛擬網卡

/sbin/ifconfig ens33:1 192.168.144.170/24

manager服務器開啓服務

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_cnf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &      //啓動MHA
masterha_check_status --conf=/etc/masterha/app1.cnf                     //檢查master
app1 (pid:3894) is running(0:PING_OK), master:192.168.144.131
cat /var/log/masterha/app1/manager.log            //查看節點信息
tailf /var/log/masterha/app1/manager.log            //啓動監控日誌

在主服務器停止MySQL模擬宕機

pkill -9 mysql                    
  • 此時在slave1服務器查看虛擬地址是否轉移到此服務器上
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章