当前位置: 澳门新濠3559 > 数据库 > 正文

以下是CentOS6.8下MySQL MHA架构搭建笔记,它自动将

时间:2019-11-09 00:17来源:数据库
1.关闭Selinux [root@localhost ~]# vi /etc/selinux/config # This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints

1.关闭Selinux

[root@localhost ~]# vi /etc/selinux/config

图片 1图片 2

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

View Code

[root@localhost ~]# reboot

关于MHA的普及操作,包涵活动Failover,手动Failover,在线切换,可参照他事他说加以考察另风流倜傥篇博客:

三、开端安装配置

     # vim /etc/rsyslog.conf

5.master备份slave还原(略),查找master Binlog位置

图片 3图片 4

[root@localhost tmp]# mysql -uroot -p
mysql> show master statusG;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

View Code

      # nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1 &

3.设置半合办理并答复制插件

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

    # ssh-keygen

4.改过my.cnf配置文件

[root@localhost tmp]# vi /etc/my.cnf

图片 5图片 6

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id=139
log_bin=/var/lib/mysql/mysql-bin
binlog-format=ROW
binlog-checksum=CRC32
log_slave_updates=true
gtid-mode=on
enforce-gtid-consistency=true
master_info_repository=TABLE
sync-master-info=1
slave-parallel-workers=2
master-verify-checksum=1
slave-sql-verify-checksum=1
relay_log=/var/lib/mysql/localhost-relay-bin
relay_log_purge=0
relay_log_recovery=1
rpl_semi_sync_master_enabled=ON
rpl_semi_sync_slave_enabled=ON

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

View Code

[server default]
manager_log=/masterha/app1/manager.log          //设置manager的日志
manager_workdir=/masterha/app1           //设置manager的工作目录
master_binlog_dir=/var/lib/mysql                  //设置master默认保存binlog的位置,以便MHA可以找到master的日志
master_ip_failover_script= /usr/local/bin/master_ip_failover    //设置自动failover时候的切换脚本
master_ip_online_change_script= /usr/local/bin/master_ip_online_change  //设置手动切换时候的切换脚本
user=monitor               // 设置监控用户
password=monitor123         //设置监控用户的密码
ping_interval=1         //设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候进行自动failover
remote_workdir=/tmp     //设置远端mysql在发生切换时binlog的保存位置
repl_user=repl          //设置复制环境中的复制用户名
repl_password=repl    //设置复制用户的密码
report_script=/usr/local/bin/send_report    //设置发生切换后发送的报警的脚本
secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.244.20 -s 192.168.244.30 --user=root --master_host=192.168.244.10 --master_ip=192.168.244.10 --master_port=3306  //一旦MHA到master的监控之间出现问题,MHA Manager将会判断其它两个slave是否能建立到master_ip 3306端口的连接
shutdown_script=""      //设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机防止发生脑裂)
ssh_user=root           //设置ssh的登录用户名

[server1]
hostname=192.168.244.10
port=3306

[server2]
hostname=192.168.244.20
port=3306
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

[server3]
hostname=192.168.244.30
port=3306

转发请评释出处,本文地址: 

        MHA在切换的过程中会直接调用mysqlbinlog命令,故须要在情形变量中钦点mysqlbinlog的切实路径。

6.验证安装内容

以下是CentOS6.8下MySQL MHA架构搭建笔记,它自动将最新数据的slave提升为master。验证ssh信任

[root@localhost mha4mysql-manager-0.56]# masterha_check_ssh --conf=/etc/mha/app1/app1.cnf

图片 7图片 8

[root@localhost mha4mysql-manager-0.56]# masterha_check_ssh --conf=/etc/mha/app1/app1.cnf
Thu Mar 16 03:53:28 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 03:53:28 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 03:53:28 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 03:53:28 2017 - [info] Starting SSH connection tests..
Thu Mar 16 03:53:33 2017 - [debug] 
Thu Mar 16 03:53:28 2017 - [debug]  Connecting via SSH from root@192.168.206.140(192.168.206.140:22) to root@192.168.206.139(192.168.206.139:22)..
Thu Mar 16 03:53:32 2017 - [debug]   ok.
Thu Mar 16 03:53:32 2017 - [debug]  Connecting via SSH from root@192.168.206.140(192.168.206.140:22) to root@192.168.206.141(192.168.206.141:22)..
Thu Mar 16 03:53:33 2017 - [debug]   ok.
Thu Mar 16 03:53:33 2017 - [debug] 
Thu Mar 16 03:53:28 2017 - [debug]  Connecting via SSH from root@192.168.206.139(192.168.206.139:22) to root@192.168.206.140(192.168.206.140:22)..
Thu Mar 16 03:53:32 2017 - [debug]   ok.
Thu Mar 16 03:53:32 2017 - [debug]  Connecting via SSH from root@192.168.206.139(192.168.206.139:22) to root@192.168.206.141(192.168.206.141:22)..
Thu Mar 16 03:53:33 2017 - [debug]   ok.
Thu Mar 16 03:53:33 2017 - [debug] 
Thu Mar 16 03:53:29 2017 - [debug]  Connecting via SSH from root@192.168.206.141(192.168.206.141:22) to root@192.168.206.139(192.168.206.139:22)..
Thu Mar 16 03:53:33 2017 - [debug]   ok.
Thu Mar 16 03:53:33 2017 - [debug]  Connecting via SSH from root@192.168.206.141(192.168.206.141:22) to root@192.168.206.140(192.168.206.140:22)..
Thu Mar 16 03:53:33 2017 - [debug]   ok.
Thu Mar 16 03:53:33 2017 - [info] All SSH connection tests passed successfully.

View Code

验证主从复制

[root@localhost mha4mysql-manager-0.56]# masterha_check_repl --conf=/etc/mha/app1/app1.cnf

图片 9图片 10

[root@localhost mha4mysql-manager-0.56]# masterha_check_repl --conf=/etc/mha/app1/app1.cnf
Thu Mar 16 03:56:02 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 03:56:02 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 03:56:02 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 03:56:02 2017 - [info] MHA::MasterMonitor version 0.56.
Thu Mar 16 03:56:03 2017 - [info] GTID failover mode = 1
Thu Mar 16 03:56:03 2017 - [info] Dead Servers:
Thu Mar 16 03:56:03 2017 - [info] Alive Servers:
Thu Mar 16 03:56:03 2017 - [info]   192.168.206.139(192.168.206.139:3306)
Thu Mar 16 03:56:03 2017 - [info]   192.168.206.140(192.168.206.140:3306)
Thu Mar 16 03:56:03 2017 - [info]   192.168.206.141(192.168.206.141:3306)
Thu Mar 16 03:56:03 2017 - [info] Alive Slaves:
Thu Mar 16 03:56:03 2017 - [info]   192.168.206.140(192.168.206.140:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 03:56:03 2017 - [info]     GTID ON
Thu Mar 16 03:56:03 2017 - [info]     Replicating from 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 03:56:03 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 03:56:03 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 03:56:03 2017 - [info]     GTID ON
Thu Mar 16 03:56:03 2017 - [info]     Replicating from 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 03:56:03 2017 - [info]     Not candidate for the new Master (no_master is set)
Thu Mar 16 03:56:03 2017 - [info] Current Alive Master: 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 03:56:03 2017 - [info] Checking slave configurations..
Thu Mar 16 03:56:03 2017 - [info]  read_only=1 is not set on slave 192.168.206.140(192.168.206.140:3306).
Thu Mar 16 03:56:03 2017 - [info]  read_only=1 is not set on slave 192.168.206.141(192.168.206.141:3306).
Thu Mar 16 03:56:03 2017 - [info] Checking replication filtering settings..
Thu Mar 16 03:56:03 2017 - [info]  binlog_do_db= , binlog_ignore_db= 
Thu Mar 16 03:56:03 2017 - [info]  Replication filtering check ok.
Thu Mar 16 03:56:03 2017 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Thu Mar 16 03:56:03 2017 - [info] Checking SSH publickey authentication settings on the current master..
Thu Mar 16 03:56:08 2017 - [warning] HealthCheck: Got timeout on checking SSH connection to 192.168.206.139! at /usr/share/perl5/vendor_perl/MHA/HealthCheck.pm line 342.
Thu Mar 16 03:56:08 2017 - [info] 
192.168.206.139(192.168.206.139:3306) (current master)
 +--192.168.206.140(192.168.206.140:3306)
 +--192.168.206.141(192.168.206.141:3306)

Thu Mar 16 03:56:08 2017 - [info] Checking replication health on 192.168.206.140..
Thu Mar 16 03:56:08 2017 - [info]  ok.
Thu Mar 16 03:56:08 2017 - [info] Checking replication health on 192.168.206.141..
Thu Mar 16 03:56:08 2017 - [info]  ok.
Thu Mar 16 03:56:08 2017 - [info] Checking master_ip_failover_script status:
Thu Mar 16 03:56:08 2017 - [info]   /etc/mha/scripts/master_ip_failover --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=status --ssh_user=root --orig_master_host=192.168.206.139 --orig_master_ip=192.168.206.139 --orig_master_port=3306 
Thu Mar 16 03:56:08 2017 - [info]  OK.
Thu Mar 16 03:56:08 2017 - [warning] shutdown_script is not defined.
Thu Mar 16 03:56:08 2017 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

View Code

      在ext3文件系统下,删除大的文本供给一定的时刻,那样会形成惨痛的复制延迟,所以在Linux中,日常都以经过硬链接的点子来删除大文件。

3.安装epel yum源

[root@localhost tmp]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@localhost tmp]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

图片 11图片 12

4.在manager节点管理MHA配置文件

眼下采纳yum安装的,未有samples文件夹,所以其余down了黄金年代份mha4mysql-manager-0.56.tar.gz包(具体经过略卡塔 尔(阿拉伯语:قطر‎

[root@localhost tmp]# tar xf mha4mysql-manager-0.56.tar.gz
[root@localhost tmp]# cd mha4mysql-manager-0.56
[root@localhost mha4mysql-manager-0.56]# mkdir -p /etc/mha/{app1,scripts}
[root@localhost mha4mysql-manager-0.56]# cp -r samples/conf/* /etc/mha/
[root@localhost mha4mysql-manager-0.56]# cp -r samples/scripts/* /etc/mha/scripts/
[root@localhost mha4mysql-manager-0.56]# mv /etc/mha/app1.cnf /etc/mha/app1/
[root@localhost mha4mysql-manager-0.56]# mv /etc/mha/masterha_default.cnf /etc/masterha_default.cnf

      3. 安装准时清理relay脚本

6.各slave上进行脚本,建设构造基本配置。(MySQL5.7得以一向动用master_auto_position=1参数)

mysql> change master to master_host='192.168.206.139',master_user='u_repl',master_password='replpass',master_log_file='mysql-bin.000001' ,master_log_pos=154;
mysql> start slave;

    至此,MHA node节点安装收尾,会在/usr/local/bin下生成以下脚本文件

2.富有节点(满含manager)下载安装mha4mysql-node-0.56

是因为google被墙,作者是间接从互连网下载的rpm文件,具体下载进程略。

[root@localhost tmp]# yum localinstall mha4mysql-node-0.56-0.el6.noarch.rpm

图片 13图片 14

[root@localhost tmp]# yum localinstall mha4mysql-node-0.56-0.el6.noarch.rpm
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Local Package Process
Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch
Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: ftp.sjtu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================
 Package                                     Arch                                Version                                 Repository                                                      Size
==============================================================================================================================================================================================
Installing:
 mha4mysql-node                              noarch                              0.56-0.el6                              /mha4mysql-node-0.56-0.el6.noarch                              102 k

Transaction Summary
==============================================================================================================================================================================================
Install       1 Package(s)

Total size: 102 k
Installed size: 102 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mha4mysql-node-0.56-0.el6.noarch                                                                                                                                           1/1 
  Verifying  : mha4mysql-node-0.56-0.el6.noarch                                                                                                                                           1/1 

Installed:
  mha4mysql-node.noarch 0:0.56-0.el6                                                                                                                                                          

Complete!

View Code

确认安装内容

[root@localhost tmp]# rpm -ql mha4mysql-node

图片 15图片 16

[root@localhost tmp]# rpm -ql mha4mysql-node
/usr/bin/apply_diff_relay_logs
/usr/bin/filter_mysqlbinlog
/usr/bin/purge_relay_logs
/usr/bin/save_binary_logs
/usr/share/man/man1/apply_diff_relay_logs.1.gz
/usr/share/man/man1/filter_mysqlbinlog.1.gz
/usr/share/man/man1/purge_relay_logs.1.gz
/usr/share/man/man1/save_binary_logs.1.gz
/usr/share/perl5/vendor_perl/MHA/BinlogHeaderParser.pm
/usr/share/perl5/vendor_perl/MHA/BinlogManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFindManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinder.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderElp.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderXid.pm
/usr/share/perl5/vendor_perl/MHA/NodeConst.pm
/usr/share/perl5/vendor_perl/MHA/NodeUtil.pm
/usr/share/perl5/vendor_perl/MHA/SlaveUtil.pm

View Code

     查看绑定情状

2.起家MHA管理用客商

mysql> GRANT ALL PRIVILEGES ON *.* to 'root'@'192.168.206.%' IDENTIFIED BY 'checkpass';
  1. master_ip_failover:管理VIP

  2. master_ip_online_change:

  3. masterha_secondary_check:当MHA manager检查测量试验到master不可用时,通过masterha_secondary_check脚本来进一层料定,减低误切的高风险。

  4. send_report:当发生故障切换时,可通过send_report脚本发送告急音信。

IP财富布置:

192.168.206.139 master

192.168.206.140 slave01(备用master)

192.168.206.141 slave02

192.168.206.142 manager

192.168.206.145 VIP

角色                             IP地址                 ServerID      类型

四、运营MHA、测量试验MHA故障转移

启动MHA

[root@localhost mha4mysql-manager-0.56]# masterha_manager --conf=/etc/mha/app1/app1.cnf

图片 17图片 18

[root@localhost mha4mysql-manager-0.56]# masterha_manager --conf=/etc/mha/app1/app1.cnf
Thu Mar 16 03:59:39 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 03:59:39 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 03:59:39 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..

View Code

抑或应用后台运转

[root@localhost mha4mysql-manager-0.56]# nohup masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &

图片 19图片 20

[root@localhost mha4mysql-manager-0.56]# nohup masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
[3] 5977
[2]   Exit 1                  nohup masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1

View Code

配置master的VIP

[root@localhost mysql]# /sbin/ifconfig eth0:234 192.168.206.145 netmask 255.255.255.0 up

确认VIP

[root@localhost mysql]# ifconfig

图片 21图片 22

[root@localhost mysql]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:81:A4:6E  
          inet addr:192.168.206.139  Bcast:192.168.206.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe81:a46e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23074 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11235 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1628296 (1.5 MiB)  TX bytes:1043306 (1018.8 KiB)

eth0:234  Link encap:Ethernet  HWaddr 00:0C:29:81:A4:6E  
          inet addr:192.168.206.145  Bcast:192.168.206.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

View Code

确认MHA运维状态:

[root@localhost ~]# masterha_check_status --conf=/etc/mha/app1/app1.cnf

图片 23图片 24

[root@localhost ~]# masterha_check_status --conf=/etc/mha/app1/app1.cnf
app1 (pid:5140) is running(0:PING_OK), master:192.168.206.139

View Code

进程确认:

[root@localhost scripts]# ps aux|grep mha

图片 25图片 26

[root@localhost scripts]# ps aux|grep mha
root       6731  1.8  0.9 194240 18724 pts/1    S    10:16   0:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover
root       6750  0.0  0.0 103316   844 pts/1    S+   10:16   0:00 grep mha

View Code

关闭MHA Manager监控:

[root@localhost mha4mysql-manager-0.56]# masterha_stop --conf=/etc/mha/app1/app1.cnf

图片 27图片 28

[root@localhost mha4mysql-manager-0.56]# masterha_stop --conf=/etc/mha/app1/app1.cnf
Stopped app1 successfully.
[3]+  Exit 1                  nohup masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1

View Code

当仁不让切换之互相形式

[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.140

图片 29图片 30

[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.140
Thu Mar 16 07:57:17 2017 - [info] MHA::MasterRotate version 0.56.
Thu Mar 16 07:57:17 2017 - [info] Starting online master switch..
Thu Mar 16 07:57:17 2017 - [info] 
Thu Mar 16 07:57:17 2017 - [info] * Phase 1: Configuration Check Phase..
Thu Mar 16 07:57:17 2017 - [info] 
Thu Mar 16 07:57:17 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 07:57:17 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 07:57:17 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 07:57:17 2017 - [info] GTID failover mode = 1
Thu Mar 16 07:57:17 2017 - [info] Current Alive Master: 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 07:57:17 2017 - [info] Alive Slaves:
Thu Mar 16 07:57:17 2017 - [info]   192.168.206.140(192.168.206.140:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 07:57:17 2017 - [info]     GTID ON
Thu Mar 16 07:57:17 2017 - [info]     Replicating from 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 07:57:17 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 07:57:17 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 07:57:17 2017 - [info]     GTID ON
Thu Mar 16 07:57:17 2017 - [info]     Replicating from 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 07:57:17 2017 - [info]     Not candidate for the new Master (no_master is set)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 192.168.206.139(192.168.206.139:3306)? (YES/no): yes
Thu Mar 16 07:57:19 2017 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Thu Mar 16 07:57:19 2017 - [info]  ok.
Thu Mar 16 07:57:19 2017 - [info] Checking MHA is not monitoring or doing failover..
Thu Mar 16 07:57:19 2017 - [info] Checking replication health on 192.168.206.140..
Thu Mar 16 07:57:19 2017 - [info]  ok.
Thu Mar 16 07:57:19 2017 - [info] Checking replication health on 192.168.206.141..
Thu Mar 16 07:57:19 2017 - [info]  ok.
Thu Mar 16 07:57:19 2017 - [info] 192.168.206.140 can be new master.
Thu Mar 16 07:57:19 2017 - [info] 
From:
192.168.206.139(192.168.206.139:3306) (current master)
 +--192.168.206.140(192.168.206.140:3306)
 +--192.168.206.141(192.168.206.141:3306)

To:
192.168.206.140(192.168.206.140:3306) (new master)
 +--192.168.206.141(192.168.206.141:3306)

Starting master switch from 192.168.206.139(192.168.206.139:3306) to 192.168.206.140(192.168.206.140:3306)? (yes/NO): yes
Thu Mar 16 07:57:21 2017 - [info] Checking whether 192.168.206.140(192.168.206.140:3306) is ok for the new master..
Thu Mar 16 07:57:21 2017 - [info]  ok.
Thu Mar 16 07:57:21 2017 - [info] ** Phase 1: Configuration Check Phase completed.
Thu Mar 16 07:57:21 2017 - [info] 
Thu Mar 16 07:57:21 2017 - [info] * Phase 2: Rejecting updates Phase..
Thu Mar 16 07:57:21 2017 - [info] 
Thu Mar 16 07:57:21 2017 - [info] Executing master ip online change script to disable write on the current master:
Thu Mar 16 07:57:21 2017 - [info]   /etc/mha/scripts/master_ip_online_change --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=stop --orig_master_host=192.168.206.139 --orig_master_ip=192.168.206.139 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='replpass' --new_master_host=192.168.206.140 --new_master_ip=192.168.206.140 --new_master_port=3306 --new_master_user='root' --new_master_password='replpass' --orig_master_ssh_user=root --new_master_ssh_user=root  
Thu Mar 16 07:57:21 2017 836071 Set read_only on the new master.. ok.
Thu Mar 16 07:57:21 2017 846572 Drpping app user on the orig master..
Thu Mar 16 07:57:21 2017 848400 Waiting all running 1 threads are disconnected.. (max 1500 milliseconds)
{'Time' => '325','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '58','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:59184'}
Thu Mar 16 07:57:22 2017 350495 Waiting all running 1 threads are disconnected.. (max 1000 milliseconds)
{'Time' => '325','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '58','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:59184'}
Thu Mar 16 07:57:22 2017 853113 Waiting all running 1 threads are disconnected.. (max 500 milliseconds)
{'Time' => '326','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '58','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:59184'}
Thu Mar 16 07:57:23 2017 355738 Set read_only=1 on the orig master.. ok.
Thu Mar 16 07:57:23 2017 360013 Waiting all running 1 queries are disconnected.. (max 500 milliseconds)
{'Time' => '326','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '58','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:59184'}
Thu Mar 16 07:57:23 2017 858184 Killing all application threads..
Thu Mar 16 07:57:23 2017 860278 done.
Thu Mar 16 07:57:23 2017 - [info]  ok.
Thu Mar 16 07:57:23 2017 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Thu Mar 16 07:57:23 2017 - [info] Executing FLUSH TABLES WITH READ LOCK..
Thu Mar 16 07:57:23 2017 - [info]  ok.
Thu Mar 16 07:57:23 2017 - [info] Orig master binlog:pos is mysql-bin.000003:984.
Thu Mar 16 07:57:23 2017 - [info]  Waiting to execute all relay logs on 192.168.206.140(192.168.206.140:3306)..
Thu Mar 16 07:57:23 2017 - [info]  master_pos_wait(mysql-bin.000003:984) completed on 192.168.206.140(192.168.206.140:3306). Executed 0 events.
Thu Mar 16 07:57:23 2017 - [info]   done.
Thu Mar 16 07:57:23 2017 - [info] Getting new master's binlog name and position..
Thu Mar 16 07:57:23 2017 - [info]  mysql-bin.000002:1033
Thu Mar 16 07:57:23 2017 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.206.140', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='u_repl', MASTER_PASSWORD='xxx';
Thu Mar 16 07:57:23 2017 - [info] Executing master ip online change script to allow write on the new master:
Thu Mar 16 07:57:23 2017 - [info]   /etc/mha/scripts/master_ip_online_change --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=start --orig_master_host=192.168.206.139 --orig_master_ip=192.168.206.139 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='replpass' --new_master_host=192.168.206.140 --new_master_ip=192.168.206.140 --new_master_port=3306 --new_master_user='root' --new_master_password='replpass' --orig_master_ssh_user=root --new_master_ssh_user=root  
Thu Mar 16 07:57:24 2017 094535 Set read_only=0 on the new master.
Thu Mar 16 07:57:24 2017 096832 Creating app user on the new master..
Thu Mar 16 07:57:24 2017 - [info]  ok.
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info] * Switching slaves in parallel..
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info] -- Slave switch on host 192.168.206.141(192.168.206.141:3306) started, pid: 6433
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info] Log messages from 192.168.206.141 ...
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info]  Waiting to execute all relay logs on 192.168.206.141(192.168.206.141:3306)..
Thu Mar 16 07:57:24 2017 - [info]  master_pos_wait(mysql-bin.000003:984) completed on 192.168.206.141(192.168.206.141:3306). Executed 0 events.
Thu Mar 16 07:57:24 2017 - [info]   done.
Thu Mar 16 07:57:24 2017 - [info]  Resetting slave 192.168.206.141(192.168.206.141:3306) and starting replication from the new master 192.168.206.140(192.168.206.140:3306)..
Thu Mar 16 07:57:24 2017 - [info]  Executed CHANGE MASTER.
Thu Mar 16 07:57:24 2017 - [info]  Slave started.
Thu Mar 16 07:57:24 2017 - [info] End of log messages from 192.168.206.141 ...
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info] -- Slave switch on host 192.168.206.141(192.168.206.141:3306) succeeded.
Thu Mar 16 07:57:24 2017 - [info] Unlocking all tables on the orig master:
Thu Mar 16 07:57:24 2017 - [info] Executing UNLOCK TABLES..
Thu Mar 16 07:57:24 2017 - [info]  ok.
Thu Mar 16 07:57:24 2017 - [info] All new slave servers switched successfully.
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info] * Phase 5: New master cleanup phase..
Thu Mar 16 07:57:24 2017 - [info] 
Thu Mar 16 07:57:24 2017 - [info]  192.168.206.140: Resetting slave info succeeded.
Thu Mar 16 07:57:24 2017 - [info] Switching master to 192.168.206.140(192.168.206.140:3306) completed successfully.

View Code

重新将旧的master到场复制,指向新的master

mysql> change master to master_host='192.168.206.140',master_user='u_repl',master_password='replpass',master_auto_position=1;
mysql> start slave;

主动切换之非人机联作格局:

[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.139 --interactive=0

图片 31图片 32

[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.139 --interactive=0
Thu Mar 16 07:51:50 2017 - [info] MHA::MasterRotate version 0.56.
Thu Mar 16 07:51:50 2017 - [info] Starting online master switch..
Thu Mar 16 07:51:50 2017 - [info] 
Thu Mar 16 07:51:50 2017 - [info] * Phase 1: Configuration Check Phase..
Thu Mar 16 07:51:50 2017 - [info] 
Thu Mar 16 07:51:50 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 07:51:50 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 07:51:50 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 07:51:50 2017 - [info] GTID failover mode = 1
Thu Mar 16 07:51:50 2017 - [info] Current Alive Master: 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 07:51:50 2017 - [info] Alive Slaves:
Thu Mar 16 07:51:50 2017 - [info]   192.168.206.139(192.168.206.139:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 07:51:50 2017 - [info]     GTID ON
Thu Mar 16 07:51:50 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 07:51:50 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 07:51:50 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 07:51:50 2017 - [info]     GTID ON
Thu Mar 16 07:51:50 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 07:51:50 2017 - [info]     Not candidate for the new Master (no_master is set)
Thu Mar 16 07:51:50 2017 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Thu Mar 16 07:51:50 2017 - [info]  ok.
Thu Mar 16 07:51:50 2017 - [info] Checking MHA is not monitoring or doing failover..
Thu Mar 16 07:51:50 2017 - [info] Checking replication health on 192.168.206.139..
Thu Mar 16 07:51:50 2017 - [info]  ok.
Thu Mar 16 07:51:50 2017 - [info] Checking replication health on 192.168.206.141..
Thu Mar 16 07:51:50 2017 - [info]  ok.
Thu Mar 16 07:51:50 2017 - [info] 192.168.206.139 can be new master.
Thu Mar 16 07:51:50 2017 - [info] 
From:
192.168.206.140(192.168.206.140:3306) (current master)
 +--192.168.206.139(192.168.206.139:3306)
 +--192.168.206.141(192.168.206.141:3306)

To:
192.168.206.139(192.168.206.139:3306) (new master)
 +--192.168.206.141(192.168.206.141:3306)
Thu Mar 16 07:51:50 2017 - [info] Checking whether 192.168.206.139(192.168.206.139:3306) is ok for the new master..
Thu Mar 16 07:51:50 2017 - [info]  ok.
Thu Mar 16 07:51:50 2017 - [info] ** Phase 1: Configuration Check Phase completed.
Thu Mar 16 07:51:50 2017 - [info] 
Thu Mar 16 07:51:50 2017 - [info] * Phase 2: Rejecting updates Phase..
Thu Mar 16 07:51:50 2017 - [info] 
Thu Mar 16 07:51:50 2017 - [info] Executing master ip online change script to disable write on the current master:
Thu Mar 16 07:51:50 2017 - [info]   /etc/mha/scripts/master_ip_online_change --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=stop --orig_master_host=192.168.206.140 --orig_master_ip=192.168.206.140 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='replpass' --new_master_host=192.168.206.139 --new_master_ip=192.168.206.139 --new_master_port=3306 --new_master_user='root' --new_master_password='replpass' --orig_master_ssh_user=root --new_master_ssh_user=root  
Thu Mar 16 07:51:50 2017 428863 Set read_only on the new master.. ok.
Thu Mar 16 07:51:50 2017 436946 Drpping app user on the orig master..
Thu Mar 16 07:51:50 2017 438015 Waiting all running 1 threads are disconnected.. (max 1500 milliseconds)
{'Time' => '180','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '72','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:48684'}
Thu Mar 16 07:51:50 2017 940921 Waiting all running 1 threads are disconnected.. (max 1000 milliseconds)
{'Time' => '181','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '72','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:48684'}
Thu Mar 16 07:51:51 2017 443500 Waiting all running 1 threads are disconnected.. (max 500 milliseconds)
{'Time' => '181','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '72','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:48684'}
Thu Mar 16 07:51:51 2017 946057 Set read_only=1 on the orig master.. ok.
Thu Mar 16 07:51:51 2017 952395 Waiting all running 1 queries are disconnected.. (max 500 milliseconds)
{'Time' => '182','Command' => 'Binlog Dump GTID','db' => undef,'Id' => '72','Info' => undef,'User' => 'u_repl','State' => 'Master has sent all binlog to slave; waiting for more updates','Host' => '192.168.206.141:48684'}
Thu Mar 16 07:51:52 2017 448683 Killing all application threads..
Thu Mar 16 07:51:52 2017 450864 done.
Thu Mar 16 07:51:52 2017 - [info]  ok.
Thu Mar 16 07:51:52 2017 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Thu Mar 16 07:51:52 2017 - [info] Executing FLUSH TABLES WITH READ LOCK..
Thu Mar 16 07:51:52 2017 - [info]  ok.
Thu Mar 16 07:51:52 2017 - [info] Orig master binlog:pos is mysql-bin.000002:1033.
Thu Mar 16 07:51:52 2017 - [info]  Waiting to execute all relay logs on 192.168.206.139(192.168.206.139:3306)..
Thu Mar 16 07:51:52 2017 - [info]  master_pos_wait(mysql-bin.000002:1033) completed on 192.168.206.139(192.168.206.139:3306). Executed 0 events.
Thu Mar 16 07:51:52 2017 - [info]   done.
Thu Mar 16 07:51:52 2017 - [info] Getting new master's binlog name and position..
Thu Mar 16 07:51:52 2017 - [info]  mysql-bin.000003:984
Thu Mar 16 07:51:52 2017 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.206.139', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='u_repl', MASTER_PASSWORD='xxx';
Thu Mar 16 07:51:52 2017 - [info] Executing master ip online change script to allow write on the new master:
Thu Mar 16 07:51:52 2017 - [info]   /etc/mha/scripts/master_ip_online_change --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=start --orig_master_host=192.168.206.140 --orig_master_ip=192.168.206.140 --orig_master_port=3306 --orig_master_user='root' --orig_master_password='replpass' --new_master_host=192.168.206.139 --new_master_ip=192.168.206.139 --new_master_port=3306 --new_master_user='root' --new_master_password='replpass' --orig_master_ssh_user=root --new_master_ssh_user=root  
Thu Mar 16 07:51:52 2017 689824 Set read_only=0 on the new master.
Thu Mar 16 07:51:52 2017 691842 Creating app user on the new master..
Thu Mar 16 07:51:56 2017 - [info]  ok.
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info] * Switching slaves in parallel..
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info] -- Slave switch on host 192.168.206.141(192.168.206.141:3306) started, pid: 6405
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info] Log messages from 192.168.206.141 ...
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info]  Waiting to execute all relay logs on 192.168.206.141(192.168.206.141:3306)..
Thu Mar 16 07:51:56 2017 - [info]  master_pos_wait(mysql-bin.000002:1033) completed on 192.168.206.141(192.168.206.141:3306). Executed 0 events.
Thu Mar 16 07:51:56 2017 - [info]   done.
Thu Mar 16 07:51:56 2017 - [info]  Resetting slave 192.168.206.141(192.168.206.141:3306) and starting replication from the new master 192.168.206.139(192.168.206.139:3306)..
Thu Mar 16 07:51:56 2017 - [info]  Executed CHANGE MASTER.
Thu Mar 16 07:51:56 2017 - [info]  Slave started.
Thu Mar 16 07:51:56 2017 - [info] End of log messages from 192.168.206.141 ...
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info] -- Slave switch on host 192.168.206.141(192.168.206.141:3306) succeeded.
Thu Mar 16 07:51:56 2017 - [info] Unlocking all tables on the orig master:
Thu Mar 16 07:51:56 2017 - [info] Executing UNLOCK TABLES..
Thu Mar 16 07:51:56 2017 - [info]  ok.
Thu Mar 16 07:51:56 2017 - [info] All new slave servers switched successfully.
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info] * Phase 5: New master cleanup phase..
Thu Mar 16 07:51:56 2017 - [info] 
Thu Mar 16 07:51:56 2017 - [info]  192.168.206.139: Resetting slave info succeeded.
Thu Mar 16 07:51:56 2017 - [info] Switching master to 192.168.206.139(192.168.206.139:3306) completed successfully.

View Code

Master192.168.206.140宕机情势下切换主从:

masterha_master_switch --master_state=dead --conf=/etc/mha/app1/app1.cnf --dead_master_host=192.168.206.140 --dead_master_ip=192.168.206.140 --dead_master_port=3306 --new_master_host=192.168.206.139

图片 33图片 34

[root@localhost scripts]# masterha_master_switch --master_state=dead --conf=/etc/mha/app1/app1.cnf --dead_master_host=192.168.206.140 --dead_master_ip=192.168.206.140 --dead_master_port=3306 --new_master_host=192.168.206.139
Thu Mar 16 08:09:36 2017 - [info] Reading default configuration from /etc/masterha_default.cnf..
Thu Mar 16 08:09:36 2017 - [info] Reading application default configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 08:09:36 2017 - [info] Reading server configuration from /etc/mha/app1/app1.cnf..
Thu Mar 16 08:09:36 2017 - [info] MHA::MasterFailover version 0.56.
Thu Mar 16 08:09:36 2017 - [info] Starting master failover.
Thu Mar 16 08:09:36 2017 - [info] 
Thu Mar 16 08:09:36 2017 - [info] * Phase 1: Configuration Check Phase..
Thu Mar 16 08:09:36 2017 - [info] 
Thu Mar 16 08:09:37 2017 - [info] GTID failover mode = 1
Thu Mar 16 08:09:37 2017 - [info] Dead Servers:
Thu Mar 16 08:09:37 2017 - [info]   192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:37 2017 - [info] Checking master reachability via MySQL(double check)...
Thu Mar 16 08:09:37 2017 - [info]  ok.
Thu Mar 16 08:09:37 2017 - [info] Alive Servers:
Thu Mar 16 08:09:37 2017 - [info]   192.168.206.139(192.168.206.139:3306)
Thu Mar 16 08:09:37 2017 - [info]   192.168.206.141(192.168.206.141:3306)
Thu Mar 16 08:09:37 2017 - [info] Alive Slaves:
Thu Mar 16 08:09:37 2017 - [info]   192.168.206.139(192.168.206.139:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:37 2017 - [info]     GTID ON
Thu Mar 16 08:09:37 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:37 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 08:09:37 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:37 2017 - [info]     GTID ON
Thu Mar 16 08:09:37 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:37 2017 - [info]     Not candidate for the new Master (no_master is set)
Master 192.168.206.140(192.168.206.140:3306) is dead. Proceed? (yes/NO): yes
Thu Mar 16 08:09:42 2017 - [info] Starting GTID based failover.
Thu Mar 16 08:09:42 2017 - [info] 
Thu Mar 16 08:09:42 2017 - [info] ** Phase 1: Configuration Check Phase completed.
Thu Mar 16 08:09:42 2017 - [info] 
Thu Mar 16 08:09:42 2017 - [info] * Phase 2: Dead Master Shutdown Phase..
Thu Mar 16 08:09:42 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] HealthCheck: SSH to 192.168.206.140 is reachable.
Thu Mar 16 08:09:46 2017 - [info] Forcing shutdown so that applications never connect to the current master..
Thu Mar 16 08:09:46 2017 - [info] Executing master IP deactivation script:
Thu Mar 16 08:09:46 2017 - [info]   /etc/mha/scripts/master_ip_failover --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --orig_master_host=192.168.206.140 --orig_master_ip=192.168.206.140 --orig_master_port=3306 --command=stopssh --ssh_user=root  
Thu Mar 16 08:09:46 2017 - [info]  done.
Thu Mar 16 08:09:46 2017 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Thu Mar 16 08:09:46 2017 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Thu Mar 16 08:09:46 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] * Phase 3: Master Recovery Phase..
Thu Mar 16 08:09:46 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Thu Mar 16 08:09:46 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] The latest binary log file/position on all slaves is mysql-bin.000002:1033
Thu Mar 16 08:09:46 2017 - [info] Latest slaves (Slaves that received relay log files to the latest):
Thu Mar 16 08:09:46 2017 - [info]   192.168.206.139(192.168.206.139:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:46 2017 - [info]     GTID ON
Thu Mar 16 08:09:46 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:46 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 08:09:46 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:46 2017 - [info]     GTID ON
Thu Mar 16 08:09:46 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:46 2017 - [info]     Not candidate for the new Master (no_master is set)
Thu Mar 16 08:09:46 2017 - [info] The oldest binary log file/position on all slaves is mysql-bin.000002:1033
Thu Mar 16 08:09:46 2017 - [info] Oldest slaves:
Thu Mar 16 08:09:46 2017 - [info]   192.168.206.139(192.168.206.139:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:46 2017 - [info]     GTID ON
Thu Mar 16 08:09:46 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:46 2017 - [info]     Primary candidate for the new Master (candidate_master is set)
Thu Mar 16 08:09:46 2017 - [info]   192.168.206.141(192.168.206.141:3306)  Version=5.7.17-log (oldest major version between slaves) log-bin:enabled
Thu Mar 16 08:09:46 2017 - [info]     GTID ON
Thu Mar 16 08:09:46 2017 - [info]     Replicating from 192.168.206.140(192.168.206.140:3306)
Thu Mar 16 08:09:46 2017 - [info]     Not candidate for the new Master (no_master is set)
Thu Mar 16 08:09:46 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] * Phase 3.3: Determining New Master Phase..
Thu Mar 16 08:09:46 2017 - [info] 
Thu Mar 16 08:09:46 2017 - [info] 192.168.206.139 can be new master.
Thu Mar 16 08:09:46 2017 - [info] New master is 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 08:09:46 2017 - [info] Starting master failover..
Thu Mar 16 08:09:46 2017 - [info] 
From:
192.168.206.140(192.168.206.140:3306) (current master)
 +--192.168.206.139(192.168.206.139:3306)
 +--192.168.206.141(192.168.206.141:3306)

To:
192.168.206.139(192.168.206.139:3306) (new master)
 +--192.168.206.141(192.168.206.141:3306)

Starting master switch from 192.168.206.140(192.168.206.140:3306) to 192.168.206.139(192.168.206.139:3306)? (yes/NO): yes
Thu Mar 16 08:09:52 2017 - [info] New master decided manually is 192.168.206.139(192.168.206.139:3306)
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info] * Phase 3.3: New Master Recovery Phase..
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info]  Waiting all logs to be applied.. 
Thu Mar 16 08:09:52 2017 - [info]   done.
Thu Mar 16 08:09:52 2017 - [info] Getting new master's binlog name and position..
Thu Mar 16 08:09:52 2017 - [info]  mysql-bin.000003:984
Thu Mar 16 08:09:52 2017 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.206.139', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='u_repl', MASTER_PASSWORD='xxx';
Thu Mar 16 08:09:52 2017 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: mysql-bin.000003, 984, 347cbac6-0906-11e7-b957-000c2981a46e:1-2,
9e2c7c0f-0908-11e7-8230-000c29ab7544:2-3
Thu Mar 16 08:09:52 2017 - [info] Executing master IP activate script:
Thu Mar 16 08:09:52 2017 - [info]   /etc/mha/scripts/master_ip_failover --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234 --command=start --ssh_user=root --orig_master_host=192.168.206.140 --orig_master_ip=192.168.206.140 --orig_master_port=3306 --new_master_host=192.168.206.139 --new_master_ip=192.168.206.139 --new_master_port=3306 --new_master_user='root' --new_master_password='replpass'  
Set read_only=0 on the new master.
Creating app user on the new master..
Undefined subroutine &main::FIXME_xxx_create_user called at /etc/mha/scripts/master_ip_failover line 93.
Thu Mar 16 08:09:52 2017 - [error][/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln1588]  Failed to activate master IP address for 192.168.206.139(192.168.206.139:3306) with return code 10:0
Thu Mar 16 08:09:52 2017 - [warning] Proceeding.
Thu Mar 16 08:09:52 2017 - [info] ** Finished master recovery successfully.
Thu Mar 16 08:09:52 2017 - [info] * Phase 3: Master Recovery Phase completed.
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info] * Phase 4: Slaves Recovery Phase..
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info] * Phase 4.1: Starting Slaves in parallel..
Thu Mar 16 08:09:52 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info] -- Slave recovery on host 192.168.206.141(192.168.206.141:3306) started, pid: 6478. Check tmp log /var/log/mha/app1/192.168.206.141_3306_20170316080936.log if it takes time..
Thu Mar 16 08:09:53 2017 - [info] 
Thu Mar 16 08:09:53 2017 - [info] Log messages from 192.168.206.141 ...
Thu Mar 16 08:09:53 2017 - [info] 
Thu Mar 16 08:09:52 2017 - [info]  Resetting slave 192.168.206.141(192.168.206.141:3306) and starting replication from the new master 192.168.206.139(192.168.206.139:3306)..
Thu Mar 16 08:09:52 2017 - [info]  Executed CHANGE MASTER.
Thu Mar 16 08:09:52 2017 - [info]  Slave started.
Thu Mar 16 08:09:52 2017 - [info]  gtid_wait(347cbac6-0906-11e7-b957-000c2981a46e:1-2,
9e2c7c0f-0908-11e7-8230-000c29ab7544:2-3) completed on 192.168.206.141(192.168.206.141:3306). Executed 0 events.
Thu Mar 16 08:09:53 2017 - [info] End of log messages from 192.168.206.141.
Thu Mar 16 08:09:53 2017 - [info] -- Slave on host 192.168.206.141(192.168.206.141:3306) started.
Thu Mar 16 08:09:53 2017 - [info] All new slave servers recovered successfully.
Thu Mar 16 08:09:53 2017 - [info] 
Thu Mar 16 08:09:53 2017 - [info] * Phase 5: New master cleanup phase..
Thu Mar 16 08:09:53 2017 - [info] 
Thu Mar 16 08:09:53 2017 - [info] Resetting slave info on the new master..
Thu Mar 16 08:09:53 2017 - [info]  192.168.206.139: Resetting slave info succeeded.
Thu Mar 16 08:09:53 2017 - [info] Master failover to 192.168.206.139(192.168.206.139:3306) completed successfully.
Thu Mar 16 08:09:53 2017 - [info] 

----- Failover Report -----

app1: MySQL Master failover 192.168.206.140(192.168.206.140:3306) to 192.168.206.139(192.168.206.139:3306) succeeded

Master 192.168.206.140(192.168.206.140:3306) is down!

Check MHA Manager logs at localhost.localdomain for details.

Started manual(interactive) failover.
Invalidated master IP address on 192.168.206.140(192.168.206.140:3306)
Selected 192.168.206.139(192.168.206.139:3306) as a new master.
192.168.206.139(192.168.206.139:3306): OK: Applying all logs succeeded.
Failed to activate master IP address for 192.168.206.139(192.168.206.139:3306) with return code 10:0
192.168.206.141(192.168.206.141:3306): OK: Slave started, replicating from 192.168.206.139(192.168.206.139:3306)
192.168.206.139(192.168.206.139:3306): Resetting slave info succeeded.
Master failover to 192.168.206.139(192.168.206.139:3306) completed successfully.

View Code

本文地址: 

参照财富:

二、在Monitor host节点上配置MHA Manager

2.关闭防火墙

[root@localhost ~]# chkconfig iptables off
[root@localhost ~]# service iptables stop

 

4.安装MHA node所需的perl模块(DBD:mysql)

[root@localhost tmp]# yum -y install perl-DBD-MySQL ncftp perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-devel

    LVS+Keepalived搭建MyCAT高可用负载均衡集群

以下是CentOS6.8下MySQL MHA架构搭建笔记

mysql> grant all privileges on *.* to 'monitor'@'%' identified by 'monitor123';
Query OK, 0 rows affected (0.07 sec)

风华正茂、准备职业:

 2. 官方对于master_ip_failover,master_ip_online_change,send_report脚本,给出的只是sample,切换的逻辑须求自个儿定义。

3.manager节点设置mha4mysql-manager-0.56

[root@localhost tmp]# yum localinstall mha4mysql-manager-0.56-0.el6.noarch.rpm

图片 35图片 36

[root@localhost tmp]# yum localinstall mha4mysql-manager-0.56-0.el6.noarch.rpm
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Local Package Process
Examining mha4mysql-manager-0.56-0.el6.noarch.rpm: mha4mysql-manager-0.56-0.el6.noarch
Marking mha4mysql-manager-0.56-0.el6.noarch.rpm to be installed
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package mha4mysql-manager.noarch 0:0.56-0.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================
 Package                                      Arch                              Version                                 Repository                                                       Size
==============================================================================================================================================================================================
Installing:
 mha4mysql-manager                            noarch                            0.56-0.el6                              /mha4mysql-manager-0.56-0.el6.noarch                            325 k

Transaction Summary
==============================================================================================================================================================================================
Install       1 Package(s)

Total size: 325 k
Installed size: 325 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mha4mysql-manager-0.56-0.el6.noarch                                                                                                                                        1/1 
  Verifying  : mha4mysql-manager-0.56-0.el6.noarch                                                                                                                                        1/1 

Installed:
  mha4mysql-manager.noarch 0:0.56-0.el6                                                                                                                                                       

Complete!

View Code

认可安装内容

[root@localhost tmp]# rpm -ql mha4mysql-manager

图片 37图片 38

[root@localhost tmp]# rpm -ql mha4mysql-node
/usr/bin/apply_diff_relay_logs
/usr/bin/filter_mysqlbinlog
/usr/bin/purge_relay_logs
/usr/bin/save_binary_logs
/usr/share/man/man1/apply_diff_relay_logs.1.gz
/usr/share/man/man1/filter_mysqlbinlog.1.gz
/usr/share/man/man1/purge_relay_logs.1.gz
/usr/share/man/man1/save_binary_logs.1.gz
/usr/share/perl5/vendor_perl/MHA/BinlogHeaderParser.pm
/usr/share/perl5/vendor_perl/MHA/BinlogManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFindManager.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinder.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderElp.pm
/usr/share/perl5/vendor_perl/MHA/BinlogPosFinderXid.pm
/usr/share/perl5/vendor_perl/MHA/NodeConst.pm
/usr/share/perl5/vendor_perl/MHA/NodeUtil.pm
/usr/share/perl5/vendor_perl/MHA/SlaveUtil.pm
[root@localhost tmp]# rpm -ql mha4mysql-manager
/usr/bin/masterha_check_repl
/usr/bin/masterha_check_ssh
/usr/bin/masterha_check_status
/usr/bin/masterha_conf_host
/usr/bin/masterha_manager
/usr/bin/masterha_master_monitor
/usr/bin/masterha_master_switch
/usr/bin/masterha_secondary_check
/usr/bin/masterha_stop
/usr/share/man/man1/masterha_check_repl.1.gz
/usr/share/man/man1/masterha_check_ssh.1.gz
/usr/share/man/man1/masterha_check_status.1.gz
/usr/share/man/man1/masterha_conf_host.1.gz
/usr/share/man/man1/masterha_manager.1.gz
/usr/share/man/man1/masterha_master_monitor.1.gz
/usr/share/man/man1/masterha_master_switch.1.gz
/usr/share/man/man1/masterha_secondary_check.1.gz
/usr/share/man/man1/masterha_stop.1.gz
/usr/share/perl5/vendor_perl/MHA/Config.pm
/usr/share/perl5/vendor_perl/MHA/DBHelper.pm
/usr/share/perl5/vendor_perl/MHA/FileStatus.pm
/usr/share/perl5/vendor_perl/MHA/HealthCheck.pm
/usr/share/perl5/vendor_perl/MHA/ManagerAdmin.pm
/usr/share/perl5/vendor_perl/MHA/ManagerAdminWrapper.pm
/usr/share/perl5/vendor_perl/MHA/ManagerConst.pm
/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm
/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm
/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm
/usr/share/perl5/vendor_perl/MHA/MasterRotate.pm
/usr/share/perl5/vendor_perl/MHA/SSHCheck.pm
/usr/share/perl5/vendor_perl/MHA/Server.pm
/usr/share/perl5/vendor_perl/MHA/ServerManager.pm

View Code

Candicate master          192.168.244.20   2                 读

1.各节点使用ssh-keygen实现三台机器之间互相免密钥登入

Memo:即使是一向通过纠正/root/.ssh/authorized_keys,须求在装置manager的服务器上把自个儿的key也加进去,不然masterha_check_ssh验证通不过。

[root@localhost tmp]# ssh-keygen -t rsa

图片 39图片 40

[root@localhost tmp]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d5:0d:37:75:67:54:47:f0:e4:72:34:26:29:0d:1b:ba root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|           o+.=B&|
|          .oo*oB=|
|         ...o o +|
|         ..    o |
|        SE       |
|                 |
|                 |
|                 |
|                 |
+-----------------+

View Code

[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.206.140

图片 41图片 42

[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.206.140
root@192.168.206.140's password: 
Now try logging into the machine, with "ssh 'root@192.168.206.140'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

View Code

[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.206.141
[root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.206.142

 

二、Replication搭建

     可知,VIP(一九二一68.244.188卡塔尔国已经绑定到Master的eth0网卡上了。

5.在manager上安装全局配置

修改master_ip_failover脚本,增加VIP漂移相关四个参数

[root@localhost mha4mysql-manager-0.56]# vi /etc/mha/scripts/master_ip_failover

图片 43图片 44

[root@localhost scripts]# vi master_ip_failover

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;

my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password,
  $virtual_ip,     $orig_master_vip_eth,  $new_master_vip_eth
);
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,
  'new_master_user=s'     => $new_master_user,
  'new_master_password=s' => $new_master_password,
  'virtual_ip=s'          => $virtual_ip,
  'orig_master_vip_eth=s' => $orig_master_vip_eth,
  'new_master_vip_eth=s'  => $new_master_vip_eth
);

exit &main();

sub main {
  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 {

      # updating global catalog, etc
      `ssh $orig_master_host -o "ConnectTimeout=5" '/sbin/ifconfig $orig_master_vip_eth down'`;
      $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 {
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      print "Creating app user on the new master..n";
      # FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc
      # FIXME_xxx;

      my $real_eth=$new_master_vip_eth;
      $real_eth=~ s/:.*//g;
      my $new_master_network= `ssh $new_master_host '/sbin/ifconfig $real_eth'`;
      my $vip_netmask = join(".", $new_master_network =~ /Mask:(d+).(d+).(d+).(d+)/ );
      my $vip_broadcast = join(".", $new_master_network =~ /Bcast:(d+).(d+).(d+).(d+)/ );
      `ssh $new_master_host -o "ConnectTimeout=15" '/sbin/ifconfig $new_master_vip_eth $virtual_ip netmask $vip_netmask up; /sbin/arping -c 3 -A $virtual_ip; ping -I $virtual_ip -b $vip_broadcast -c 3 -c 3 -i 0.01 -q -W 10 > /dev/null'`;

      $exit_code = 0;
    };
    if ($@) {
      warn $@;

      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

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 --virtual_ip=ip --orig_master_vip_eth=eth --new_master_vip_eth=ethn";
}

View Code

修改master_ip_online_change脚本,增添VIP漂移相关四个参数

[root@localhost scripts]# vi master_ip_online_change

图片 45图片 46

[root@localhost scripts]# vi master_ip_online_change

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;

my $_tstart;
my $_running_interval = 0.1;
my (
  $command,              $orig_master_is_new_slave, $orig_master_host,
  $orig_master_ip,       $orig_master_port,         $orig_master_user,
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,
  $new_master_ip,        $new_master_port,          $new_master_user,
  $new_master_password,  $new_master_ssh_user,
  $virtual_ip,     $orig_master_vip_eth,  $new_master_vip_eth
);
GetOptions(
  'command=s'                => $command,
  'orig_master_is_new_slave' => $orig_master_is_new_slave,
  'orig_master_host=s'       => $orig_master_host,
  'orig_master_ip=s'         => $orig_master_ip,
  'orig_master_port=i'       => $orig_master_port,
  'orig_master_user=s'       => $orig_master_user,
  'orig_master_password=s'   => $orig_master_password,
  'orig_master_ssh_user=s'   => $orig_master_ssh_user,
  'new_master_host=s'        => $new_master_host,
  'new_master_ip=s'          => $new_master_ip,
  'new_master_port=i'        => $new_master_port,
  'new_master_user=s'        => $new_master_user,
  'new_master_password=s'    => $new_master_password,
  'new_master_ssh_user=s'    => $new_master_ssh_user,
  'virtual_ip=s'          => $virtual_ip,
  'orig_master_vip_eth=s' => $orig_master_vip_eth,
  'new_master_vip_eth=s'  => $new_master_vip_eth
);

exit &main();

sub current_time_us {
  my ( $sec, $microsec ) = gettimeofday();
  my $curdate = localtime($sec);
  return $curdate . " " . sprintf( "%06d", $microsec );
}

sub sleep_until {
  my $elapsed = tv_interval($_tstart);
  if ( $_running_interval > $elapsed ) {
    sleep( $_running_interval - $elapsed );
  }
}

sub get_threads_util {
  my $dbh                    = shift;
  my $my_connection_id       = shift;
  my $running_time_threshold = shift;
  my $type                   = shift;
  $running_time_threshold = 0 unless ($running_time_threshold);
  $type                   = 0 unless ($type);
  my @threads;

  my $sth = $dbh->prepare("SHOW PROCESSLIST");
  $sth->execute();

  while ( my $ref = $sth->fetchrow_hashref() ) {
    my $id         = $ref->{Id};
    my $user       = $ref->{User};
    my $host       = $ref->{Host};
    my $command    = $ref->{Command};
    my $state      = $ref->{State};
    my $query_time = $ref->{Time};
    my $info       = $ref->{Info};
    $info =~ s/^s*(.*?)s*$/$1/ if defined($info);
    next if ( $my_connection_id == $id );
    next if ( defined($query_time) && $query_time < $running_time_threshold );
    next if ( defined($command)    && $command eq "Binlog Dump" );
    next if ( defined($user)       && $user eq "system user" );
    next
      if ( defined($command)
      && $command eq "Sleep"
      && defined($query_time)
      && $query_time >= 1 );

    if ( $type >= 1 ) {
      next if ( defined($command) && $command eq "Sleep" );
      next if ( defined($command) && $command eq "Connect" );
    }

    if ( $type >= 2 ) {
      next if ( defined($info) && $info =~ m/^select/i );
      next if ( defined($info) && $info =~ m/^show/i );
    }

    push @threads, $ref;
  }
  return @threads;
}

sub main {
  if ( $command eq "stop" ) {
    ## Gracefully killing connections on the current master
    # 1. Set read_only= 1 on the new master
    # 2. DROP USER so that no app user can establish new connections
    # 3. Set read_only= 1 on the current master
    # 4. Kill current queries
    # * Any database access failure will result in script die.
    my $exit_code = 1;
    eval {
      ## Setting read_only=1 on the new master (to avoid accident)
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error(die_on_error)_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      print current_time_us() . " Set read_only on the new master.. ";
      $new_master_handler->enable_read_only();
      if ( $new_master_handler->is_read_only() ) {
        print "ok.n";
      }
      else {
        die "Failed!n";
      }
      $new_master_handler->disconnect();

      # Connecting to the orig master, die if any database error happens
      my $orig_master_handler = new MHA::DBHelper();
      $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
        $orig_master_user, $orig_master_password, 1 );

      ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
      $orig_master_handler->disable_log_bin_local();
      print current_time_us() . " Drpping app user on the orig master..n";
      # FIXME_xxx_drop_app_user($orig_master_handler);

      ## Waiting for N * 100 milliseconds so that current connections can exit
      my $time_until_read_only = 15;
      $_tstart = [gettimeofday];
      my @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_read_only > 0 && $#threads >= 0 ) {
        if ( $time_until_read_only % 5 == 0 ) {
          printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)n",
            current_time_us(), $#threads + 1, $time_until_read_only * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_read_only--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }

      ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
      print current_time_us() . " Set read_only=1 on the orig master.. ";
      $orig_master_handler->enable_read_only();
      if ( $orig_master_handler->is_read_only() ) {
        print "ok.n";
      }
      else {
        die "Failed!n";
      }

      ## Waiting for M * 100 milliseconds so that current update queries can complete
      my $time_until_kill_threads = 5;
      @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
        if ( $time_until_kill_threads % 5 == 0 ) {
          printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)n",
            current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_kill_threads--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }

      ## Terminating all threads
      print current_time_us() . " Killing all application threads..n";
      $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
      print current_time_us() . " done.n";
      $orig_master_handler->enable_log_bin_local();
      $orig_master_handler->disconnect();

      ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    ## Activating master ip on the new master
    # 1. Create app user with write privileges
    # 2. Moving backup script if needed
    # 3. Register new master's ip to the catalog database

# We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
# If exit code is 0 or 10, MHA does not abort
    my $exit_code = 10;
    eval {
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print current_time_us() . " Set read_only=0 on the new master.n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      print current_time_us() . " Creating app user on the new master..n";
      # FIXME_xxx_create_app_user($new_master_handler);
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc
      `ssh $orig_master_host -o "ConnectTimeout=5" '/sbin/ifconfig $orig_master_vip_eth down'`;
      my $real_eth=$new_master_vip_eth;
      $real_eth=~ s/:.*//g;
      my $new_master_network= `ssh $new_master_host '/sbin/ifconfig $real_eth'`;
      my $vip_netmask = join(".", $new_master_network =~ /Mask:(d+).(d+).(d+).(d+)/ );
      my $vip_broadcast = join(".", $new_master_network =~ /Bcast:(d+).(d+).(d+).(d+)/ );
      `ssh $new_master_host -o "ConnectTimeout=15" '/sbin/ifconfig $new_master_vip_eth $virtual_ip netmask $vip_netmask up; /sbin/arping -c 3 -A $virtual_ip; ping -I $virtual_ip -b $vip_broadcast -c 3 -c 3 -i 0.01 -q -W 10 > /dev/null'`;

      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

sub usage {
  print
"Usage: master_ip_online_change --command=start|stop|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 --virtual_ip=ip --orig_master_vip_eth=eth --new_master_vip_eth=ethn";
  die;
}

View Code

修改masterha_default.cnf全局配置

[root@localhost mha4mysql-manager-0.56]# vi /etc/masterha_default.cnf

图片 47图片 48

[root@localhost scripts]# vi /etc/masterha_default.cnf
[server default]
user=root
password=checkpass
ssh_user=root
repl_user=u_repl
repl_password=replpass
master_binlog_dir= /var/lib/mysql,/var/log/mysql
remote_workdir=/data/log/masterha
secondary_check_script= masterha_secondary_check -s 192.168.206.140 -s 192.168.206.139 --user=root --master_host=192.168.206.139 --master_port=3306
ping_interval=3
master_ip_failover_script= /etc/mha/scripts/master_ip_failover --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234
# shutdown_script= /etc/mha/scripts/power_manager
# report_script= /etc/mha/scripts/send_report
master_ip_online_change_script= /etc/mha/scripts/master_ip_online_change --virtual_ip=192.168.206.145 --orig_master_vip_eth=eth0:234 --new_master_vip_eth=eth0:234

View Code

开创日志目录:

[root@localhost mha4mysql-manager-0.56]# mkdir -p /var/log/mha/app1

配置app1.cnf:

[root@localhost mha4mysql-manager-0.56]# vi /etc/mha/app1/app1.cnf

图片 49图片 50

[root@localhost mha4mysql-manager-0.56]# vi /etc/mha/app1/app1.cnf

[server default]
manager_workdir=/var/log/mha/app1
manager_log=/var/log/mha/app1/manager.log

[server1]
hostname=192.168.206.139
master_binlog_dir=/var/lib/mysql/
candidate_master=1

[server2]
hostname=192.168.206.140
master_binlog_dir=/var/lib/mysql/
candidate_master=1

[server3]
hostname=192.168.206.141
master_binlog_dir=/var/lib/mysql/
no_master=1

View Code

 

1.建立replication用户(master和slave01)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'u_repl'@'192.168.206.%' IDENTIFIED BY 'replpass' with grant option;

      # tar xvf mha4mysql-node-0.56.tar.gz

     # cd mha4mysql-manager-0.56

五、 配置MHA

另好似下几个脚本需自定义:

       6. 在Slave上搭建从库

     # chmod +x /etc/init.d/keepalived

1> 安装keepalived

# ll /usr/local/bin/
total 44
-r-xr-xr-x 1 root root 16367 Jul 20 07:00 apply_diff_relay_logs
-r-xr-xr-x 1 root root  4807 Jul 20 07:00 filter_mysqlbinlog
-r-xr-xr-x 1 root root  8261 Jul 20 07:00 purge_relay_logs
-r-xr-xr-x 1 root root  7525 Jul 20 07:00 save_binary_logs

于今,MHA高可用项境中央搭建实现。

      在暗中同意情状下,从服务器上的衔接日志会在SQL线程试行完后被活动删除。但是在MHA碰到中,这一个中继日志在复苏别的从服务器时恐怕会被用到,由此需求禁用中继日志的全自动排除。改为按时手动排除SQL线程应用完的联网日志。

     # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.20

     # yum install perl-ExtUtils-MakeMaker -y

     如:

View Code

Slave                           192.168.244.30   3                 读

     # scp all.sql 192.168.244.30:/root/

图片 51图片 52

      # perl Makefile.PL  

     4. 将备份文件复制到Candicate master和Slave上

MHA Manager:

MHA在线切换的步子和公理

 

View Code

     1. 在Master上施行备份

      MHA在发生切换进度中,从库在回复的经过中,重视于relay log的连带音信,所以大家这里要将relay log的自动清楚设置为OFF,采纳手动清楚relay log的诀要。

 

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.30

Master                         192.168.244.10   1                 写入

一、在全部节点上设置MHA node

    # perl Makefile.PL

七、查看全部集群的意况

        --workdir:钦赐创设relay log的硬链接的职位,私下认可的是/var/tmp。由于系统分化分区成立硬链接文件会停业,故要求内定具体的硬链接的岗位。

 

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.20

# ll /usr/local/bin/
total 40
-r-xr-xr-x 1 root root 1991 Jul 20 00:50 masterha_check_repl
-r-xr-xr-x 1 root root 1775 Jul 20 00:50 masterha_check_ssh
-r-xr-xr-x 1 root root 1861 Jul 20 00:50 masterha_check_status
-r-xr-xr-x 1 root root 3197 Jul 20 00:50 masterha_conf_host
-r-xr-xr-x 1 root root 2513 Jul 20 00:50 masterha_manager
-r-xr-xr-x 1 root root 2161 Jul 20 00:50 masterha_master_monitor
-r-xr-xr-x 1 root root 2369 Jul 20 00:50 masterha_master_switch
-r-xr-xr-x 1 root root 5167 Jul 20 00:50 masterha_secondary_check
-r-xr-xr-x 1 root root 1735 Jul 20 00:50 masterha_stop

VIP配置能够利用二种格局,一是因而引进Keepalived来治本VIP,另意气风发种是在本子中手动管理。

  1. keepalived管理VIP

 

    2. 在富有的节点上设置mha node

         # purge_relay_logs --user=monitor --password=monitor123 -disable_relay_log_purge --workdir=/tmp/

    3. 查看备份文件all.sql中的CHANGE MASTEOdyssey语句

       # masterha_check_ssh --conf=/etc/masterha/app1.cnf

 

MHA(Master High Availability卡塔 尔(阿拉伯语:قطر‎是大器晚成套相对成熟的MySQL高可用方案,能成就在0~30s内自动达成数据库的故障切换操作,在master服务器不宕机的情事下,基本能保险数据的风姿洒脱致性。

     # make

        --password:mysql客商的密码

       8. 在Master中创设监察和控制客户

      # yum install perl-DBD-MySQL -y

      # cd mha4mysql-node-0.56

     # service keepalived start

图片 53图片 54

Wed Jul 20 14:33:36 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jul 20 14:33:36 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Jul 20 14:33:36 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Jul 20 14:33:36 2016 - [info] Starting SSH connection tests..
Wed Jul 20 14:33:51 2016 - [debug] 
Wed Jul 20 14:33:36 2016 - [debug]  Connecting via SSH from root@192.168.244.10(192.168.244.10:22) to root@192.168.244.20(192.168.244.20:22)..
Wed Jul 20 14:33:48 2016 - [debug]   ok.
Wed Jul 20 14:33:48 2016 - [debug]  Connecting via SSH from root@192.168.244.10(192.168.244.10:22) to root@192.168.244.30(192.168.244.30:22)..
Wed Jul 20 14:33:50 2016 - [debug]   ok.
Wed Jul 20 14:33:55 2016 - [debug] 
Wed Jul 20 14:33:37 2016 - [debug]  Connecting via SSH from root@192.168.244.30(192.168.244.30:22) to root@192.168.244.10(192.168.244.10:22)..
Wed Jul 20 14:33:49 2016 - [debug]   ok.
Wed Jul 20 14:33:49 2016 - [debug]  Connecting via SSH from root@192.168.244.30(192.168.244.30:22) to root@192.168.244.20(192.168.244.20:22)..
Wed Jul 20 14:33:54 2016 - [debug]   ok.
Wed Jul 20 14:33:55 2016 - [debug] 
Wed Jul 20 14:33:36 2016 - [debug]  Connecting via SSH from root@192.168.244.20(192.168.244.20:22) to root@192.168.244.10(192.168.244.10:22)..
Wed Jul 20 14:33:49 2016 - [debug]   ok.
Wed Jul 20 14:33:49 2016 - [debug]  Connecting via SSH from root@192.168.244.20(192.168.244.20:22) to root@192.168.244.30(192.168.244.30:22)..
Wed Jul 20 14:33:54 2016 - [debug]   ok.
Wed Jul 20 14:33:55 2016 - [info] All SSH connection tests passed successfully.

    3. 在Candicate master(192.168.244.20)上配置     

    若无Candicate master,多个Slave的身份平等,则五个Slave上都需安装keepalived。

        --host: mysqlserver地址

 

        --disable_relay_log_purge:暗中同意情形下,要是relay_log_purge=1,则脚本会直接退出。通过安装那个参数,该脚本会首先将relay_log_purge设置为1,覆灭掉relay log后,再将该参数设置为0。

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;
my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);

my $vip = '192.168.244.188';
my $key = "2";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip/24";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_send_garp = "/sbin/arping -U $vip -I eth0 -c 1";


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,
  'new_master_user=s'     => $new_master_user,
  'new_master_password=s' => $new_master_password,
);

exit &main();

sub main {
  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 "Disabling the VIP an 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" ) {

    # 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 {

      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      # print "Creating app user on the new master..n";
      # FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      print "Enabling the VIP $vip on the new master: $new_master_host n";
      &start_vip();
      $exit_code = 0;
    };
    if ($@) {
      warn $@;

      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

sub start_vip(){
    `ssh $ssh_user@$new_master_host " $ssh_start_vip "`;
    `ssh $ssh_user@$new_master_host " $ssh_send_garp "`;
}

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=portn";
}

3> 启动keepalived

个中,master对外提供写服务,备选master提供读服务,slave也提供相关的读服务,意气风发旦master宕机,将会把打算master升高为新的master,slave指向新的master

图片 55图片 56

MHA软件由两有的构成:Manager工具包和Node工具包,具体表明如下:

View Code

      注意:

     # masterha_check_repl --conf=/etc/masterha/app1.cnf

    # scp /etc/keepalived/keepalived.conf 192.168.244.20:/etc/keepalived/

     # perl Makefile.PL  

View Code

 

 

     # vim /etc/keepalived/keepalived.conf

      # ssh-keygen

     1. 在Monitor host(192.168.244.40卡塔尔国上成立MHA工作目录,何况创制连锁配置文件

    # yum install perl-CPAN -y

     查看MHA Manager监察和控制是否健康

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.10

    # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

      # head -n 30 all.sql

    # wget

         下边看看剧本的接受办法:

附:

图片 57图片 58

参考:

    将配备文件scp到Candicate master上

     # mysqldump --master-data=2 --single-transaction -R --triggers -A > all.sql

     2. 在Master上成立复制顾客

     # make install

 

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /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 inc/Module/Install/Can.pm line 6.
BEGIN failed--compilation aborted at inc/Module/Install/Can.pm line 6.
Compilation failed in require at inc/Module/Install.pm line 283.
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /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 inc/Module/Install/Makefile.pm line 4.
BEGIN failed--compilation aborted at inc/Module/Install/Makefile.pm line 4.
Compilation failed in require at inc/Module/Install.pm line 283.
Can't locate ExtUtils/MM_Unix.pm in @INC (@INC contains: inc /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 inc/Module/Install/Metadata.pm line 349.

1. 可独自调节和测量检验master_ip_failover,master_ip_online_change,send_report等脚本

    2. 在Master(192.168.244.10)上配置

local0.*           /var/log/keepalived.log

    # cd keepalived-1.2.24

#!/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

use strict;
use warnings FATAL => 'all';

use English qw(-no_match_vars);
use Getopt::Long;
use Pod::Usage;
use MHA::ManagerConst;

my @monitoring_servers;
my (
  $help,        $version,         $ssh_user,  $ssh_port,
  $ssh_options, $master_host,     $master_ip, $master_port,
  $master_user, $master_password, $ping_type
);
my $timeout = 5;

$| = 1;
GetOptions(
  'help'              => $help,
  'version'           => $version,
  'secondary_host=s'  => @monitoring_servers,
  'user=s'            => $ssh_user,
  'port=s'            => $ssh_port,
  'options=s'         => $ssh_options,
  'master_host=s'     => $master_host,
  'master_ip=s'       => $master_ip,
  'master_port=i'     => $master_port,
  'master_user=s'     => $master_user,
  'master_password=s' => $master_password,
  'ping_type=s'       => $ping_type,
  'timeout=i'         => $timeout,
);

if ($version) {
  print "masterha_secondary_check version $MHA::ManagerConst::VERSION.n";
  exit 0;
}

if ($help) {
  pod2usage(0);
}

unless ($master_host) {
  pod2usage(1);
}

sub exit_by_signal {
  exit 1;
}
local $SIG{INT} = $SIG{HUP} = $SIG{QUIT} = $SIG{TERM} = &exit_by_signal;

$ssh_user    = "root" unless ($ssh_user);
$ssh_port    = 22     unless ($ssh_port);
$master_port = 3306   unless ($master_port);

if ($ssh_options) {
  $MHA::ManagerConst::SSH_OPT_CHECK = $ssh_options;
}
$MHA::ManagerConst::SSH_OPT_CHECK =~ s/VAR_CONNECT_TIMEOUT/$timeout/;

# 0: master is not reachable from all monotoring servers
# 1: unknown errors
# 2: at least one of monitoring servers is not reachable from this script
# 3: master is reachable from at least one of monitoring servers
my $exit_code = 0;

foreach my $monitoring_server (@monitoring_servers) {
  my $ssh_user_host = $ssh_user . '@' . $monitoring_server;
  my $command =
"ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host "perl -e "
    . "\"use IO::Socket::INET; my \\\$sock = IO::Socket::INET->new"
    . "(PeerAddr => \\\"$master_host\\\", PeerPort=> $master_port, "
    . "Proto =>'tcp', Timeout => $timeout); if(\\\$sock) { close(\\\$sock); "
    . "exit 3; } exit 0;\" "";
  my $ret = system($command);
  $ret = $ret >> 8;
  if ( $ret == 0 ) {
    print
"Monitoring server $monitoring_server is reachable, Master is not reachable from $monitoring_server. OK.n";
    next;
  }
  if ( $ret == 3 ) {
    if ( defined $ping_type
      && $ping_type eq $MHA::ManagerConst::PING_TYPE_INSERT )
    {
      my $ret_insert;
      my $command_insert =
          "ssh $MHA::ManagerConst::SSH_OPT_CHECK -p $ssh_port $ssh_user_host '"
        . "/usr/bin/mysql -u$master_user -p$master_password -h$master_host "
        . "-e "CREATE DATABASE IF NOT EXISTS infra; "
        . "CREATE TABLE IF NOT EXISTS infra.chk_masterha (\`key\` tinyint NOT NULL primary key,\`val\` int(10) unsigned NOT NULL DEFAULT '0') engine=MyISAM; "
        . "INSERT INTO infra.chk_masterha values (1,unix_timestamp()) ON DUPLICATE KEY UPDATE val=unix_timestamp()"'";
      my $sigalrm_timeout = 3;
      eval {
        local $SIG{ALRM} = sub {
          die "timeout.n";
        };
        alarm $sigalrm_timeout;
        $ret_insert = system($command_insert);
        $ret_insert = $ret_insert >> 8;
        alarm 0;
      };
      if ( $@ || $ret_insert != 0 ) {
        print
"Monitoring server $monitoring_server is reachable, Master is not writable from $monitoring_server. OK.n";
        next;
      }
    }
    print "Master is reachable from $monitoring_server!n";
    $exit_code = 3;
    last;
  }
  else {
    print "Monitoring server $monitoring_server is NOT reachable!n";
    $exit_code = 2;
    last;
  }
}

exit $exit_code;

# ############################################################################
# Documentation
# ############################################################################

=pod

=head1 NAME

masterha_secondary_check - Checking master availability from additional network routes

=head1 SYNOPSIS

masterha_secondary_check -s secondary_host1 -s secondary_host2 .. --user=ssh_username --master_host=host --master_ip=ip --master_port=port

See online reference (http://code.google.com/p/mysql-master-ha/wiki/Parameters#secondary_check_script) for details.

=head1 DESCRIPTION

See online reference (http://code.google.com/p/mysql-master-ha/wiki/Parameters#secondary_check_script) for details.
  1. 经过脚本的秘技管理VIP

      1> 在编写该公文时,前面包车型大巴申明切记要去掉,MHA并不会将前边的故事情节识别为注释。

图片 59图片 60

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.10

《深入显出MySQL》 

     # mkdir -p /etc/masterha

    keepalived专门的学问原理和配备表明

    关于keepalived的参数的详实介绍,可参照:

    # ssh-keygen

 

2017-04-24 20:27:46: purge_relay_logs script started.
 Found relay_log.info: /var/lib/mysql/relay-log.info
 Opening /var/lib/mysql/mysqld-relay-bin.000001 ..
 Opening /var/lib/mysql/mysqld-relay-bin.000002 ..
 Opening /var/lib/mysql/mysqld-relay-bin.000003 ..
 Opening /var/lib/mysql/mysqld-relay-bin.000004 ..
 Opening /var/lib/mysql/mysqld-relay-bin.000005 ..
 Opening /var/lib/mysql/mysqld-relay-bin.000006 ..
 Executing SET GLOBAL relay_log_purge=1; FLUSH LOGS; sleeping a few seconds so that SQL thread can delete older relay log files (if i
t keeps up); SET GLOBAL relay_log_purge=0; .. ok.2017-04-24 20:27:50: All relay log purging operations succeeded.

       在Monitor host上执行

     # tar xvf mha4mysql-manager-0.56.tar.gz 

View Code

0 4 * * * /usr/local/bin/purge_relay_logs --user=monitor --password=monitor123 -disable_relay_log_purge --workdir=/tmp/ >> /tmp/purge
_relay_logs.log 2>&1

     启动Candicate master的keepalived

save_binary_logs:保存或复制master的二进制日志

2> 为keepalived设置单独的日记文件(非必得卡塔 尔(阿拉伯语:قطر‎

master_ip_online_change

    因为自己这里安装了Candicate master,故只在Master和Candicate master上设置。

 

    # ssh-keygen

     相当多童鞋对perl并不熟习,感觉不能够出手,其实,完全能够调用别的脚本,例如python,shell等。

env: /etc/init.d/keepalived: Permission denied

     # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.30

    1. 在MySQL服务器上安装MHA node所需的perl模块(DBD:mysql卡塔 尔(阿拉伯语:قطر‎

      2. 设置relay log排除方式(在每种Slave上卡塔 尔(英语:State of Qatar)

     设置复制消息

         MHA节点中含有了purge_relay_logs脚本,它可认为relay log创设硬链接,施行set global relay_log_purge=1,等待几分钟以便SQL线程切换成新的交接日志,再实行set global relay_log_purge=0。

九、开启MHA Manager监控

对此keepalived管理VIP,存在脑裂情状,即当主从网络现身难点时,slave会抢占VIP,那样会变成基本数据库都富有VIP,变成IP冲突,所以在网络不是很好的图景下,不提出利用keepalived服务。

mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.244.10',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='repl',
    -> MASTER_LOG_FILE='mysql-bin.000002',
    -> MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.19 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave statusG

masterha_secondary_check

View Code

 

实质上分娩际遇中,推荐这种格局来保管VIP,可实用防卫脑裂景况的发生。

   编辑/usr/local/bin/master_ip_failover

     增添如下内容:

    # tar xvf keepalived-1.2.24.tar.gz

图片 61图片 62

     在Master上修改

#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;
use MHA::NodeUtil;
use Time::HiRes qw( sleep gettimeofday tv_interval );
use Data::Dumper;

my $_tstart;
my $_running_interval = 0.1;

my $vip = '192.168.244.188';
my $key = "2";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip/24";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_send_garp = "/sbin/arping -U $vip -I eth0 -c 1";

my (
  $command,              $orig_master_is_new_slave, $orig_master_host,
  $orig_master_ip,       $orig_master_port,         $orig_master_user,
  $orig_master_password, $orig_master_ssh_user,     $new_master_host,
  $new_master_ip,        $new_master_port,          $new_master_user,
  $new_master_password,  $new_master_ssh_user,
);
GetOptions(
  'command=s'                => $command,
  'orig_master_is_new_slave' => $orig_master_is_new_slave,
  'orig_master_host=s'       => $orig_master_host,
  'orig_master_ip=s'         => $orig_master_ip,
  'orig_master_port=i'       => $orig_master_port,
  'orig_master_user=s'       => $orig_master_user,
  'orig_master_password=s'   => $orig_master_password,
  'orig_master_ssh_user=s'   => $orig_master_ssh_user,
  'new_master_host=s'        => $new_master_host,
  'new_master_ip=s'          => $new_master_ip,
  'new_master_port=i'        => $new_master_port,
  'new_master_user=s'        => $new_master_user,
  'new_master_password=s'    => $new_master_password,
  'new_master_ssh_user=s'    => $new_master_ssh_user,
);

exit &main();

sub start_vip(){
    `ssh $new_master_ssh_user@$new_master_host " $ssh_start_vip "`;
    `ssh $new_master_ssh_user@$new_master_host " $ssh_send_garp "`;
}

sub stop_vip(){
    `ssh $orig_master_ssh_user@$orig_master_host " $ssh_stop_vip "`;
}


sub current_time_us {
  my ( $sec, $microsec ) = gettimeofday();
  my $curdate = localtime($sec);
  return $curdate . " " . sprintf( "%06d", $microsec );
}

sub sleep_until {
  my $elapsed = tv_interval($_tstart);
  if ( $_running_interval > $elapsed ) {
    sleep( $_running_interval - $elapsed );
  }
}

sub get_threads_util {
  my $dbh                    = shift;
  my $my_connection_id       = shift;
  my $running_time_threshold = shift;
  my $type                   = shift;
  $running_time_threshold = 0 unless ($running_time_threshold);
  $type                   = 0 unless ($type);
  my @threads;

  my $sth = $dbh->prepare("SHOW PROCESSLIST");
  $sth->execute();

  while ( my $ref = $sth->fetchrow_hashref() ) {
    my $id         = $ref->{Id};
    my $user       = $ref->{User};
    my $host       = $ref->{Host};
    my $command    = $ref->{Command};
    my $state      = $ref->{State};
    my $query_time = $ref->{Time};
    my $info       = $ref->{Info};
    $info =~ s/^s*(.*?)s*$/$1/ if defined($info);
    next if ( $my_connection_id == $id );
    next if ( defined($query_time) && $query_time < $running_time_threshold );
    next if ( defined($command)    && $command eq "Binlog Dump" );
    next if ( defined($user)       && $user eq "system user" );
    next
      if ( defined($command)
      && $command eq "Sleep"
      && defined($query_time)
      && $query_time >= 1 );

    if ( $type >= 1 ) {
      next if ( defined($command) && $command eq "Sleep" );
      next if ( defined($command) && $command eq "Connect" );
    }

    if ( $type >= 2 ) {
      next if ( defined($info) && $info =~ m/^select/i );
      next if ( defined($info) && $info =~ m/^show/i );
    }

    push @threads, $ref;
  }
  return @threads;
}

sub main {
  if ( $command eq "stop" ) {
    ## Gracefully killing connections on the current master
    # 1. Set read_only= 1 on the new master
    # 2. DROP USER so that no app user can establish new connections
    # 3. Set read_only= 1 on the current master
    # 4. Kill current queries
    # * Any database access failure will result in script die.
    my $exit_code = 1;
    eval {
      ## Setting read_only=1 on the new master (to avoid accident)
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error(die_on_error)_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );
      print current_time_us() . " Set read_only on the new master.. ";
      $new_master_handler->enable_read_only();
      if ( $new_master_handler->is_read_only() ) {
        print "ok.n";
      }
      else {
        die "Failed!n";
      }
      $new_master_handler->disconnect();

      # Connecting to the orig master, die if any database error happens
      my $orig_master_handler = new MHA::DBHelper();
      $orig_master_handler->connect( $orig_master_ip, $orig_master_port,
        $orig_master_user, $orig_master_password, 1 );

      ## Drop application user so that nobody can connect. Disabling per-session binlog beforehand
      $orig_master_handler->disable_log_bin_local();
      # print current_time_us() . " Drpping app user on the orig master..n";
      #drop_app_user($orig_master_handler);

      ## Waiting for N * 100 milliseconds so that current connections can exit
      my $time_until_read_only = 15;
      $_tstart = [gettimeofday];
      my @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_read_only > 0 && $#threads >= 0 ) {
        if ( $time_until_read_only % 5 == 0 ) {
          printf
"%s Waiting all running %d threads are disconnected.. (max %d milliseconds)n",
            current_time_us(), $#threads + 1, $time_until_read_only * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_read_only--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }

      ## Setting read_only=1 on the current master so that nobody(except SUPER) can write
      print current_time_us() . " Set read_only=1 on the orig master.. ";
      $orig_master_handler->enable_read_only();
      if ( $orig_master_handler->is_read_only() ) {
        print "ok.n";
      }
      else {
        die "Failed!n";
      }

      ## Waiting for M * 100 milliseconds so that current update queries can complete
      my $time_until_kill_threads = 5;
      @threads = get_threads_util( $orig_master_handler->{dbh},
        $orig_master_handler->{connection_id} );
      while ( $time_until_kill_threads > 0 && $#threads >= 0 ) {
        if ( $time_until_kill_threads % 5 == 0 ) {
          printf
"%s Waiting all running %d queries are disconnected.. (max %d milliseconds)n",
            current_time_us(), $#threads + 1, $time_until_kill_threads * 100;
          if ( $#threads < 5 ) {
            print Data::Dumper->new( [$_] )->Indent(0)->Terse(1)->Dump . "n"
              foreach (@threads);
          }
        }
        sleep_until();
        $_tstart = [gettimeofday];
        $time_until_kill_threads--;
        @threads = get_threads_util( $orig_master_handler->{dbh},
          $orig_master_handler->{connection_id} );
      }

      ## Terminating all threads
      print current_time_us() . " Killing all application threads..n";
      $orig_master_handler->kill_threads(@threads) if ( $#threads >= 0 );
      print current_time_us() . " done.n";
      $orig_master_handler->enable_log_bin_local();
      $orig_master_handler->disconnect();

      ## Droping the VIP     
      print "Disabling the VIP an old master: $orig_master_host n";
      &stop_vip();

      ## After finishing the script, MHA executes FLUSH TABLES WITH READ LOCK
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "start" ) {
    ## Activating master ip on the new master
    # 1. Create app user with write privileges
    # 2. Moving backup script if needed
    # 3. Register new master's ip to the catalog database

# We don't return error even though activating updatable accounts/ip failed so that we don't interrupt slaves' recovery.
# If exit code is 0 or 10, MHA does not abort
    my $exit_code = 10;
    eval {
      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print current_time_us() . " Set read_only=0 on the new master.n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      #print current_time_us() . " Creating app user on the new master..n";
      # create_app_user($new_master_handler);
      print "Enabling the VIP $vip on the new master: $new_master_host n";
      &start_vip();
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      ## Update master ip on the catalog database, etc
      $exit_code = 0;
    };
    if ($@) {
      warn "Got Error: $@n";
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

sub usage {
  print
"Usage: master_ip_online_change --command=start|stop|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=portn";
  die;
}

4> MHA中引入keepalived

 

View Code

图片 63图片 64

图片 65图片 66

    在master-backup方式下,借使主库宕掉,VIP会自动漂移到Slave上,当主库修复,keepalived运行后,还恐怕会将VIP抢过来,就算设置了nopreempt(不抢占卡塔 尔(英语:State of Qatar)的方

*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
Can't locate CPAN.pm in @INC (@INC contains: inc /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 inc/Module/AutoInstall.pm line 277.

purge_relay_logs:湮灭中继日志(不会窒碍SQL线程卡塔尔

       7. slave服务器设置为read only

图片 67图片 68

     先在Master上启动

send_report

    检查通过~

        纵然通常,会来得“PING_OK”,不然会显示“NOT_RUNNING”,代表MHA监察和控制还从未开启。

    # mkdir /etc/keepalived

十一、VIP配置

View Code

     相对于原来的书文件,改良地点为93-95行

     5. 在Candicate master上搭建从库

     此中,-RAV4是备份存款和储蓄进度,--triggers是备份触发器 -A代表全库

global_defs {
   notification_email {
     slowtech@qq.com 
   }
   notification_email_from root@localhost.localdomain 
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30
   router_id MySQL-HA
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.244.188/24
    }
}

 

mysql> set global read_only=1;
Query OK, 0 rows affected (0.04 sec)

八、 检查MHA Manager的状态

     # make install

     在Monitor host上执行

     keepalived的日记暗许是出口到/var/log/message中

     # make 

     # scp all.sql 192.168.244.20:/root/

 

master_ip_failover

    # make

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.20

 

 

    # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

Monitor host                 192.168.244.40                      监察和控制集群组

MHA自动Failover与手动Failover的推行及原理

总结:

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=120;

View Code

     # ip a

     4. 在Slave(192.168.244.30)上配置     

它由两局地构成:MHA Manager(管理节点卡塔 尔(英语:State of Qatar)和MHA Node(数据节点卡塔 尔(阿拉伯语:قطر‎。此中,MHA Manager能够独立安顿在生机勃勃台独立的机器上管住多少个master-slave集群,也能够布置在大器晚成台slave上。MHA Node则运行在各种mysql节点上,MHA Manager会依期探测集群中的master节点,当master现身故障时,它自动将流行数据的slave升高为master,然后将别的具有的slave指向新的master。

 

 /usr/local/bin/master_ip_online_change --command=stop --orig_master_ip=192.168.244.10 --orig_master_host=192.168.244.10 --orig_master_port=3306 --orig_master_user=monitor --orig_master_password=monitor123 --orig_master_ssh_user=root --new_master_host=192.168.244.20 --new_master_ip=192.168.244.20 --new_master_port=3306 --new_master_user=monitor --new_master_password=monitor123 --new_master_ssh_user=root

 

MHA Node:

四、搭建主从复制景况

     通过报错能够见到,是互为表里信任包未有安装。

    式,该动作仍会生出。但在backup-backup形式下,当主库校订,并运维keepalived后,并不会抢占新主的VIP,纵然原主的priority高于新主的。

    # ./configure --prefix=/usr/local/keepalived

        --user:mysql用户名

注:操作系统均为GL450HEL 6.7

三、配置SSH登入无密码验证

*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.609)
- DBD::mysql            ...loaded. (4.013)
- Time::HiRes           ...missing.
- Config::Tiny          ...missing.
- Log::Dispatch         ...missing.
- Parallel::ForkManager ...missing.
- MHA::NodeConst        ...missing.
==> Auto-install the 5 mandatory module(s) from CPAN? [y] y
*** Dependencies will be installed the next time you type 'make'.
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Warning: prerequisite Config::Tiny 0 not found.
Warning: prerequisite Log::Dispatch 0 not found.
Warning: prerequisite MHA::NodeConst 0 not found.
Warning: prerequisite Parallel::ForkManager 0 not found.
Warning: prerequisite Time::HiRes 0 not found.
Writing Makefile for mha4mysql::manager

    注意:大家为何在这里处设置keepalived为backup方式呢?

View Code

     编辑/usr/local/bin/master_ip_failover

 

[root@node4 ~]# cat test.pl
#!/usr/bin/perl
use strict;
my $cmd='python /root/test.py';
system($cmd);

[root@node4 ~]# cat test.py
#!/usr/bin/python
print "hello,python"

[root@node4 ~]# perl test.pl
hello,python

集群新闻

    # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

/usr/local/bin/master_ip_failover --command=start --ssh_user=root --orig_master_host=192.168.244.10 --orig_master_ip=192.168.244.10 --orig_master_port=3306 --new_master_host=192.168.244.20 --new_master_ip=192.168.244.20 --new_master_port=3306 --new_master_user='monitor' --new_master_password='monitor123'
# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 is stopped(2:NOT_RUNNING).  

 

    # make install

      ignore_last_failover:在私下认可情形下,MHA发生切换后将会在/masterha/app1下产生app1.failover.complete文件,下一次再次切换的时候要是发掘该目录下存在该公文且两遍切换的年月间隔不足8小时的话,将不容许触发切换。除非在率先次切换后手动rm -rf /masterha/app1/app1.failover.complete。该参数代表忽视上次MHA触发切换发生的文书。

 

      下载地址为:

       注意:最佳是每台slave服务器在不一致有时间间点奉行该布署职责。

      2> 配置文件中安装了master_ip_failover_script,secondary_check_script,master_ip_online_change_script,report_script,对应的文件见小说末 尾。

  1 #!/usr/bin/env perl
  2 
  3 #  Copyright (C) 2011 DeNA Co.,Ltd.
  4 #
  5 #  This program is free software; you can redistribute it and/or modify
  6 #  it under the terms of the GNU General Public License as published by
  7 #  the Free Software Foundation; either version 2 of the License, or
  8 #  (at your option) any later version.
  9 #
 10 #  This program is distributed in the hope that it will be useful,
 11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 #  GNU General Public License for more details.
 14 #
 15 #  You should have received a copy of the GNU General Public License
 16 #   along with this program; if not, write to the Free Software
 17 #  Foundation, Inc.,
 18 #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 19 
 20 ## Note: This is a sample script and is not complete. Modify the script based on your environment.
 21 
 22 use strict;
 23 use warnings FATAL => 'all';
 24 
 25 use Getopt::Long;
 26 use MHA::DBHelper;
 27 my (
 28   $command,        $ssh_user,         $orig_master_host,
 29   $orig_master_ip, $orig_master_port, $new_master_host,
 30   $new_master_ip,  $new_master_port,  $new_master_user,
 31   $new_master_password
 32 );
 33 
 34 GetOptions(
 35   'command=s'             => $command,
 36   'ssh_user=s'            => $ssh_user,
 37   'orig_master_host=s'    => $orig_master_host,
 38   'orig_master_ip=s'      => $orig_master_ip,
 39   'orig_master_port=i'    => $orig_master_port,
 40   'new_master_host=s'     => $new_master_host,
 41   'new_master_ip=s'       => $new_master_ip,
 42   'new_master_port=i'     => $new_master_port,
 43   'new_master_user=s'     => $new_master_user,
 44   'new_master_password=s' => $new_master_password,
 45 );
 46 
 47 exit &main();
 48 
 49 sub main {
 50   if ( $command eq "stop" || $command eq "stopssh" ) {
 51 
 52     # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
 53     # If you manage master ip address at global catalog database,
 54     # invalidate orig_master_ip here.
 55     my $exit_code = 1;
 56     eval {
 57 
 58       # updating global catalog, etc
 59       $exit_code = 0;
 60     };
 61     if ($@) {
 62       warn "Got Error: $@n";
 63       exit $exit_code;
 64     }
 65     exit $exit_code;
 66   }
 67   elsif ( $command eq "start" ) {
 68 
 69     # all arguments are passed.
 70     # If you manage master ip address at global catalog database,
 71     # activate new_master_ip here.
 72     # You can also grant write access (create user, set read_only=0, etc) here.
 73     my $exit_code = 10;
 74     eval {
 75       my $new_master_handler = new MHA::DBHelper();
 76 
 77       # args: hostname, port, user, password, raise_error_or_not
 78       $new_master_handler->connect( $new_master_ip, $new_master_port,
 79         $new_master_user, $new_master_password, 1 );
 80 
 81       ## Set read_only=0 on the new master
 82       $new_master_handler->disable_log_bin_local();
 83       print "Set read_only=0 on the new master.n";
 84       $new_master_handler->disable_read_only();
 85 
 86       ## Creating an app user on the new master
 87       #print "Creating app user on the new master..n";
 88       #FIXME_xxx_create_user( $new_master_handler->{dbh} );
 89       $new_master_handler->enable_log_bin_local();
 90       $new_master_handler->disconnect();
 91 
 92       ## Update master ip on the catalog database, etc
 93       my $cmd;
 94       $cmd = 'ssh '.$ssh_user.'@'.$orig_master_ip.' service keepalived stop';
 95       system($cmd);
 96       $exit_code = 0;
 97     };
 98     if ($@) {
 99       warn $@;
100 
101       # If you want to continue failover, exit 10.
102       exit $exit_code;
103     }
104     exit $exit_code;
105   }
106   elsif ( $command eq "status" ) {
107 
108     # do nothing
109     exit 0;
110   }
111   else {
112     &usage();
113     exit 1;
114   }
115 }
116 
117 sub usage {
118   print
119 "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=portn";
120 }
#!/usr/bin/env perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';

use Getopt::Long;
use MHA::DBHelper;
my (
  $command,        $ssh_user,         $orig_master_host,
  $orig_master_ip, $orig_master_port, $new_master_host,
  $new_master_ip,  $new_master_port,  $new_master_user,
  $new_master_password
);

my $vip = '192.168.244.188';
my $key = "2";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip/24";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $ssh_send_garp = "/sbin/arping -U $vip -I eth0 -c 1";


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,
  'new_master_user=s'     => $new_master_user,
  'new_master_password=s' => $new_master_password,
);

exit &main();

sub main {
  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 "Disabling the VIP an 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" ) {

    # 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 {

      my $new_master_handler = new MHA::DBHelper();

      # args: hostname, port, user, password, raise_error_or_not
      $new_master_handler->connect( $new_master_ip, $new_master_port,
        $new_master_user, $new_master_password, 1 );

      ## Set read_only=0 on the new master
      $new_master_handler->disable_log_bin_local();
      print "Set read_only=0 on the new master.n";
      $new_master_handler->disable_read_only();

      ## Creating an app user on the new master
      # print "Creating app user on the new master..n";
      # FIXME_xxx_create_user( $new_master_handler->{dbh} );
      $new_master_handler->enable_log_bin_local();
      $new_master_handler->disconnect();

      print "Enabling the VIP $vip on the new master: $new_master_host n";
      &start_vip();
      $exit_code = 0;
    };
    if ($@) {
      warn $@;

      # If you want to continue failover, exit 10.
      exit $exit_code;
    }
    exit $exit_code;
  }
  elsif ( $command eq "status" ) {

    # do nothing
    exit 0;
  }
  else {
    &usage();
    exit 1;
  }
}

sub start_vip(){
    `ssh $ssh_user@$new_master_host " $ssh_start_vip "`;
    `ssh $ssh_user@$new_master_host " $ssh_send_garp "`;
}

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=portn";
}

图片 69图片 70

  1. masterha_check_ssh:检查MHA的SSH配置情况

  2. masterha_check_repl:检查MySQL的复制场景

  3. masterha_manager:启动MHA

  4. masterha_check_status:检查测验当前MHA运维状态

  5. masterha_master_monitor:检验master是或不是宕机

  6. masterha_master_switch:调节故障转移(自动或手动卡塔尔

  7. masterha_conf_host:增多或删除配置的server消息

  8. masterha_stop:关闭MHA

 

    1. 在manager上安顿到具备Node节点的无密码验证

图片 71图片 72

    设置log-bin后,重新奉行:

在MHA自动故障切换进度中,MHA试图保存master的二进制日志,从而最大程度地保障数据不舍弃,当这并不总是平价的,举例,主服务器硬件故障或不能通过ssh采访,MHA就万般无奈保存二进制日志,那样就只举行了故障转移但错过了风尚数据。可组合MySQL 5.5中出产的半合伙复制来裁减数据遗失的风险。

     # service keepalived start

     # mysql < all.sql 

# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 (pid:1873) is running(0:PING_OK), master:192.168.244.10

    # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.30

     # service keepalived start

     # perl Makefile.PL

    # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

        其中,

    

    实行完成后,会在/usr/local/bin下增加生产总量以下多少个文本  

    # service rsyslog restart 

View Code

在实际上生育中运用非常多的也是第三种,即在本子中手动管理VIP,所以,对keepalived不感兴趣的童鞋可径直跳过第意气风发种艺术。

Wed Jul 20 14:44:30 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jul 20 14:44:30 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Jul 20 14:44:30 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Jul 20 14:44:30 2016 - [info] MHA::MasterMonitor version 0.56.
Wed Jul 20 14:44:31 2016 - [info] GTID failover mode = 0
Wed Jul 20 14:44:31 2016 - [info] Dead Servers:
Wed Jul 20 14:44:31 2016 - [info] Alive Servers:
Wed Jul 20 14:44:31 2016 - [info]   192.168.244.10(192.168.244.10:3306)
Wed Jul 20 14:44:31 2016 - [info]   192.168.244.20(192.168.244.20:3306)
Wed Jul 20 14:44:31 2016 - [info]   192.168.244.30(192.168.244.30:3306)
Wed Jul 20 14:44:31 2016 - [info] Alive Slaves:
Wed Jul 20 14:44:31 2016 - [info]   192.168.244.20(192.168.244.20:3306)  Version=5.6.31 (oldest major version between slaves) log-bin:disabled
Wed Jul 20 14:44:31 2016 - [info]     Replicating from 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 14:44:31 2016 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Jul 20 14:44:31 2016 - [info]   192.168.244.30(192.168.244.30:3306)  Version=5.6.31 (oldest major version between slaves) log-bin:disabled
Wed Jul 20 14:44:31 2016 - [info]     Replicating from 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 14:44:31 2016 - [info] Current Alive Master: 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 14:44:31 2016 - [info] Checking slave configurations..
Wed Jul 20 14:44:31 2016 - [warning]  log-bin is not set on slave 192.168.244.20(192.168.244.20:3306). This host cannot be a master.
Wed Jul 20 14:44:31 2016 - [warning]  log-bin is not set on slave 192.168.244.30(192.168.244.30:3306). This host cannot be a master.
Wed Jul 20 14:44:31 2016 - [info] Checking replication filtering settings..
Wed Jul 20 14:44:31 2016 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Jul 20 14:44:31 2016 - [info]  Replication filtering check ok.
Wed Jul 20 14:44:31 2016 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln361] None of slaves can be master. Check failover configuration file or log-bin settings in my.cnf
Wed Jul 20 14:44:31 2016 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln424] Error happened on checking configurations.  at /usr/local/bin/masterha_check_repl line 48.
Wed Jul 20 14:44:31 2016 - [error][/usr/local/share/perl5/MHA/MasterMonitor.pm, ln523] Error happened on monitoring servers.
Wed Jul 20 14:44:31 2016 - [info] Got exit code 1 (Not master dead).

MySQL Replication Health is NOT OK!
Wed Jul 20 15:49:58 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jul 20 15:49:58 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Wed Jul 20 15:49:58 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Wed Jul 20 15:49:58 2016 - [info] MHA::MasterMonitor version 0.56.
Wed Jul 20 15:49:59 2016 - [info] GTID failover mode = 0
Wed Jul 20 15:49:59 2016 - [info] Dead Servers:
Wed Jul 20 15:49:59 2016 - [info] Alive Servers:
Wed Jul 20 15:49:59 2016 - [info]   192.168.244.10(192.168.244.10:3306)
Wed Jul 20 15:49:59 2016 - [info]   192.168.244.20(192.168.244.20:3306)
Wed Jul 20 15:49:59 2016 - [info]   192.168.244.30(192.168.244.30:3306)
Wed Jul 20 15:49:59 2016 - [info] Alive Slaves:
Wed Jul 20 15:49:59 2016 - [info]   192.168.244.20(192.168.244.20:3306)  Version=5.6.31-log (oldest major version between slaves) log-bin:enabled
Wed Jul 20 15:49:59 2016 - [info]     Replicating from 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 15:49:59 2016 - [info]     Primary candidate for the new Master (candidate_master is set)
Wed Jul 20 15:49:59 2016 - [info]   192.168.244.30(192.168.244.30:3306)  Version=5.6.31-log (oldest major version between slaves) log-bin:enabled
Wed Jul 20 15:49:59 2016 - [info]     Replicating from 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 15:49:59 2016 - [info] Current Alive Master: 192.168.244.10(192.168.244.10:3306)
Wed Jul 20 15:49:59 2016 - [info] Checking slave configurations..
Wed Jul 20 15:49:59 2016 - [info] Checking replication filtering settings..
Wed Jul 20 15:49:59 2016 - [info]  binlog_do_db= , binlog_ignore_db= 
Wed Jul 20 15:49:59 2016 - [info]  Replication filtering check ok.
Wed Jul 20 15:49:59 2016 - [info] GTID (with auto-pos) is not supported
Wed Jul 20 15:49:59 2016 - [info] Starting SSH connection tests..
Wed Jul 20 15:50:17 2016 - [info] All SSH connection tests passed successfully.
Wed Jul 20 15:50:17 2016 - [info] Checking MHA Node version..
Wed Jul 20 15:50:18 2016 - [info]  Version check ok.
Wed Jul 20 15:50:18 2016 - [info] Checking SSH publickey authentication settings on the current master..
Wed Jul 20 15:50:20 2016 - [info] HealthCheck: SSH to 192.168.244.10 is reachable.
Wed Jul 20 15:50:21 2016 - [info] Master MHA Node version is 0.56.
Wed Jul 20 15:50:21 2016 - [info] Checking recovery script configurations on 192.168.244.10(192.168.244.10:3306)..
Wed Jul 20 15:50:21 2016 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysqld-bin.000002 
Wed Jul 20 15:50:21 2016 - [info]   Connecting to root@192.168.244.10(192.168.244.10:22).. 
  Creating /tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to mysqld-bin.000002
Wed Jul 20 15:50:23 2016 - [info] Binlog setting check done.
Wed Jul 20 15:50:23 2016 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Wed Jul 20 15:50:23 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='monitor' --slave_host=192.168.244.20 --slave_ip=192.168.244.20 --slave_port=3306 --workdir=/tmp --target_version=5.6.31-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Jul 20 15:50:23 2016 - [info]   Connecting to root@192.168.244.20(192.168.244.20:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysqld-relay-bin.000004
    Temporary relay log file is /var/lib/mysql/mysqld-relay-bin.000004
    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Jul 20 15:50:28 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='monitor' --slave_host=192.168.244.30 --slave_ip=192.168.244.30 --slave_port=3306 --workdir=/tmp --target_version=5.6.31-log --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Wed Jul 20 15:50:28 2016 - [info]   Connecting to root@192.168.244.30(192.168.244.30:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to mysqld-relay-bin.000008
    Temporary relay log file is /var/lib/mysql/mysqld-relay-bin.000008
    Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
 done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Wed Jul 20 15:50:32 2016 - [info] Slaves settings check done.
Wed Jul 20 15:50:32 2016 - [info] 
192.168.244.10(192.168.244.10:3306) (current master)
 +--192.168.244.20(192.168.244.20:3306)
 +--192.168.244.30(192.168.244.30:3306)

Wed Jul 20 15:50:32 2016 - [info] Checking replication health on 192.168.244.20..
Wed Jul 20 15:50:32 2016 - [info]  ok.
Wed Jul 20 15:50:32 2016 - [info] Checking replication health on 192.168.244.30..
Wed Jul 20 15:50:32 2016 - [info]  ok.
Wed Jul 20 15:50:32 2016 - [info] Checking master_ip_failover_script status:
Wed Jul 20 15:50:32 2016 - [info]   /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.244.10 --orig_master_ip=192.168.244.10 --orig_master_port=3306 
Wed Jul 20 15:50:32 2016 - [info]  OK.
Wed Jul 20 15:50:32 2016 - [warning] shutdown_script is not defined.
Wed Jul 20 15:50:32 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

 

    报错很醒目,Candicate master和Slave都并未有运营log-bin,若无运行以来,后续就不能够进步为主

      一路按“Enter”

从那之后,MHA部办事处署完成,上面,来铺排VIP。

apply_diff_relay_logs:识别差别的relay log并将差异的event应用到其余slave中

     设置syslog

十、 关闭MHA Manager监控

        # vim /etc/cron.d/purge_relay_logs

        设置crontab来定时清理relay log

六、 检查SSH的配置

 

View Code

      remove_dead_master_conf:该参数代表当产生主从切换后,老的主库的IP将会从布局文件中移除。

mysql> set global relay_log_purge=0;
Query OK, 0 rows affected (0.00 sec)

      4. 将mysqlbinlog的门路增添随地境变量中

     # ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.244.10

图片 73图片 74

     # vim /etc/sysconfig/keepalived

    只需将配置文件中的priority设置为90

filter_mysqlbinlog:去除不须求的ROLLBACK事件(MHA已不再选取那几个工具卡塔尔国

mysql> grant replication slave on *.* to 'repl'@'192.168.244.%' identified by 'repl';
Query OK, 0 rows affected (0.09 sec)
# masterha_stop --conf=/etc/masterha/app1.cnf 
Stopped app1 successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1

     # vim /etc/masterha/app1.cnf

 

2> 配置keepalived

KEEPALIVED_OPTIONS="-D -d -S 0"

      其中,

#!/usr/bin/perl

#  Copyright (C) 2011 DeNA Co.,Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#  Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

## Note: This is a sample script and is not complete. Modify the script based on your environment.

use strict;
use warnings FATAL => 'all';
use Mail::Sender;
use Getopt::Long;

#new_master_host and new_slave_hosts are set only when recovering master succeeded
my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
my $smtp='smtp.126.com';
my $mail_from='slowtech@126.com';
my $mail_user='slowtech@126.com';
my $mail_pass='xxxxx';
my $mail_to=['slowtech@126.com'];
GetOptions(
  'orig_master_host=s' => $dead_master_host,
  'new_master_host=s'  => $new_master_host,
  'new_slave_hosts=s'  => $new_slave_hosts,
  'subject=s'          => $subject,
  'body=s'             => $body,
);
mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);

sub mailToContacts {
    my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
    open my $DEBUG, "> /tmp/monitormail.log"
        or die "Can't open the debug      file:$!n";
    my $sender = new Mail::Sender {
        ctype       => 'text/plain; charset=utf-8',
        encoding    => 'utf-8',
        smtp        => $smtp,
        from        => $mail_from,
        auth        => 'LOGIN',
        TLS_allowed => '0',
        authid      => $user,
        authpwd     => $passwd,
        to          => $mail_to,
        subject     => $subject,
        debug       => $DEBUG
    };

    $sender->MailMsg(
        {   msg   => $msg,
            debug => $DEBUG
        }
    ) or print $Mail::Sender::Error;
    return 1;
}



# Do whatever you want here
exit 0;
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI        ...loaded. (1.609)
- DBD::mysql ...loaded. (4.013)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::node

      由于该网站在国内被墙,相关文书下载后,放到了个体网盘中,

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c6:47:04 brd ff:ff:ff:ff:ff:ff
    inet 192.168.244.10/24 brd 192.168.244.255 scope global eth0
    inet 192.168.244.188/24 scope global secondary eth0
    inet6 fe80::20c:29ff:fec6:4704/64 scope link 
       valid_lft forever preferred_lft forever

View Code

编辑:数据库 本文来源:以下是CentOS6.8下MySQL MHA架构搭建笔记,它自动将

关键词:

  • 上一篇:没有了
  • 下一篇:没有了