MHA安装配置

 
 
环境:
\
 
 
管理MasterIP地址
HA方案中,很多情况下人们会在master上绑定一个虚拟IP。当master崩溃的时候,软件比如Keepalived会将虚拟IP重新指向正常的Server。
通用的方法就是创建一个全局的目录库,在库中存放着所有应用和IP地址之间的映射关系,用以取代VIP。在这种方案下,如果master崩溃,那么你就需要修改这个目录库。
两种方案都各有优缺点,MHA不会强制使用哪一种。MHA可以调用其他的脚本来禁用\激活write ip地址,通过设置master_ip_failover_script 脚本的参数,该脚本可在manager节点中找到。你可以在该脚本中更新目录库,或者实现VIP漂移等任何你想干的事。你同样可以借用现有的HA方案的软 件实现IP故障转移,比如Pacemaker,在这种情况下MHA将不会做IP故障转移。
MHA工作原理总结为以下几条:
 
(1)从宕机崩溃的master保存二进制日志事件(binlog events);
(2)识别含有最新更新的slave;
(3)应用差异的中继日志(relay log) 到其他slave;
(4)应用从master保存的二进制日志事件(binlog events);
(5)提升一个slave为新master;
(6)使用其他的slave连接新的master进行复制。
 
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线程)
 
一主二从
172.17.103.115   manager
172.17.103.116   node  master
172.17.103.117   node  slave
172.17.103.119   node  slave
 
 操作步骤:
A. 首先用ssh-keygen实现四台主机之间相互免密钥登录
B. 安装MHAmha4mysql-node,mha4mysql-manager 软件包
C. 建立master,slave1,slave2之间主从复制
D. 管理机manager上配置MHA文件
E. masterha_check_ssh工具验证ssh信任登录是否成功
F. masterha_check_repl工具验证mysql复制是否成功
G. 启动MHA manager,并监控日志文件
H. 测试master(156)宕机后,是否会自动切换
 
在/usr/bin下:
- masterha_check_ssh : 检查MHA的SSH配置。
- masterha_check_repl : 检查MySQL复制。
- masterha_manager : 启动MHA。
- masterha_check_status : 检测当前MHA运行状态。
- masterha_master_monitor : 监测master是否宕机。
- masterha_master_switch : 控制故障转移(自动或手动)。
- masterha_conf_host : 添加或删除配置的server信息。 
 
- save_binary_logs : 保存和复制master的二进制日志。
- apply_diff_relay_logs : 识别差异的中继日志事件并应用于其它slave。
- filter_mysqlbinlog : 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)。
- purge_relay_logs : 清除中继日志(不会阻塞SQL线程)。 
 
一,配置ssh免秘钥:
 
如果manager和slave在同一台机子上了,但是manger依旧通过SSH连接到slave,所以你依旧需要配置SSH无密码登陆
比如:
172.17.103.116 为slave,也为manager
shell > ssh-keygen -t rsa  #创建密钥
shell > ssh-copy-id -i ~/.ssh/id_rsa.pub 172.17.103.116  #发送ssh密钥到其他服务器
shell > ssh-copy-id -i ~/.ssh/id_rsa.pub 172.17.103.116 #发送ssh密钥到自己
 
在manager上
在manager上172.17.103.115配置到个Node的无密码验证:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.116
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.117
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.119
 
在Node:172.17.103.116上:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.117
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.119
 
在Node:172.17.103.117上:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.116
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.119
 
在Node:172.17.103.119上:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.116
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.103.117
或者
 
在原端:
 $ /usr/bin/ssh-keygen -t rsa
$ /usr/bin/ssh-keygen -t dsa
 
$ touch ~/.ssh/testkey
 
$ cat /home2inst1/.ssh/id_rsa.pub >> testkey
$ cat /home2inst1/.ssh/id_dsa.pub >> testkey  
 
通过scp或其他ftp方式把testky传到192.168.30.83下面的.ssh目录
 
在目标端:
追加密钥
cat testkey>>authorized_keys (注意要追加到该文件,否则会影响之前配置的ssh关系)
 
二, 安装mha node与mha node manager
##先安装 MHA Node
 
yum install perl-DBD-MySQL
rpm -ivh mha4mysql-node-X.Y-0.noarch.rpm
 
 
## Install DBD::mysql if not installed
tar -zxf mha4mysql-node-X.Y.tar.gz
perl Makefile.PL
make
sudo make install
 
##再安装 MHA Manager
 
centos:
 wget ftp://ftp.muug.mb.ca/mirror/CentOS/5.10/os/x86_64/CentOS/perl-5.8.8-41.el5.x86_64.rpm
 wget ftp://ftp.muug.mb.ca/mirror/centos/6.5/os/x86_64/Packages/compat-db43-4.3.29-15.el6.x86_64.rpm
 wget http://downloads.naulinux.ru/pub/NauLinux/6x/i386/sites/School/RPMS/perl-Log-Dispatch-2.27-1.el6.noarch.rpm
 wget http://dl.Fedoraproject.org/pub/epel/6/i386/perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
 wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
 wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
 wget http://mirror.centos.org/centos/6/os/x86_64/Packages/perl-Time-HiRes-1.9721-136.el6.x86_64.rpm
 
 
 ## Install dependent Perl modules
 yum install perl-DBD-MySQL
 yum install perl-Config-Tiny
 yum install perl-Log-Dispatch
 yum install perl-Parallel-ForkManager
yum install -y rrdtool perl-rrdtool rrdtool-devel perl-Params-Validate
 yum install perl-Config-Tiny
yum install perl-* --skip-broken  安装perl所有的文件;
 
 
在suse环境下用源码方法安装:
 
#perl -MCPAN -e shell
mcpan>install Log::Dispatch
mcpan>install Parallel::ForkManager 
mcpan>install YAML 
 
------------------------------
 
 
## Install MHA Node, since MHA Manager uses some modules provided by MHA Node.
  # rpm -ivh mha4mysql-node-X.Y-0.noarch.rpm
 
  ## Finally you can install MHA Manager
  # rpm -ivh mha4mysql-manager-X.Y-0.noarch.rpm
 
###You can also install MHA Manager from source.
  ## Installing MHA Manager
  $ tar -zxf mha4mysql-manager-X.Y.tar.gz
  $ perl Makefile.PL
  $ make
  $ sudo make install
 
三:建立master,slave1,slave2之间主从复制,
1,在所有的mysql机器上执行:
在主116上:grant replication slave, replication client on *.* to 'repl'@'172.17.103.%' identified by 'repl12’;
在主117上:grant replication slave, replication client on *.* to 'repl'@'172.17.103.%' identified by 'repl12’;
在主119上:grant replication slave, replication client on *.* to 'repl'@'172.17.103.%' identified by 'repl12’;
2 manager管理node的权限,在所有mysql机器上执行:
在主116上:grant all privileges on *.* to 'mha'@'172.17.103.%' identified by ‘mha1234’;
在主117上:grant all privileges on *.* to ‘mha'@'172.17.103.%' identified by ‘mha1234’;
在主119上:grant all privileges on *.* to ‘mha'@'172.17.103.%' identified by ‘mha1234’;
 
 mysqldump  --master-data=2 --single-transaction --set-gtid-purged=OFF --databases report  > reportdb.sql  (5.6)
2,在117,119从上:
change master to master_host='172.17.103.116', MASTER_PORT=3306, master_user='repl', master_password='repl1234',MASTER_AUTO_POSITION=1;
start slave;
在两台从机上设置read_only=1,relay_log_purge=0
mysql -e 'set global read_only=1’
mysql -e 'set global relay_log_purge=0'
四. 管理机manager上创建配置MHA文件
MHA的配置文件与mysql的my.cnf文件配置相似,采取的是分模块,param=value的方式来配置,配置文件位于管理节点,通常包括每一个mysql server的主机名,mysql用户名,密码,工作目录等等。本文列出了单套MHA以及采用全局配置来管理多套MHA配置文件的一些样例。
more /etc/masterha_default.cnf
[server default]
#MySQL的用户和密码
user=mha
password=mha1234
 
#系统ssh用户
ssh_user=root
 
#复制用户
repl_user=repl
repl_password= repl12
 
#监控
ping_interval=1
#shutdown_script=""
 
more /etc/masterha/app1.conf
[server default]
 
 
#mha manager工作目录
manager_workdir = /var/log/masterha/app1 //设置manager的工作目录,不存在Node会自动创建
manager_log = /var/log/masterha/app1/app1.log //设置manager的日志
remote_workdir = /var/log/masterha/app1 //设置远端mysql在发生切换时binlog的保存位置
 
[server1]
hostname=10.132.2.40
master_binlog_dir = /data/mysql/mysql3316/logs
candidate_master = 1
check_repl_delay = 0 #用防止master故障时,切换时slave有延迟,卡在那里切不过来。
port=3316
 
[server2]
hostname=10.132.2.41
master_binlog_dir=/data/mysql/mysql3316/logs
candidate_master=1 //设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库,即使这个主库不是集群中事件最新的slave
check_repl_delay=0 //默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
 
port=3316
 
#切换调用的脚本
master_ip_failover_script= /etc/masterha/master_ip_failover1 //master failover时执行,不配置vip时不用配
master_ip_online_change_script= /etc/masterha/master_ip_online_change1 //设置手动切换时候的切换脚本,masterswitchover时执行,不配置vip时不用配
 
 
[root@sjehjin-db03 bin]# more /etc/masterha/app2.conf
[server default]
 
 
#mha manager工作目录
manager_workdir = /var/log/masterha/app2
manager_log = /var/log/masterha/app2/app2.log
remote_workdir = /var/log/masterha/app2
 
[server1]
hostname=10.132.2.40
port=3326
master_binlog_dir = /data/mysql/mysql3326/logs
candidate_master = 1
check_repl_delay = 0 #用防止master故障时,切换时slave有延迟,卡在那里切不过来。
 
[server2]
hostname=10.132.2.41
port=3326
master_binlog_dir=/data/mysql/mysql3326/logs
candidate_master=1
check_repl_delay=0
 
#切换调用的脚本
master_ip_failover_script= /etc/masterha/master_ip_failover_2
master_ip_online_change_script= /etc/masterha/master_ip_online_change_2
4,
masterha_check_ssh --conf=/etc/masterha/app1.cnf
报错
Base class package "Log::Dispatch::Base" is empty.
    (Perhaps you need to 'use' the module which defines that package first,
    or make that module available 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/Log/Dispatch.pm line 10
BEGIN failed--compilation aborted at /usr/local/share/perl5/Log/Dispatch.pm line 10.
Compilation failed in require at /usr/local/share/perl5/MHA/SSHCheck.pm line 29.
BEGIN failed--compilation aborted at /usr/local/share/perl5/MHA/SSHCheck.pm line 29.
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.
 
执行
perl -MCPAN -e "install Log::Dispatch"  
后报错消失,估计是在安装依赖包的时候网络出了问题,少装了这个包
 
五,masterha_check_ssh工具验证ssh信任登录是否成功
先关闭selinux,   关闭firewall
 masterha_check_ssh --conf=/etc/app1.cnf 
Thu Mar 17 17:21:06 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Thu Mar 17 17:21:06 2016 - [info] Reading application default configurations from /etc/app1.cnf.. Thu Mar 17 17:21:06 2016 - [info] Reading server configurations from /etc/app1.cnf..
问题:
报错:Can't locate MHA/SSHCheck.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/bin/masterha_check_ssh line 25.
BEGIN failed--compilation aborted at /usr/bin/masterha_check_ssh line 25.
解决方法:
ln -s /usr/lib/perl5/vendor_perl/MHA /usr/lib64/perl5/vendor_perl/MHA
 
六: masterha_check_repl工具验证mysql复制是否成功
 masterha_check_repl --conf=/etc/app1.cnf
报错1:
Can't locate MHA/BinlogManager.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/bin/apply_diff_relay_logs line 24. BEGIN failed--compilation aborted at /usr/bin/apply_diff_relay_logs line 24.解决
解决方法:
主从都得安装;
ln -s /usr/lib/perl5/vendor_perl/MHA /usr/lib64/perl5/vendor_perl/
报错2:
Failed to save binary log: Binlog not found from /var/lib/mysql! If you got this error at MHA Manager, please set "master_binlog_dir=/path/to/binlog_directory_of_the_master" correctly in the MHA Manager's configuration file and try again.
 
方法:
在more /etc/app1.cnf中加master_binlog_dir目录;
[server default] # mysql user and password user=root password=root1234 ssh_user=root repl_user=repl repl_password=repl1234 ping_interval=1 master_binlog_dir=/home/mysql/data/ ##必须为master的binlog日志目录
 
报错:
Can't exec "mysqlbinlog": No such file or directory at /usr/lib64/perl5/vendor_perl/MHA/BinlogManager.pm line 99. mysqlbinlog version not found!
解决方法:
ln -s /data/mysql3306/bin/mysqlbinlog /usr/bin/mysqlbinlog
报错:
Testing mysql connection and privileges..sh: mysql: command not found mysql command failed with rc 127:0!
解决方法:
ln -s /data/mysql3306/bin/mysql /usr/bin/mysql
七:
Sat Mar 19 17:20:06 2016 - [warning] master_ip_failover_script is not defined. Sat Mar 19 17:20:06 2016 - [warning] shutdown_script is not defined.
master_ip_failover见最后
八:启动MHA manager,并监控日志文件
需要手动在master服务器上绑定一个vip
/sbin/ifconfig bond0:1 192.168.9.245/24
删除网卡的第二个IP地址: 
ip addr del 192.168.9.245/24 dev bond0:1
 
启动命令:
nohup masterha_manager --conf=/etc/app1.cnf &
nohup masterha_manager --conf=/etc/app1.cnf --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &
九,检查状态:
masterha_check_status --conf=/etc/app1.cnf
十,终止manager
masterha_stop --conf=/etc/app1.cnf
十一,为了防止脑裂发生,推荐生产环境采用脚本的方式来管理虚拟ip,而不是使用keepalived来完成。到此为止,基本MHA集群已经配置完毕。接下来就是实际的测试环节了。通过一些测试来看一下MHA到底是如何进行工作的
而且如果使用脚本管理vip的话,需要手动在master服务器上绑定一个vip
 
十二 配置VIP的方式
A. 通过全局配置文件实现 
vim /etc/mha/masterha_default.cnf
 
[server default] user=root password=geekwolf ssh_user=root repl_user=rep repl_password=geekwolf ping_interval=1 secondary_check_script = masterha_secondary_check -s node1 -s node2 -s node3 --user=root --master_host=node1 --master_ip=192.168.10.216 --master_port=3306 master_ip_failover_script="/etc/mha/scripts/master_ip_failover" master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change" #shutdown_script= /script/masterha/power_manager #report_script=""
 
修改后的master_ip_failover、master_ip_online_change脚本
 
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.0.88/24'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig eth1:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth1:$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 "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"; }
 
B. 通过第三方HA(keepalived、heartbeat)实现VIP,以keepalived为例 
以node1 node2互为主备进行配置keepalived 
在node1 node2上分别下载安装keepalived 
yum -y install popt-* 
./configure ―prefix=/usr/local/keepalived ―enable-snmp 
make && make install 
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ 
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/rc.d/init.d/keepalived 
chkconfig keepalived on 
mkdir /etc/keepalived 
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin
 
修改node1(192.168.10.216)配置文件 
vim /etc/keepalived/keepalived.conf
 
! Configuration File for keepalived global_defs { router_id MHA notification_email { root@localhost #接收邮件,可以有多个,一行一个 } #当主、备份设备发生改变时,通过邮件通知 notification_email_from m@localhost #发送邮箱服务器 smtp_server 127.0.0.1 #发送邮箱超时时间 smtp_connect_timeout 30 } varrp_script check_mysql { script "/etc/keepalived/check_mysql.sh" } vrrp_sync_group VG1 { group { VI_1 } notify_master "/etc/keepalived/master.sh" } vrrp_instance VI_1 { state master interface eth0 virtual_router_id 110 priority 100 advert_int 1 nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来 authentication { # 认证方式,可以是PASS或AH两种认证方式 auth_type PASS # 认证密码 auth_pass geekwolf } track_script { check_mysql } virtual_ipaddress { 192.168.10.219 } }
 
修改node2(192.168.10.217)配置文件 
vim /etc/keepalived/keepalived.conf
 
! Configuration File for keepalived global_defs { router_id MHA notification_email { root@localhost #接收邮件,可以有多个,一行一个 } #当主、备份设备发生改变时,通过邮件通知 notification_email_from m@localhost #发送邮箱服务器 smtp_server 127.0.0.1 #发送邮箱超时时间 smtp_connect_timeout 30 } varrp_script check_mysql { script "/etc/keepalived/check_mysql.sh" } vrrp_sync_group VG1 { group { VI_1 } notify_master "/etc/keepalived/master.sh" } vrrp_instance VI_1 { state backup interface eth0 virtual_router_id 110 priority 99 advert_int 1 authentication { # 认证方式,可以是PASS或AH两种认证方式 auth_type PASS # 认证密码 auth_pass geekwolf } track_script { check_mysql } virtual_ipaddress { 192.168.10.219 } }
 
check_mysql.sh
 
#!/bin/bash MYSQL=/usr/local/mysql/bin/mysql MYSQL_HOST=127.0.0.1 MYSQL_USER=root MYSQL_PASSWORD=geekwolf CHECK_TIME=3 #mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0 MYSQL_OK=1 function check_mysql_helth (){ $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -e "show status;" >/dev/null 2>&1 if [ $? = 0 ] ;then MYSQL_OK=1 else MYSQL_OK=0 fi return $MYSQL_OK } while [ $CHECK_TIME -ne 0 ] do let "CHECK_TIME -= 1" check_mysql_helth if [ $MYSQL_OK = 1 ] ; then CHECK_TIME=0 exit 0 fi if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ] then pkill keepalived exit 1 fi sleep 1 done
 
master.sh
#!/bin/bash VIP=192.168.10.219 GATEWAY=1.1 /sbin/arping -I eth0 -c 5 -s $VIP $GATEWAY &>/dev/null
 
 
vip切换后新主机上需要作 新主机广播IP
arping -I bond0 -s 192.168.9.85 -b -c 1 192.168.9.85
 
chmod +x /etc/keepalived/check_mysql.sh 
chmod +x /etc/keepalived/master.sh
 
五.MHA常用命令
 
查看manager状态 
masterha_check_status ―conf=/etc/mha/app1/app1.cnf
 
查看免密钥是否正常 
masterha_check_ssh ―conf=/etc/mha/app1/app1.cnf
 
查看主从复制是否正常 
masterha_check_repl ―conf=/etc/mha/app1/app1.cnf
 
添加新节点server4到配置文件 
masterha_conf_host ―command=add ―conf=/etc/mha/app1/app1.cnf ―hostname=geekwolf ―block=server4 ―params=“no_master=1;ignore_fail=1” ***server4节点 
masterha_conf_host ―command=delete ―conf=/etc/mha/app1/app1.cnf ―block=server4
 
注:  
block:为节点区名,默认值 为[server_$hostname],如果设置成block=100,则为[server100] params:参数,分号隔开(参考 https://code.google.com/p/mysql-master-ha/wiki/Parameters )
 
关闭manager服务 
masterha_stop ―conf=/etc/mha/app1/app1.cnf
 
主手动切换(前提不要启动masterha_manager服务) 
在主node1存活情况下进行切换 
交互模式: 
masterha_master_switch ―master_state=alive ―conf=/etc/mha/app1/app1.cnf ―new_master_host=node2 
非交互模式: 
masterha_master_switch ―master_state=alive ―conf=/etc/mha/app1/app1.cnf ―new_master_host=node2 ―interactive=0 
在主node1宕掉情况下进行切换 
masterha_master_switch ―master_state=dead ―conf=/etc/mha/app1/app1.cnf ―dead_master_host=node1 ―dead_master_ip=192.168.10.216 ―dead_master_port=3306 ―new_master_host=192.168.10.217 详细请参考: https://code.google.com/p/mysql-master-ha/wiki/TableOfContents?tm=6  *
 
六.注意事项  
 
A.  以上两种vip切换方式,建议采用第一种方法 
B.  发生主备切换后,manager服务会自动停掉,且在/var/log/mha/app1下面生成
app1.failover.complete,若再次发生切换需要***app1.failover.complete文件
C.  测试过程发现一主两从的架构(两从都设置可以担任主角色candidate_master=1),当旧主故障迁移到备主 后,***app1.failover.complete,再次启动manager,停掉新主后,发现无法正常切换(解决方式:***/etc/mha /app1/app1.cnf里面的旧主node1的信息后,重新切换正常) 
D.  arp缓存导致切换VIP后,无法使用问题 
E.  使用Semi-Sync能够最大程度保证数据安全
F.  Purge_relay_logs脚本***中继日志不会阻塞SQL线程,在每台从节点上设置计划任务定期清除中继日志
0 5 * * * root /usr/bin/purge_relay_logs ―user=root ―password=geekwolf ―disable_relay_log_purge >> /var/log/mha/purge_relay_logs.log 2>&1
 
 
十二:自动failover模拟测试的操作步骤如下:
1,yum install sysbench -y
2,在主库(172.17.103.116)上进行sysbench数据生成,在sbtest库下生成sbtest表,共100W记录。
测试:
sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=1800 --mysql-user=root --mysql-password=root1234 --mysql-host=172.17.103.116 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex prepare sysbench: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
这说明sysbench无法找到mysql的库文件,这很可能是环境变量LD_LIBRARY_PATH没有设置,设置后即可解决该问题: 
解决方法:
export LD_LIBRARY_PATH=/data/mysql3306/lib
 
sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=1800 --mysql-user=root --mysql-password=root1234 --mysql-host=172.17.103.116 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex prepare
3,停掉slave sql线程,模拟主从延时。(172.17.103.117)
mysql > stop slave io_thread;
4,模拟sysbench压力测试。
主库(172.17.103.116)持续时间为3分钟,产生大量的binlog。
sysbench --test=oltp --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=180 --mysql-user=root --mysql-password=root1234 --mysql-host=172.17.103.116 --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex run
 

posted @ 2019-03-07 20:22  瓶子倒了水未必流出来  阅读(819)  评论(0编辑  收藏  举报