描述:MHA(Master High Availability),目前mysql中比较常用的解决方案。
工具:
masterha_check_ssh 检查MHA的SSH配置状况masterha_check_repl 检查MySQL复制状况masterha_manger 启动MHAmasterha_check_status 检测当前MHA运行状态masterha_master_monitor 检测master是否宕机masterha_master_switch 控制故障转移(自动或者手动)masterha_conf_host 添加或删除配置的server信息
环境配置:
| IP | 作用 | 描述 mysql5.7版本 |
| 192.168.68.129 | master (主) | node_master节点 |
| 192.168.68.130 | slave(从+备用master ) | node_bakup_master备用 |
| 192.168.68.131 | slave (从) | 节点 |
| 192.168.68.132 | slave+manager(从+管理) | 节点+管理机 |
| 192.168.68.110 | 共享IP,无需机器 |
一、一主三从的配置:(忽略过程)
注意:
#1、my.cnf 中要禁用gtid模式 ,5.7版本默认禁用gtid模式,如果非5.7版本需要在[mysqld]中增加gtid_mode=0
#2、4台mysql中的server_id 不能相同- servicer_id=1、2、3、4
- #3、 auto.cnf 中的 server-uuid 4台mysql不能相同
- [root@slave tmp]# cd /usr/local/mysql/data/ [root@slave data]# cat auto.cnf [auto] server-uuid=8c1ea0b7-5be8-11e7-93a7-000c292ef002 [root@slave data]#
二、增加mha需要复制时的用户名及密码:
## all_db 都需要执行:grant all privileges on *.* to 'mha_manager'@'%' identified by '123456';grant replication slave on *.* to 'repl'@'%' identified by '123456';flush privileges;## 如果有一台slave上面运行manager,在此电脑上面执行: 此次是 192.168.68.132 使用managerdb 代表:GRANT ALL PRIVILEGES ON *.* TO 'mha_manager'@'manager.100' IDENTIFIED BY '123456'(manager.100)是本机名称,uname -a 可以查看到
三、ssh-keygen 建立免密信任
## 192.168.68.129ssh-keygen ##猛砸回车cd ~/.ssh/ls | grep id_rsa显示如下:
## 192.168.68.129 复制id_rsa.pub 密钥,并后成key+ip的文件cp id_rsa.pub key129## 将生成的key+ip文件分发到各机器上面scp key129 192.168.68.130:~/.ssh/scp key129 192.168.68.131:~/.ssh/scp key129 192.168.68.132:~/.ssh/## 130、131、132 重复上述工作最后的结果如下:
## 129、130、131、132 重复如下操作cat key129 key130 key131 key132 > authorized_keys## 完后成,在每一台机器上面访问其它三台机器,ssh 192.168.68.xx## 免密登录 算是正确
四、下载 rpm 包:并复制到all_db 中
mha4mysql-node-0.56-0.el6.noarch.rpmmha4mysql-manager-0.56-0.el6.noarch.rpm##如何下载请自行百度
五、安装 mha4mysql-node-0.56-0.el6.noarch.rpm
包在所有的机器上面 all_db
六、manager:即192.168.68.132上面操作内容如下: 安装 mha4mysql-manager-0.56-0.el6.noarch.rpm
包
yum install perl cpanyum install perl-Config-Tinyyum install perl-Time-HiResyum install -y rrdtool perl-rrdtool rrdtool-devel perl-Params-Validate## 无法使用yum 源的请自行百度,非常简单
wget http://downloads.naulinux.ru/pub/NauLinux/6x/i386/sites/School/RPMS/perl-Log-Dispatch-2.27-1.el6.noarch.rpmwget http://dl.Fedoraproject.org/pub/epel/6/i386/perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpmwget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sender-0.8.16-3.el6.noarch.rpmwget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
rpm -ivh perl-Mail-Sender-0.8.16-1.el6.rf.noarch.rpmrpm -ivh perl-Mail-Sendmail-0.79-1.2.el6.rf.noarch.rpmyum localinstall perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpmyum localinstall perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
rpm -ivh mha4mysql-manager-0.53-0.el6.noarch.rpm
七、建立mha需要的目录及文件 manager_db中操作:
## manager-db 中操作shell> mkdir /etc/masterha --建立文件夹shell> vim /etc/masterha/app1.cnf -- 新建配置文件部分
#### app1.cnf 配置如下 manager-db 中操作[server default]manager_log=/var/log/mha/app1/manager.logmanager_workdir=/var/log/mha/app1master_binlog_dir=/usr/local/mysql/dataremote_workdir=/var/log/mha/app1user=mha_managerpassword=123456repl_user=replrepl_password=123456ssh_user=rootping_interval=1secondary_check_script="masterha_secondary_check -s 192.168.68.129 -s 192.168.68.130"master_ip_failover_script="/tmp/master_ip_failover.sh"[server1]hostname=192.168.68.129#可以升为mastercandidate_master=1[server2]hostname=192.168.68.130candidate_master=1check_repl_delay=0[server3]hostname=192.168.68.131no_master=1[server4]hostname=192.168.68.132no_master=1
### 所有的DB 上面建立软件 连接 注意all-db 所有的机器都需要操作ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysqlln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlogln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlogln -s /usr/local/mysql/bin/mysql /usr/bin
### app1.cnf 中要使用的脚本 /tmp/master_ip_failover.sh 内容如下 只需要在manager中操作如下### 脚本要顶格上,以上两行,请自行删除。 其中的IP为共享IP#!/usr/bin/env perluse 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.68.110/24'; # Virtual IPmy $key = "1";my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";my $exit_code = 0;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" ) {# $orig_master_host, $orig_master_ip, $orig_master_port are passed.# If you manage master ip address at global catalog database,# invalidate orig_master_ip here.my $exit_code = 1;eval {print "\n\n\n***************************************************************\n";print "Disabling the VIP - $vip on old master: $orig_master_host\n";print "***************************************************************\n\n\n\n";&stop_vip();$exit_code = 0;};if ($@) {warn "Got Error: $@\n";exit $exit_code;}exit $exit_code;}elsif ( $command eq "start" ) {# all arguments are passed.# If you manage master ip address at global catalog database,# activate new_master_ip here.# You can also grant write access (create user, set read_only=0, etc) here.my $exit_code = 10;eval {print "\n\n\n***************************************************************\n";print "Enabling the VIP - $vip on new master: $new_master_host \n";print "***************************************************************\n\n\n\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";`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;exit 0;}else {&usage();exit 1;}}# A simple system call that enable the VIP on the new mastersub start_vip() {`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;}# A simple system call that disable the VIP on the old_mastersub stop_vip() {`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;}sub usage {"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";}
### 需要给/tmp 的格式 和master_ip_failover.sh 执行权限 manager db中操作cd /tmpchmod 777 /tmpchmod 777 master_ip_failover.shchown +x master_ip_failover.sh
八、测试是否成功 manager_db中操作
## manager_db 有如下图显示才算成功 测试ssh 是否Okmasterha_check_ssh --conf=/etc/masterha/app1.cnf
## manager_db 有如下图显示才算成功 测试复制是否Okmasterha_check_repl --conf=/etc/masterha/app1.cnf
九、准备开启mha
## master _db 上面执行 ,请注意是master_db 而非manager_dbmaster_db:ifconfig eth0:0 192.168.68.110 netmask 255.255.255.0 up
## 请注意是manager_db 上面执行masterha_manager --conf=/etc/masterha/app1.cnf### 回车后,会卡住如下图,不要管
### 在manager_db 中,另起一个shell 查看mha状态masterha_check_status --conf=/etc/masterha/app1.cnf### 如果启动成功,如下提示
十、测试mha是否自动切换
### master_db上面执行 192.168.68.129ip addr show### 观察是否是双IP 192.168.68.129 与192.168.68.110 (110为共享IP)service mysqld stop##再次观察IP , 只有一个 192.168.68.129ip addr show### 再去备用-master 192.168.68.130 观察,是否成双IP## 192.168.68.130 与192.168.68.110ip addr showmysql -uroot -p123456show processlist;## 我们会看到有两台slave(131,132)及 130升级为 master## 如切换成功,mha程序自动停止### 如果想再次启动mha ,需要的操作如下:### 现在的状态为 129 ,stop ,130 master ,131 slave ,132 slave+manager# 1、 130_dbmysql -uroot -p123456show master status;### 记录日志文件及日志位置#2、 129_dbservice mysqld startmysql -uroot -p123456stop slave ;change master to master_host='192.168.68.129',master_user='repl', master_password='123456', master_log_file='mysql-bin.00000xxx',master_log_pos=xxx;start slave;# 3、 132_db manager 操作rm -rf /var/log/mha/app1/app1.master_status.healthmasterha_check_status --conf=/etc/masterha/app1.cnf#### 这次启动不用修改 app1.cnf中的任何内容
9728

被折叠的 条评论
为什么被折叠?



