MySQL MHA

MySQL高可用架构之基于MHA的搭建

MySQL MHA架构介绍:

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以单独部署在一*立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器,一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。(出自:《深入浅出MySQL(第二版)》)

下图展示了如何通过MHA Manager管理多组主从复制。

MySQL MHA

 

可以将MHA工作原理总结为如下:

(1)从宕机崩溃的master保存二进制日志事件(binlog events);

(2)识别含有最新更新的slave;

(3)应用差异的中继日志(relay log)到其他的slave;

(4)应用从master保存的二进制日志事件(binlog events);

(5)提升一个slave为新的master;

(6)使其他的slave连接新的master进行复制;

MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下。

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              添加或删除配置的server信息

Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具:

save_binary_logs                保存和复制master的二进制日志

apply_diff_relay_logs           识别差异的中继日志事件并将其差异的事件应用于其他的slave

filter_mysqlbinlog              去除不必要的ROLLBACK事件(MHA已不再使用这个工具)

purge_relay_logs                清除中继日志(不会阻塞SQL线程)

  • 环境准备
系统 ip 名称
centos7 192.168.10.214 master
centos7 192.168.10.215 salve1
centos7 192.168.10.216 slave2
centos7 192.168.10.217 monitor

1.在四台主机上更改主机名

master

[root@localhost ~]# hostnamectl set-hostname master

[root@localhost ~]# bash

slave1

[root@localhost ~]# hostnamectl set-hostname salve1

[root@localhost ~]# bash

slave2

[root@localhost ~]# hostnamectl set-hostname slave2

[root@localhost ~]# bash

monitor

[root@localhost ~]# hostnamectl set-hostname monitor

[root@localhost ~]# bash

2.四台服务器互相配置SSH免密码登录(注意是互相)

四台服务器都运行ssh-keygen -t rsa

master

[root@master ~]# ssh-copy-id root@192.168.10.214

[root@master ~]# ssh-copy-id root@192.168.10.215

[root@master ~]# ssh-copy-id root@192.168.10.216

[root@master ~]# ssh-copy-id root@192.168.10.217

salve1

[root@salve1 ~]#ssh-copy-id root@192.168.10.214

[root@salve1 ~]#ssh-copy-id root@192.168.10.215

[root@salve1 ~]# ssh-copy-id root@192.168.10.216

[root@salve1 ~]# ssh-copy-id root@192.168.10.217

salve2

​[root@salve2 ~]#ssh-copy-id root@192.168.10.214

[root@salve2 ~]#ssh-copy-id root@192.168.10.215

[root@salve2 ~]# ssh-copy-id root@192.168.10.216

[root@salve2 ~]# ssh-copy-id root@192.168.10.217

monitor

[root@monitor ~]#ssh-copy-id root@192.168.10.214

[root@monitor ~]#ssh-copy-id root@192.168.10.215

[root@monitor ~]# ssh-copy-id root@192.168.10.216

[root@monitor ~]# ssh-copy-id root@192.168.10.217

3.搭建MySQL主从复制环境

master

[root@master ~]#cat <<END >> /etc/my.cnf         

server-id=1

log-bin=mysql-bin

log-salve-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

symbolic-links=0

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

END

[root@master ~]#systemctl restart mysqld

[root@master ~]#mysql -u root -p123123

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000006 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

mysql> grant replication slave on *.* to 'repl'@'192.168.10.%' identified by '123456';

mysql> flush privileges;

mysql> create databases nmf;

mysql> grant all privileges on *.* to 'root'@'192.168.10.%' identified by '123456';

mysql> flush privileges;

salve1

[root@salve1 ~]#cat <<END >> /etc/my.cnf         

server-id=2

log-bin=mysql-bin

log-salve-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

symbolic-links=0

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

END

[root@salve1 ~]#systemctl restart mysqld

[root@salve1 ~]#mysql -u root -p123123

mysql> change master to master_host='192.168.10.214',master_user='repl',master_password='123456',master_log_file='mysql-bin.000006' ,master_log_pos=154;

mysql> start slave;

mysql> show slave status\G;

……

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

……

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| asd                |

| mysql              |

| nmf                |

| performance_schema |

| sys                |

+--------------------+

salve2

[root@salve2 ~]#cat <<END >> /etc/my.cnf         

server-id=3

log-bin=mysql-bin

log-salve-updates

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

symbolic-links=0

relay-log=relay-log-bin

relay-log-index=slave-relay-bin.index

END

[root@salve2 ~]#systemctl restart mysqld

[root@salve2 ~]#mysql -u root -p123123

mysql> change master to master_host='192.168.10.214',master_user='repl',master_password='123456',master_log_file='mysql-bin.000006' ,master_log_pos=154;

mysql> start slave;

mysql> show slave status\G;

……

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

……

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| asd                |

| mysql              |

| nmf                |

| performance_schema |

| sys                |

+--------------------+

4.安装MHA

安装MHA的perl依赖包

[root@master ~]# yum -y install perl-DBD-MySQL perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

[root@salve1 ~]# yum -y install perl-DBD-MySQL perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

[root@salve2 ~]# yum -y install perl-DBD-MySQL perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

[root@monitor ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

软件包 perl-DBD-MySQL-4.023-5.el7.x86_64 已安装并且是最新版本

没有可用软件包 perl-Config-Tiny。

没有可用软件包 perl-Log-Dispatch。

没有可用软件包 perl-Parallel-ForkManager。

软件包 4:perl-Time-HiRes-1.9725-3.el7.x86_64 已安装并且是最新版本

无须任何处理

[root@monitor ~]# ls

666.sh  anaconda-ks.cfg perl-Log-Dispatch

[root@monitor ~]# cat <<END >> /etc/yum.repos.d/linux.repo



[perl]

name=perl

baseurl=file:///root/perl-Log-Dispatch

enabled=1

gpgcheck=0

END

[root@monitor ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

[root@monitor ~]# yum -y install perl-DBD-MySQL perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

master salve1 salve2 monitor 上安装MHA Node软件包

[root@master ~]# tar zxvf mha4mysql-node-0.56.tar.gz

[root@master ~]# cd mha4mysql-node-0.56

[root@master mha4mysql-node-0.56]# perl Makefile.PL

[root@master mha4mysql-node-0.56]# make && make install

在monitor上安装MHA Manager软件包

[root@monitor ~]# tar zxvf mha4mysql-manager-0.56.tar.gz

[root@monitor ~]# cd mha4mysql-manager-0.56

[root@monitor mha4mysql-manager-0.56]# perl Makefile.PL

[root@monitor mha4mysql-manager-0.56]# make && make install

NO

NO

[root@monitor mha4mysql-manager-0.56]# cp samples/scripts/* /usr/local/bin

5.配置MHA

创建MHA的工作目录并创建相关配置文件

[root@monitor ~]# mkdir -p /etc/masterha

[root@monitor ~]# cp /root/mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/masterha/

修改配置文件

[root@monitor ~]# vim /etc/masterha/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=123456

user=root

ping_interval=1

remote_workdir=/tmp

repl_password=123456

repl_user=repl

report_script=/usr/local/bin/send_report

secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.10.214 -s 192.168.10.215 -s 192.168.10.216

shutdown_script=""

ssh_user=root

[server1]

hostname=192.168.10.214

port=3306



[server2]

hostname=192.168.10.215

port=3306

candidate_master=1

check_repl_delay=0

[server3]

hostname=192.168.10.216

port=3306

no_master=1



[server4]

hostname=host4

no_master=1

清除两台从服务器的中继日志

[root@salve1 ~]# mysql -u root -p123123 -e "set global relay_log_purge=0"

[root@salve2 ~]# mysql -u root -p123123 -e "set global relay_log_purge=0"

修改monitor脚本

[root@monitor ~]# cd /usr/local/bin/
[root@monitor bin]# rm -rf master_ip_failover 
上传一个master_ ip_ failover并给予执行权限
[root@monitor bin]# chmod a+x master_ip_failover 
[root@monitor bin]# vim master_ip_failover 
……
my $vip = '192.168.10.250/24'; # Virtual IP
my $gateway = '192.168.10.254'; #Gateway IP
my $interface = 'ens33';
……

检查SSH配置

[root@monitor bin]# masterha_check_ssh --conf=/etc/masterha/app1.cnf

Can't locate MHA/NodeConst.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/MHA/ManagerConst.pm line 25.

BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/ManagerConst.pm line 25.

Compilation failed in require at /usr/local/share/perl5/MHA/Server.pm line 27.

BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/Server.pm line 27.

Compilation failed in require at /usr/local/share/perl5/MHA/Config.pm line 29.

BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/Config.pm line 29.

Compilation failed in require at /usr/local/share/perl5/MHA/SSHCheck.pm line 32.

BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/SSHCheck.pm line 32.

Compilation failed in require at /usr/local/bin/masterha_check_ssh line 25.

BEGIN failed--compilation aborted at /usr/local/bin/masterha_check_ssh line 25.

[root@monitor bin]# yum -y install perl_DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-YAML-Tiny perl-PAR-Dist perl-Module-ScanDeps perl-Module-CoreList perl-Module-Build perl-CPAN perl-CPANPLUS perl-File-Remove perl-Module-Install

[root@monitor mha4mysql-node-0.56]# masterha_check_ssh --conf=/etc/masterha/app1.cnf

Sat Dec 25 23:09:39 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Sat Dec 25 23:09:39 2021 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Sat Dec 25 23:09:39 2021 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Sat Dec 25 23:09:39 2021 - [info] Starting SSH connection tests..

Sat Dec 25 23:09:41 2021 - [debug]

Sat Dec 25 23:09:39 2021 - [debug]  Connecting via SSH from root@192.168.10.214(192.168.10.214:22) to root@192.168.10.215(192.168.10.215:22)..

Sat Dec 25 23:09:40 2021 - [debug]   ok.

Sat Dec 25 23:09:40 2021 - [debug]  Connecting via SSH from root@192.168.10.214(192.168.10.214:22) to root@192.168.10.216(192.168.10.216:22)..

Sat Dec 25 23:09:41 2021 - [debug]   ok.

Sat Dec 25 23:09:41 2021 - [debug]

Sat Dec 25 23:09:39 2021 - [debug]  Connecting via SSH from root@192.168.10.215(192.168.10.215:22) to root@192.168.10.214(192.168.10.214:22)..

Sat Dec 25 23:09:40 2021 - [debug]   ok.

Sat Dec 25 23:09:40 2021 - [debug]  Connecting via SSH from root@192.168.10.215(192.168.10.215:22) to root@192.168.10.216(192.168.10.216:22)..

Sat Dec 25 23:09:41 2021 - [debug]   ok.

Sat Dec 25 23:09:42 2021 - [debug]

Sat Dec 25 23:09:40 2021 - [debug]  Connecting via SSH from root@192.168.10.216(192.168.10.216:22) to root@192.168.10.214(192.168.10.214:22)..

Sat Dec 25 23:09:41 2021 - [debug]   ok.

Sat Dec 25 23:09:41 2021 - [debug]  Connecting via SSH from root@192.168.10.216(192.168.10.216:22) to root@192.168.10.215(192.168.10.215:22)..

Sat Dec 25 23:09:42 2021 - [debug]   ok.

Sat Dec 25 23:09:42 2021 - [info] All SSH connection tests passed successfully.

检查主从复制环境状况

在master、slaver1、slaver2 上都做一遍软连接

ln -s /usr/local/mysql/bin/* /usr/local/bin/

[root@monitor ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf

……
MySQL Replication Health is OK.

5.开启MHA Manager监控

[root@manger ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf  --remove_dead_master_conf --ignore_last_failover /var/log/masterha/app1/manager.log 2>&1 &                   
[1] 38422
[root@manger ~]# nohup: 忽略输入并把输出追加到"nohup.out"     #回车
[root@manger ~]# jobs -l                         
[1]+ 38422 运行中               nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover /var/log/masterha/app1/manager.log 2>&1 &

6.查看监控状态

[root@manger ~]# masterha_check_status --conf=/etc/masterha/app1.cnf                    

app1 (pid:38422) is running(0:PING_OK), master:192.168.10.214

上一篇:嵌入式Linux之vs code开发环境搭建


下一篇:Dubbo03【管理控制台和监控中心搭建】,架构师带你玩转Redis高性能缓存设计实战