主頁(yè) > 知識(shí)庫(kù) > MySQL 搭建MHA架構(gòu)部署的步驟

MySQL 搭建MHA架構(gòu)部署的步驟

熱門(mén)標(biāo)簽:怎樣在地圖標(biāo)注銷(xiāo)售區(qū)域 外呼系統(tǒng)打電話上限是多少 曲靖移動(dòng)外呼系統(tǒng)公司 武漢網(wǎng)絡(luò)外呼系統(tǒng)服務(wù)商 啥是企業(yè)400電話辦理 百應(yīng)電話機(jī)器人優(yōu)勢(shì) 電話外呼系統(tǒng)改號(hào) 地圖標(biāo)注費(fèi)用是多少 南昌三維地圖標(biāo)注

MAH

一:MAH架構(gòu)介紹

  • MHA (Master High Availability)目前在MySQL高可用方面是一個(gè)相對(duì)成熟的解決方案,它由日本人youshimaton開(kāi)發(fā),是一套優(yōu)秀的作為MySQL高可用性環(huán)境下故障切換和主從提升的高可用軟件。在MySQL故障切換過(guò)程中,MHA能做到0~30秒之內(nèi)自動(dòng)完成數(shù)據(jù)庫(kù)的故障切換操作,并且在進(jìn)行故障切換的過(guò)程中,MHA能最大程度上保證數(shù)據(jù)庫(kù)的一致性,以達(dá)到真正意義上的高可用。
  • MHA由兩部分組成:MHA Manager (管理節(jié)點(diǎn))和MHANode(數(shù)據(jù)節(jié)點(diǎn))。MHA Manager可以獨(dú)立部署在一臺(tái)獨(dú)立的機(jī)器上管理多個(gè)Master-Slave集群,也可以部署在一臺(tái)Slave上。當(dāng)Master 出現(xiàn)故障是,它可以自動(dòng)將最新數(shù)據(jù)的Slave 提升為新的Master,然后將所有其他的Slave重新指向新的Master。整個(gè)故障轉(zhuǎn)移過(guò)程對(duì)應(yīng)用程序是完全透明的。

二:適用場(chǎng)景

目前MHA主要支持一主多從的架構(gòu),要搭建MHA,要求一個(gè)復(fù)制集群必須最少有3臺(tái)數(shù)據(jù)庫(kù)服務(wù)器,一主二從,即一臺(tái)充當(dāng)Master ,一臺(tái)充當(dāng)備用Master,另一臺(tái)充當(dāng)從庫(kù)。出于成本考慮,淘寶在此基礎(chǔ)上進(jìn)行了改造,目前淘寶開(kāi)發(fā)的 TMHA 已經(jīng)支持一主一從。

三:MHA工作原理

1.從宕機(jī)崩潰的 Master保存二進(jìn)制日志事件(binlog event) ;

2.識(shí)別含有最新更新的Slave;

3.應(yīng)用差異的中繼日志(relay log)到其他Slave;

4.應(yīng)用從 Master 保存的二進(jìn)制日志事件;

5.提升—個(gè) Slave為新的Master;

6.使其他的 Slave 連接新的 Master 進(jìn)行復(fù)制;

四:MHA的組成

  •  Manager工具包
  • Node工具包

1:Manager工具包

  • masterha_check_ssh:檢查MHA的 SSH配置情況
  • masterha_check_repl:檢查MySQL復(fù)制狀況
  • masterha_manager:啟動(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:添加或刪除配置的server 信息

2:Node工具包

通常由MHA Manager的腳本觸發(fā),無(wú)需人工操作

  • save_binary_logs:保存和復(fù)制Master 的 binlog日志
  • apply_diff_relay_logs:識(shí)別差異的中級(jí)日志時(shí)間并將其應(yīng)用到其他 Slave
  • filter_mysqlbinlog:去除不必要的ROOLBACK事件(已經(jīng)廢棄)
  • purge_relay_logs:清除中繼日志(不阻塞SQL線程)

五:MHA 特點(diǎn)

  • 自動(dòng)故障切換過(guò)程中,MHA試圖從宕機(jī)的主服務(wù)器上保存進(jìn)制日志,最大程度的保證數(shù)據(jù)不丟失
  • 使用半同步復(fù)制,可以大大降低數(shù)據(jù)丟失的風(fēng)險(xiǎn)
  • 目前MHA支持一主多從架構(gòu),最少三臺(tái)服務(wù),即一主兩從

MHA 架構(gòu)部署

一:拓?fù)鋱D

二:數(shù)據(jù)庫(kù)安裝

MySQL 版本使用 5.6.36、cmake 版本使用 2.8.6

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

[root@master ~]# yum -y install ncurses-devel gcc-c++ perl-Module-Install

2.:安裝 gmake 編譯軟件

[root@master ~]# tar zxvf cmake-2.8.6.tar.gz
[root@master ~]# cd cmake-2.8.6
[root@master cmake-2.8.6]# ./configure
[root@master cmake-2.8.6]# gmake  gmake install

3:安裝 MySQL 數(shù)據(jù)庫(kù)

[root@master ~]# tar -zxvf mysql-5.6.36.tar.gz
[root@master ~]# cd mysql-5.6.36
[root@master 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
[root@master mysql-5.6.36]# make  make install
[root@master mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
[root@master mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@master ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@master ~]# chkconfig --add mysqld
[root@master ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@master ~]# source /etc/profile
chown -R mysql.mysql /usr/local/mysql groupadd mysql
[root@master ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@master ~]# chown -R mysql.mysql /usr/local/mysql
[root@master ~]# mkdir -p /data/mysql
[root@master ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

4:修改 Master 的主配置文件/etc/my.cnf 文件

將原來(lái)配置全部刪除

[root@master ~]# vi /etc/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log_bin = master-bin
log-slave-updates = true

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

另外兩臺(tái) slave 數(shù)據(jù)庫(kù)

三臺(tái)服務(wù)器的 server-id 不能一樣,其余一樣正常寫(xiě)入

server-id = 2
log_bin = master-bin
relay-log = relay-log-bin 
relay-log-index = slave-relay-bin.index 
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin 
relay-log-index = slave-relay-bin.index 

5:三臺(tái)數(shù)據(jù)庫(kù)分別做兩個(gè)軟鏈接,軟鏈接是為 HMA 服務(wù)的

[root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

6:三臺(tái)數(shù)據(jù)庫(kù)啟動(dòng) MySQL

[root@master ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql 
[root@master ~]# service mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

三:數(shù)據(jù)庫(kù)配置主從同步

登錄數(shù)據(jù)庫(kù)

[root@master ~]# mysql

1:在所有數(shù)據(jù)庫(kù)節(jié)點(diǎn)上授權(quán)兩個(gè)用戶,一個(gè)是從庫(kù)同步使用,另外一個(gè)是 manager 使用

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

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

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';

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 |  608 |    |     |     |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4:在 slave1 和 slave2 分別執(zhí)行同步

mysql> change master to master_host='20.0.0.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=608
mysql> start slave;

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

mysql> show slave status\G;
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 20.0.0.10
     Master_User: myslave
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: master-bin.000001
   Read_Master_Log_Pos: 608
    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: 
   Replicate_Ignore_DB: 

必須設(shè)置兩個(gè)從庫(kù)為只讀模式

mysql> set global read_only=1;

6:在 master 主庫(kù)插入兩條數(shù)據(jù),測(cè)試是否同步

mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)
mysql> use test_db;
Database changed
mysql> create table test(id int);
Query OK, 0 rows affected (0.13 sec)
mysql> insert into test(id) values (1);
Query OK, 1 row affected (0.03 sec)

7:在兩個(gè)從庫(kù)分別查詢?nèi)缦滤菊f(shuō)明主從同步正常

mysql> select * from test_db.test;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

四:安裝 MHA 軟件

1:所有服務(wù)器上都安裝 MHA 依賴的環(huán)境,首先安裝 epel 源(3+1)

[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# ll
總用量 20
drwxr-xr-x. 2 root root 187 10月 10 18:08 backup
-rw-r--r--. 1 root root 1458 12月 28 23:07 CentOS7-Base-163.repo
-rw-r--r--. 1 root root 951 12月 29 14:52 epel.repo
-rw-r--r--. 1 root root 1050 11月 1 04:33 epel.repo.rpmnew
-rw-r--r--. 1 root root 1149 11月 1 04:33 epel-testing.repo
-rw-r--r--. 1 root root 228 10月 27 18:43 local.repo

三臺(tái)數(shù)據(jù)庫(kù)加上一臺(tái) mha-manager

[root@mha-manager ~]# yum install epel-release --nogpgcheck
[root@mha-manager ~]# yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-ParalExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

2:在所有服務(wù)器上必須先安裝 node 組件 (3+1)

[root@mha-manager ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@mha-manager ~]# cd mha4mysql-node-0.57
[root@mha-manager mha4mysql-node-0.57]# perl Makefile.PL
[root@mha-manager mha4mysql-node-0.57]# make  make install

3:在 mha-manager 上安裝 manager 組件

[root@mha-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz 
[root@mha-manager ~]# cd mha4mysql-manager-0.57/
[root@mha-manager mha4mysql-manager-0.57]# perl Makefile.PL
[root@mha-manager mha4mysql-manager-0.57]# make  make install

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

masterha_check_ssh 檢查 MHA 的 SSH 配置狀況
masterha_check_repl 檢查 MySQL 復(fù)制狀況
masterha_manger 啟動(dòng) manager的腳本
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 添加或刪除配置的 server 信息
masterha_stop 關(guān)閉manager

node 安裝后也會(huì)在/usr/local/bin 下面會(huì)生成幾個(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 線程)

五:配置無(wú)密碼認(rèn)證

1:在 manager 上配置到所有節(jié)點(diǎn)的無(wú)密碼認(rèn)證

(1)生成密鑰

[root@mha-manager ~]# ssh-keygen -t rsa     # 一直回車(chē)

(2)生成密鑰之后發(fā)送給其他3個(gè)數(shù)據(jù)庫(kù)

[root@mha-manager ~]# ssh-copy-id 20.0.0.10  # 輸入:yes 密碼:123456
[root@mha-manager ~]# ssh-copy-id 20.0.0.11
[root@mha-manager ~]# ssh-copy-id 20.0.0.12 

(3)登錄測(cè)試

[root@mha-manager ~]# ssh root@20.0.0.10
Last login: Tue Dec 29 14:52:09 2020 from 20.0.0.1
[root@master ~]# exit
登出
Connection to 20.0.0.10 closed.
[root@mha-manager ~]# ssh root@20.0.0.11
Last login: Tue Dec 29 13:20:07 2020 from 20.0.0.1
[root@slave1 ~]# exit
登出
Connection to 20.0.0.11 closed.
[root@mha-manager ~]# ssh root@20.0.0.12
Last login: Tue Oct 27 19:45:24 2020 from 20.0.0.1
[root@slave2 ~]# exit
登出
Connection to 20.0.0.12 closed.

2:在 master 上配置到數(shù)據(jù)庫(kù)節(jié)點(diǎn)的無(wú)密碼認(rèn)證

(1)生成密鑰

[root@master ~]# ssh-keygen -t rsa

(2)生成密鑰之后發(fā)送給其他2個(gè)數(shù)據(jù)庫(kù)

[root@master ~]# ssh-copy-id 20.0.0.11
[root@master ~]# ssh-copy-id 20.0.0.12

(3)登錄測(cè)試

[root@master ~]# ssh root@20.0.0.11
Last login: Tue Dec 29 16:40:06 2020 from 20.0.0.13
[root@slave1 ~]# exit
登出
Connection to 20.0.0.11 closed.
[root@master ~]# ssh root@20.0.0.12
Last login: Tue Oct 27 23:05:20 2020 from 20.0.0.13
[root@slave2 ~]# exit
登出
Connection to 20.0.0.12 closed.

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

(1)生成密鑰

[root@slave1 ~]# ssh-keygen -t rsa

(2)生成密鑰之后發(fā)送給其他2個(gè)數(shù)據(jù)庫(kù)

[root@slave1 ~]# ssh-copy-id 20.0.0.10
[root@slave1 ~]# ssh-copy-id 20.0.0.12

(3)登錄測(cè)試

[root@slave1 ~]# ssh root@20.0.0.10
Last login: Tue Dec 29 16:39:55 2020 from 20.0.0.13
[root@master ~]# exit
登出
Connection to 20.0.0.10 closed.
[root@slave1 ~]# ssh root@20.0.0.12
Last login: Tue Oct 27 23:14:06 2020 from 20.0.0.10
[root@slave2 ~]# exit
登出
Connection to 20.0.0.12 closed.

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

(1)生成密鑰

[root@slave2 ~]# ssh-keygen -t rsa

(2)生成密鑰之后發(fā)送給其他2個(gè)數(shù)據(jù)庫(kù)

[root@slave2 ~]# ssh-copy-id 20.0.0.10
[root@slave2 ~]# ssh-copy-id 20.0.0.11

(3)登錄測(cè)試

[root@slave2 ~]# ssh root@20.0.0.10
Last login: Tue Dec 29 16:59:43 2020 from 20.0.0.11
[root@master ~]# exit
登出
Connection to 20.0.0.10 closed.
[root@slave2 ~]# ssh root@20.0.0.11
Last login: Tue Dec 29 16:48:51 2020 from 20.0.0.10
[root@slave1 ~]# exit
登出
Connection to 20.0.0.11 closed.

六:配置 MHA

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

(1)拷貝

[root@mha-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/

(2)拷貝后會(huì)有四個(gè)執(zhí)行文件

master_ip_failover #自動(dòng)切換時(shí) VIP 管理的腳本

master_ip_online_change #在線切換時(shí) vip 的管理

power_manager #故障發(fā)生后關(guān)閉主機(jī)的腳本

send_report #因故障切換后發(fā)送報(bào)警的腳本

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

[root@mha-manager scripts]# cp master_ip_failover /usr/local/bin/
[root@mha-manager scripts]# cd ..
[root@mha-manager bin]# ll
總用量 88

2:修改自動(dòng)切換腳本

[root@mha-manager ~]# vi /usr/local/bin/master_ip_failover  # 刪除里面所有內(nèi)容
#!/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 = '20.0.0.200';
my $brdc = '20.0.0.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";
}

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

[root@mha-manager ~]# mkdir /etc/mha
[root@mha-manager ~]# cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/mha
[root@mha-manager ~]# vi /etc/mha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
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
user=mha
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.11 -s 20.0.0.12
shutdown_script=""
ssh_user=root

[server1]
hostname=20.0.0.10
port=3306
[server2]
hostname=20.0.0.11
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=20.0.0.12
port=3306

七:健康檢查

1:測(cè)試 ssh 無(wú)密碼認(rèn)證,如果正常最后會(huì)輸出 successfully

[root@mha-manager ~]# masterha_check_ssh
--conf=server_config_file> must be set.
[root@mha-manager ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf
Tue Dec 29 20:19:16 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Dec 29 20:19:16 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Dec 29 20:19:16 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Dec 29 20:19:16 2020 - [info] Starting SSH connection tests..
Tue Dec 29 20:19:17 2020 - [debug] 
Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from root@20.0.0.10(20.0.0.10:22) to root@20.0.0.11(20.0.0.11:22)..
Tue Dec 29 20:19:16 2020 - [debug] ok.
Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from root@20.0.0.10(20.0.0.10:22) to root@20.0.0.12(20.0.0.12:22)..
Tue Dec 29 20:19:17 2020 - [debug] ok.
Tue Dec 29 20:19:18 2020 - [debug] 
Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from root@20.0.0.12(20.0.0.12:22) to root@20.0.0.10(20.0.0.10:22)..
Tue Dec 29 20:19:17 2020 - [debug] ok.
Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from root@20.0.0.12(20.0.0.12:22) to root@20.0.0.11(20.0.0.11:22)..
Tue Dec 29 20:19:18 2020 - [debug] ok.
Tue Dec 29 20:19:18 2020 - [debug] 
Tue Dec 29 20:19:16 2020 - [debug] Connecting via SSH from root@20.0.0.11(20.0.0.11:22) to root@20.0.0.10(20.0.0.10:22)..
Tue Dec 29 20:19:17 2020 - [debug] ok.
Tue Dec 29 20:19:17 2020 - [debug] Connecting via SSH from root@20.0.0.11(20.0.0.11:22) to root@20.0.0.12(20.0.0.12:22)..
Tue Dec 29 20:19:17 2020 - [debug] ok.
Tue Dec 29 20:19:18 2020 - [info] All SSH connection tests passed successfully.

2:測(cè)試 MySQL 主從連接情況,最后出現(xiàn) MySQL Replication Health is OK 字樣說(shuō)明

[root@mha-manager ~]# masterha_check_repl --conf=/etc/mha/app1.cnf
Tue Dec 29 20:30:29 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Dec 29 20:30:29 2020 - [info] Reading application default configuration from /etc/mha/app1.cnf..
Tue Dec 29 20:30:29 2020 - [info] Reading server configuration from /etc/mha/app1.cnf..
Tue Dec 29 20:30:29 2020 - [info] MHA::MasterMonitor version 0.57.
Tue Dec 29 20:30:30 2020 - [info] GTID failover mode = 0
Tue Dec 29 20:30:30 2020 - [info] Dead Servers:
Tue Dec 29 20:30:30 2020 - [info] Alive Servers:
Tue Dec 29 20:30:30 2020 - [info] 20.0.0.10(20.0.0.10:3306)
Tue Dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306)
Tue Dec 29 20:30:30 2020 - [info] 20.0.0.12(20.0.0.12:3306)
Tue Dec 29 20:30:30 2020 - [info] Alive Slaves:
Tue Dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306) Version=5.6.36-log (oldest major version between slaves) log-bin:enabled
.......省略
Checking the Status of the script.. OK 
Tue Dec 29 20:30:55 2020 - [info] OK.
Tue Dec 29 20:30:55 2020 - [warning] shutdown_script is not defined.
Tue Dec 29 20:30:55 2020 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

八:查看 master1 的 VIP 地址

查看 20.0.0.200 是否存在

這個(gè) VIP 地址不會(huì)因?yàn)閙anager 節(jié)點(diǎn)停止 MHA 服務(wù)而消失

第一次啟動(dòng)mha,主庫(kù)上并不會(huì)主動(dòng)的生成vip地址,需要手動(dòng)開(kāi)啟

[root@master ~]# ifconfig ens33:1 20.0.0.200/24 up
[root@master ~]# ip addr
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:8d:e2:af brd ff:ff:ff:ff:ff:ff
 inet 20.0.0.10/24 brd 20.0.0.255 scope global ens33
  valid_lft forever preferred_lft forever
 inet 20.0.0.200/24 brd 20.0.0.255 scope global secondary ens33:1
  valid_lft forever preferred_lft forever
 inet6 fe80::a6c1:f3d4:160:102a/64 scope link 
  valid_lft forever preferred_lft forever

九:?jiǎn)?dòng) MHA 并查看狀態(tài)

[root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  /dev/null > /var/log/masterha/app1/manager.log 2>1 
[1] 57152
[root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:57152) is running(0:PING_OK), master:20.0.0.10

故障模擬與修復(fù)

一:故障模擬

1:將 master 主服務(wù)器 down 掉

[root@master ~]# pkill mysqld

2:查看日志信息

[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log

master 20.0.0.10(20.0.0.10:3306) is down!  # 20.0.0.10 以停掉

Check MHA Manager logs at mha-manager:/var/log/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 20.0.0.10(20.0.0.10:3306)
The latest slave 20.0.0.11(20.0.0.11:3306) has all relay logs for recovery.
Selected 20.0.0.11(20.0.0.11:3306) as a new master.  # 20.0.0.11 成為主服務(wù)器
20.0.0.11(20.0.0.11:3306): OK: Applying all logs succeeded.
20.0.0.11(20.0.0.11:3306): OK: Activated master IP address.
20.0.0.12(20.0.0.12:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.

3:查看虛擬地址

虛擬地址已到 20.0.0.11 上面

[root@slave1 ~]# ip addr
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:49:77:39 brd ff:ff:ff:ff:ff:ff
 inet 20.0.0.11/24 brd 20.0.0.255 scope global ens33
  valid_lft forever preferred_lft forever
 inet 20.0.0.200/8 brd 20.255.255.255 scope global ens33:1
  valid_lft forever preferred_lft forever
 inet6 fe80::5cbb:1621:4281:3b24/64 scope link 
  valid_lft forever preferred_lft forever

4:查看主從狀態(tài)

查看主服務(wù)器的二進(jìn)制文件

[root@slave1 ~]# mysql

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 |  120 |    |     |     |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

查看從2的狀態(tài)

[root@slave2 ~]# mysql

mysql> show slave status\G;
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 20.0.0.11
     Master_User: myslave
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: master-bin.000003
   Read_Master_Log_Pos: 120
    Relay_Log_File: relay-log-bin.000002
    Relay_Log_Pos: 284
  Relay_Master_Log_File: master-bin.000003
    Slave_IO_Running: Yes
   Slave_SQL_Running: Yes
    Replicate_Do_DB: 
   Replicate_Ignore_DB: 

二:故障修復(fù)

1:開(kāi)啟 down 掉的數(shù)據(jù)庫(kù)

[root@master ~]# systemctl start mysqld
[root@master ~]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
 Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
 Active: active (running) since 二 2020-12-29 21:50:03 CST; 25s ago
  Docs: man:systemd-sysv-generator(8)
 Process: 977 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
 CGroup: /system.slice/mysqld.service
   ├─1026 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-fi...
   └─1358 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/m

2:down 掉的數(shù)據(jù)庫(kù)進(jìn)行主從復(fù)制

主從復(fù)制

[root@master ~]# mysql

mysql> change master to master_host='20.0.0.11',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120; 
Query OK, 0 rows affected, 2 warnings (0.01 sec)
# 20.0.0.11 是主服務(wù)器 down 掉之后成為的主服務(wù)器

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

查看狀態(tài)

mysql> show slave status\G;
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 20.0.0.11
     Master_User: myslave
     Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: master-bin.000003
   Read_Master_Log_Pos: 120
    Relay_Log_File: mysqld-relay-bin.000002
    Relay_Log_Pos: 284
  Relay_Master_Log_File: master-bin.000003
    Slave_IO_Running: Yes
   Slave_SQL_Running: Yes
    Replicate_Do_DB: 
   Replicate_Ignore_DB: 

3:修改 mha 配置文件

[root@mha-manager ~]# vi /etc/mha/app1.cnf

secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.10 -s 20.0.0.12
# 由于 20.0.0.11 成為主服務(wù)器,所以要添加 20.0.0.10 和 20.0.0.12 為從服務(wù)器
[server1]
hostname=20.0.0.10
candidate_master=1
check_repl_delay=0
port=3306
[server2]
hostname=20.0.0.11
port=3306
# 由于 20.0.0.10 down 掉,所以 server1 文件自動(dòng)刪除,重新添加 server1,并設(shè)為備選主服務(wù)器,server2 修改

4:進(jìn)入數(shù)據(jù)庫(kù)重新授權(quán)

[root@master ~]# mysql

mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5:再次啟動(dòng) mha

[root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover  /dev/null > /var/log/masterha/app1/manager.log 2>1 
[1] 58927
[root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:58927) is running(0:PING_OK), master:20.0.0.11

6:再次查看日志

[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log
......
Tue Dec 29 22:16:53 2020 - [info] Dead Servers:  # 停掉的服務(wù)
Tue Dec 29 22:16:53 2020 - [info] Alive Servers:  # 存活的服務(wù)
Tue Dec 29 22:16:53 2020 - [info] 20.0.0.10(20.0.0.10:3306)
Tue Dec 29 22:16:53 2020 - [info] 20.0.0.11(20.0.0.11:3306)
Tue Dec 29 22:16:53 2020 - [info] 20.0.0.12(20.0.0.12:3306)
.......

7:主數(shù)據(jù)庫(kù)寫(xiě)入數(shù)據(jù)同步并查看

其他數(shù)據(jù)庫(kù)都可以查到

mysql> create database ooo;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| mysql    |
| ooo    |
| performance_schema |
| test    |
| test_db   |
+--------------------+
6 rows in set (0.00 sec)

到此這篇關(guān)于MySQL 搭建MHA架構(gòu)部署的步驟的文章就介紹到這了,更多相關(guān)MySQL 搭建MHA架構(gòu)部署內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 如何通過(guò)Python收集MySQL MHA 部署及運(yùn)行狀態(tài)信息的功能
  • MySQL高可用架構(gòu)之MHA架構(gòu)全解
  • MySQL之MHA高可用配置及故障切換實(shí)現(xiàn)詳細(xì)部署步驟
  • MySQL 有關(guān)MHA搭建與切換的幾個(gè)錯(cuò)誤log匯總
  • Mysql GTID Mha配置方法
  • MySQL下高可用故障轉(zhuǎn)移方案MHA的超級(jí)部署教程
  • MHA實(shí)現(xiàn)mysql主從數(shù)據(jù)庫(kù)手動(dòng)切換的方法
  • MySQL MHA 運(yùn)行狀態(tài)監(jiān)控介紹

標(biāo)簽:吉林 甘南 隨州 黑河 荊州 滄州 錦州 資陽(yáng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL 搭建MHA架構(gòu)部署的步驟》,本文關(guān)鍵詞  MySQL,搭建,MHA,架構(gòu),部署,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL 搭建MHA架構(gòu)部署的步驟》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MySQL 搭建MHA架構(gòu)部署的步驟的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章