前言:首先介绍一下mha,引用自网络。
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案。
该软件由两部分组成: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,另外一台充当从库。
正文:
一、系统环境
系统:centos6
mysql版本:5.6.20
IP | 主机名 | 状态 |
10.10.1.12 | DB-01 | 主 |
10.10.0.5 | DB-02 | 候选主 |
10.10.0.25 | DFS-01 | 备 |
二、准备
mysql主从安装可参考我上一篇博客(环境不同,参考即可):http://wangwq.blog.51cto.com/8711737/1956819
主库和候选主库添加半同步
安装插件:
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME'semisync_master.so';
启动模块:
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
设置超时时间:
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 1000;
安装插件:
msyql> INSTALL PLUGIN rpl_semi_sync_slave SONAME'semisync_slave.so';
启动模块:
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
启动前查看相关信息
mysql> show variables like'rpl%';
几个用到的用户:
mysql>grant all privileges on *.* to hk_mhaap@'%' identified by '9uqZ51eraKFiEknho3Nw'; \\ 添加mha管理用户,赋予所有库所有权限。 mysql>grant replication slave on *.* to mysqlsync@'%' identified by 'mysqlsync'; \\ 添加mysql同步用户 mysql>flush privileges;
mysql 删除用户为空的帐户
mysql>delete from mysql.user where user=''; mysql>flush privileges;
如果不删除空账户,后面会报错。
三、主机添加hosts文件
四、上传mha文件
mha4mysql-node-0.54-0.el6.noarch.rpm
mha4mysql-manager-0.55-0.el6.noarch.rpm
五、安装mha
node安装在每一台mysql服务器上,manager用于管理,这里安装在备库。
安装node节点:
解决依赖关系
yum install -y perl-DBD-MySQL
安装node
rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
安装manager:
解决依赖关系
yum install -y epel-release
yum install -y perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-DBD-MySQL perl-Time-HiRes
脚本需要的依赖包
yum -y install expect mailx
安装manager
rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm
六、配置manager
1. 配置文件
MHA管理服务部署在10.10.0.25上面,此服务器为mysql从库。MHA manager配置文件需手动创建。
vi /etc/masterha/masterha_default.cnf [server default] user=hk_mhaap password=9uqZ51eraKFiEkn ssh_user=root repl_user=mysqlsync repl_password=mysqlsync ping_interval=30 manager_workdir=/var/log/masterha/app1 manager_log=/var/log/masterha/app1/app1.log master_ip_failover_script=/etc/masterha/scripts/master_ip_failover_script.sh report_script=/etc/masterha/scripts/email.sh [server1] hostname=10.10.1.12 master_binlog_dir=/data candidate_master=1 [server2] hostname=10.10.0.5 master_binlog_dir=/data candidate_master=1 [server3] hostname=10.10.0.25 master_binlog_dir=/data no_master=1
2. 添加监控脚本
主库故障切换脚本:
cat /etc/masterha/scripts/master_ip_failover_script.sh #!/bin/bash para_num=$# para=$@ #--orig_master_ip 当前含有VIP的主机 #--command 执行的命令 #--ssh_user ssh的用户 #--new_master_ip 准备配置VIP的主机 #日志路径 log=/tmp/remote.log for i in $para do #command keyword echo $i | grep command &>/dev/null end=$? if [ $end -eq 0 ] then command=$(echo $i|cut -d\= -f2) # echo $command fi #orig_master_ip keyword echo $i | grep orig_master_ip &>/dev/null end1=$? if [ $end1 -eq 0 ] then orig_master_ip=$(echo $i|cut -d\= -f2) # echo $orig_master_ip fi #ssh_user keyword echo $i|grep ssh_user &>/dev/null end2=$? if [ $end2 -eq 0 ] then ssh_user=$(echo $i|cut -d\= -f2) # echo $ssh_user fi #new_master_ip keyword echo $i|grep new_master_ip &>/dev/null end3=$? if [ $end3 -eq 0 ] then new_master_ip=$(echo $i|cut -d\= -f2) # echo $new_master_ip fi done #连接新主,配置VIP connection_start() { /usr/bin/expect -c " set timeout 30 spawn ssh -l $ssh_user $new_master_ip expect \"*#\" send \"sudo /etc/init.d/keepalived start\r\" expect \"*#\" send \"exit\r\" " } >> $log #连接旧主,去除VIP connection_stop() { /usr/bin/expect -c " set timeout 30 spawn ssh -l $ssh_user $orig_master_ip expect \"*#\" send \"sudo /etc/init.d/keepalived stop\r\" expect \"*#\" send \"exit\r\" " } >> $log #连接当前主,查看状态 connection_status() { /usr/bin/expect -c " set timeout 30 spawn ssh -l $ssh_user $orig_master_ip expect \"*#\" send \"sudo /etc/init.d/keepalived status\r\" expect \"*#\" send \"exit\r\" " } 2>&1 case $command in start) connection_start ;; stopssh|stop) connection_stop ;; status) connection_status ;; esac
邮件脚本:
cat /etc/masterha/scripts/email.sh #!/bin/bash [email protected] [email protected] body=$@ result_body=${body##*=} subject=$(echo ${body%%body*}) result_subject=$(echo ${subject##*=}) echo $result_body |mail -s "$result_subject" -r $mail_from $admin_email
给脚本赋予执行权限
chmod +x /etc/masterha/scripts/*
七、测试mha
1. 验证ssh互相认证是否成功:
masterha_check_ssh --conf=/etc/masterha/masterha_default.cnf
注:一定要注意本机也要添加自己的秘钥,不然会报错。
2. 验证mysql主从复制
masterha_check_repl --conf=/etc/masterha/masterha_default.cnf
报错1:
Thu Jun 15 1:20:37 2017 - [error][/usr/local/perl5/vender_perl/MHA/ServerManger.pm, ln255] Got MySQL error when connecting 10.10.0.25(10.10.0.25:3306):1045:Access denied for user 'hk_mhaap'@'DB-01' (using password:YES),but this is not mysql crash. Check MySQL server setings.
at /usr/share/perl5/vender_perl/MHA/ServerManger.pm line 251
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln263] Got fatal error, stopping operations
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln386] Error happend on checking configurations.
at /usr/share/perl5/vender_perl/MHA/ServerManger.pm line 300
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln482] Error happened on monitoring servers.
Thu Jun 15 1:20:37 2017 - [info] Got exit code 1 (Not master dead).
MySQL Replication health is NOT OK!
=====原因:mysql的用户表里存在user为空的权限设置(上述提到过):
mysql>delete from mysql.user where user=''; mysql>flush privileges;
报错2:
Can't exec "mysqlbinlog": No such file or directory at /usr/share/perl5/vender_perl/MHA/ServerManger.pm line 99.
mysqlbinlog version not found!
at /usr/bin/apply_diff_relay_logs line 482
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln195] Slaves settings check failed!
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln375] Slave configuration failed.
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln386] Error happened on check configurations. at /usr/bin/masterha_check_repl line 48
Thu Jun 15 1:20:37 2017 - [error][/usr/share/perl5/vender_perl/MHA/ServerManger.pm, ln482] Error happened on monitoring servers.
Thu Jun 15 1:20:37 2017 - [info] Got exit code 1 (Not master dead).
MySQL Replication health is NOT OK!
====原因:没找到mysqlbinlog命令,这里设置环境变量并不管用,需要做软链接:
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
测试成功:
验证ok,后面运行keepalived的命令是脚本自动运行的,暂可忽略,跟上下输出内容无关。
八、启动服务
nohup masterha_manager --conf=/etc/masterha/masterha_default.cnf > /tmp/mha_manager.log &1 &
查看日志:
因为有些功能没有添加脚本,所以会有警告。
九、keepalived安装配置
wget tar xzf keepalived-1.3.5.tar.gz cd keepalived-1.3.5 ./configure --prefix=/home/szjruser/keepalived yum install openssl-devel ./configure --prefix=/home/szjruser/keepalived make make install ln -s /home/szjruser/keepalived/sbin/keepalived /usr/sbin/keepalived cp /home/szjruser/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir -p ~/keepalived/lock/subsys mkdir /etc/keepalived cp /home/szjruser/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ chkconfig --add keepalived chkconfig keepalived on chmod 755 /etc/init.d/keepalived
修改配置文件:
10.10.1.12
10.10.0.5
启动服务:
/etc/init.d/keepalived start