三.MHA架构
四.部署MHA
环境要求:四台虚拟机server1是master,server2是备选master,server3和server4是slave
在所有节点安装MHA Node所需的perl模块,需要软件包perl-DBD-Mysql 然后yum install
在所有节点安装mha node,需要软件包mha4mysql-node-0.53.tar.gz
tar xf mha4mysql-node-0.53.tar.gz
cd mha4mysql-node-0.53
Perl Makefile.PL
make && make install
安装MHA Manager
首先安装MHA Manager依赖和perl模块
yum install perl-DBD-Mysql perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
需要软件包mha4mysql-manager-0.53.tar.gz
tar xf mha4mysql-manager-0.53.tar.gz
cd mha4mysql-manager-0.53
Perl Makefile.PL
make && make install
安装完成后在/usr/local/bin下生成脚本文件
进入 cd /root/mha4mysql-manager-0.53/samples/scripts下
将master_ip_failover master_ip_online_change复制到/usr/local/bin下,与之前生成的脚本合在一起
配置SSH登录无密码验证
搭建主从复制环境
在server2上执行备份:mysqldump --master-data=2 --sigle-transaction -R --triggers -A > all.sql
--master-data=2 ##备份时刻记录master的binlog的位置和position
--single-transaction ##获取一致性快照
-R ##备份存储过程和函数
--tiggers ##备份触发器
-A ##备份所有的库
在server2上创建复制用户:grant replication slave on *.* to ‘repl’@‘172.25.15.%’ identified by ‘westos’;
flush privileges
查看主库备份时的binlog名称和位置,MASTER_LOG_FILE和MASTER_LOG_POS:
Head -n 30 all.sql | grep ‘CHANGE MASTER TO’
把备份复制到server3和server4
Scp all.sql server3:/data
Scp all.sql server4:/data
导入备份到server3,执行复制相关命令
mysql < /data/all.sql
CHANGE MASTER TO MASTER—HOST=‘172.2515.3’,MASTER_USER=‘repl’,MASTER_PASSWORD=‘westos’,MASTER_LOG_FILE=‘mysql-bin.000010’,MASTER_LOG_POS=112;
start slave
在server4上也搭建复制环境
mysql < /data/all.sql
CHANGE MASTER TO MASTER—HOST=‘172.2515.3’,MASTER_USER=‘repl’,MASTER_PASSWORD=‘westos’,MASTER_LOG_FILE=‘mysql-bin.000010’,MASTER_LOG_POS=112;
start slave
两台slave服务器server3和server4设置read_only
Mysql -e ‘set global read_only=1’
在master上创建监控用户
Grant all privileges on *.* to ‘root’@‘172.25.15.%’identified by ‘westos’;
flush privileges
集群环境搭建完毕
五.配置MHA
创建MHA工作目录,并且创建相关配置文件
mkdir -p /etc/mastermha
cp mha4mysql-manager-0.53/samples/conf/appl.cnf /etc/mastermha
修改app1.cnf配置文件
manager_workdir=/var/log/mastermha/app1.log ##设置manager的工作目录
manager_log=/var/log/mastermha/app1.log ##设置manager的日志
master_binlog_dir=/data/mysql ##设置master保存binlog的位置
master_ip_failover_script=/usr/local/bin/master_ip_failover ##设置自动failover时候的切换脚本
master_ip_online_change_script=/usr/local/bin/master_ip_online_change ##设置手动切换时候的切换脚本
password=westos ##设置mysql中root用户的密码
user=root ##设置监控用户root
ping_interval=1 ##设置监控主库,发送ping包的时间间隔默认是3秒,尝试3次没有回应的时候自动进行railover
remote_workdir=/tmp ##设置远端mysql在发生切换时binlog的保存位置
repl_password=westos ##设置复制用户的密码
repl_user=repl ##设置复制环境中的复制用户名
report_script=/usr/local/send_report ##设置发生切换后发送的报警的脚本
secondary_check_script=/usr/local/bin/mastermha_secondary_check -s server3 -s server2
shutdown_script=“” ##设置故障发生后关闭故障主机脚本
ssh_user=root ##设置ssh的登录用户名
[server1]
hostname=172.25.15.1
port=3306
[server2]
hostname=172.25.15.2
port=3306
candidate_master=1 ##设置为候选master。如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中最新的slave
check_repl_delay=0 ##默认情况下如果一个slave落后master100M的relay logs的话
[server3]
hostname=172.25.15.3
port=3306
修改完成
检查ssh配置
mastermaha_check_ssh --conf=/etc/mastermha/app1.cnf
检查整个复制环境状况
mastermaha_check_repl --conf=/etc/mastermha/app1.cnf
检查MHA Manager的状态
mastermaha_check_status --conf=/etc/mastermha/app1.cnf
开启MHA Manager监控
nohup mastermha_manager --conf=/etc/mastermha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mastermha/app1/manager.log 2>&1 &
--remove_dead_master_conf ##该参数表示当发生主从切换后,老的主库的ip将会从配置文件中移除
--manager.log ##日志存放位置
--ignore_last_failover ##缺省情况下,MHA监测到连续宕机,且两次间隔不足8小时,则不会进行failover
六.配置vip
配置vip可以采用两种方式,一种通过keepalived的方式,另一种通过脚本方式启动虚拟ip
需要软件包keepalived-1.2.12.tar.gz
tar xf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure --prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
在master配置keepalived的配置文件
Vim /etc/keepalived/keepalived.conf
Global_defs {
notification_email {
root@localhost
}
notification_email_from [email protected]
smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Mysql-HA
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 150
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
Virtual_ipaddress {
172.25.15.100
router_id Mysql-HA ##设定keepalived组的名称
172.25.15.100这个虚拟IP绑定到该主机的eth1网卡上,并且设置为backup状态
在候选master上也如此配置,但优先级更低
启动keepalived服务 /etc/init.d/keepalived start
七.MHA引入keepalived
在主库上编辑脚本/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 = '172.25.0.100/24';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$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,
);
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 \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
Print
检查集群状态 master_check_repl --conf=/etc/mastermha/app1.cnf
八.MHA的工作情况
1.自动failover
开启slave上的IO线程 start slave io_thread
杀掉主库mysql进程,进行自动failover kill -9 mysqld
2.手动failover
手动切换命令 mastermha_master_switch --master_state=dead --conf=/etc/mastermha/app1.cnf --dead_master_host=172.25.15.1 --dead_master_port=3306 --new_master_host=172.25.15..2 --new_master_port=3306
--ignore_last_failover
3.在线切换
停掉MHA监控 master_stop --conf=/etc/mastermha/app1.cnf
在线切换操作 mastermha_master_switch --conf=/etc/mastermha/app1.cnf --master_state=alive --new_master_host=172.25.15.2 --new_master_port=3306 --orig_master_is_new_slave --running_update_limit=10000
--orig_master_is_new_slave ##将原master变为slave
--running_update_limit=10000 ##故障切换时,延迟在此时间范围内都可切换