Sat Dec 29 16:04:00 2018 - [debug] ok.
root@server02(192.168.200.112:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
root@server03(192.168.200.113:22) to root@server01(192.168.200.111:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
root@server03(192.168.200.113:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
root@server03(192.168.200.113:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
root@server01(192.168.200.111:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:03:59 2018 - [debug] ok.
root@server01(192.168.200.111:22) to root@server03(192.168.200.113:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
root@server01(192.168.200.111:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
root@server04(192.168.200.114:22) to root@server01(192.168.200.111:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
root@server04(192.168.200.114:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
root@server04(192.168.200.114:22) to root@server03(192.168.200.113:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
Sat Dec 29 16:04:02 2018 - [info] All SSH connection tests passed successfully.
Sat Dec 29 16:03:57 2018 - [warning] Global configuration file /etc/masterha_default.cnf not
found. Skipping.
Sat Dec 29 16:03:57 2018 - [info] Reading application default configurations from
/etc/masterha/app1.cnf..
Sat Dec 29 16:03:57 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
Sat Dec 29 16:03:57 2018 - [info] Starting SSH connection tests..
Sat Dec 29 16:04:02 2018 - [debug]
Sat Dec 29 16:03:58 2018 - [debug] Connecting via SSH from
root@server02(192.168.200.112:22) to root@server01(192.168.200.111:22)..
Sat Dec 29 16:03:59 2018 - [debug] ok.
Sat Dec 29 16:03:59 2018 - [debug] Connecting via SSH from
root@server02(192.168.200.112:22) to root@server03(192.168.200.113:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
Sat Dec 29 16:04:00 2018 - [debug] Connecting via SSH from
root@server02(192.168.200.112:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
Sat Dec 29 16:04:02 2018 - [debug]
Sat Dec 29 16:03:58 2018 - [debug] Connecting via SSH from
root@server03(192.168.200.113:22) to root@server01(192.168.200.111:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
Sat Dec 29 16:04:00 2018 - [debug] Connecting via SSH from
root@server03(192.168.200.113:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
Sat Dec 29 16:04:01 2018 - [debug] Connecting via SSH from
root@server03(192.168.200.113:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
Sat Dec 29 16:04:02 2018 - [debug]
Sat Dec 29 16:03:57 2018 - [debug] Connecting via SSH from
root@server01(192.168.200.111:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:03:59 2018 - [debug] ok.
Sat Dec 29 16:03:59 2018 - [debug] Connecting via SSH from
root@server01(192.168.200.111:22) to root@server03(192.168.200.113:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
Sat Dec 29 16:04:00 2018 - [debug] Connecting via SSH from
root@server01(192.168.200.111:22) to root@server04(192.168.200.114:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
Sat Dec 29 16:04:02 2018 - [debug]
Sat Dec 29 16:03:59 2018 - [debug] Connecting via SSH from
root@server04(192.168.200.114:22) to root@server01(192.168.200.111:22)..
Sat Dec 29 16:04:00 2018 - [debug] ok.
Sat Dec 29 16:04:00 2018 - [debug] Connecting via SSH from
root@server04(192.168.200.114:22) to root@server02(192.168.200.112:22)..
Sat Dec 29 16:04:01 2018 - [debug] ok.
Sat Dec 29 16:04:01 2018 - [debug] Connecting via SSH from
root@server04(192.168.200.114:22) to root@server03(192.168.200.113:22)..
Sat Dec 29 16:04:02 2018 - [debug] ok.
Sat Dec 29 16:04:02 2018 - [info] All SSH connection tests passed successfully.
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Sat Dec 29 16:04:53 2018 - [warning] Global configuration file /etc/masterha_default.cnf not
found. Skipping.
Sat Dec 29 16:04:53 2018 - [info] Reading application default configurations from
/etc/masterha/app1.cnf..
Sat Dec 29 16:04:53 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
Sat Dec 29 16:04:53 2018 - [info] MHA::MasterMonitor version 0.56.
Creating directory /var/log/masterha/app1.. done.
Sat Dec 29 16:04:55 2018 - [info] Dead Servers:
Sat Dec 29 16:04:55 2018 - [info] Alive Servers:
Sat Dec 29 16:04:55 2018 - [info] server01(192.168.200.111:3306)
Sat Dec 29 16:04:55 2018 - [info] server02(192.168.200.112:3306)
Sat Dec 29 16:04:55 2018 - [info] server03(192.168.200.113:3306)
Sat Dec 29 16:04:55 2018 - [info] server04(192.168.200.114:3306)
Sat Dec 29 16:04:55 2018 - [info] Alive Slaves:
Sat Dec 29 16:04:55 2018 - [info] server02(192.168.200.112:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:04:55 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:04:55 2018 - [info] Primary candidate for the new Master
(candidate_master is set)
Sat Dec 29 16:04:55 2018 - [info] server03(192.168.200.113:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:04:55 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:04:55 2018 - [info] server04(192.168.200.114:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:04:55 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:04:55 2018 - [info] Current Alive Master: server01(192.168.200.111:3306)
Sat Dec 29 16:04:55 2018 - [info] Checking slave configurations..
Sat Dec 29 16:04:55 2018 - [info] Checking replication filtering settings..
Sat Dec 29 16:04:55 2018 - [info] binlog_do_db= , binlog_ignore_db=
Sat Dec 29 16:04:55 2018 - [info] Replication filtering check ok.
Sat Dec 29 16:04:55 2018 - [info] Starting SSH connection tests..
Sat Dec 29 16:05:00 2018 - [info] All SSH connection tests passed successfully.
Sat Dec 29 16:05:00 2018 - [info] Checking MHA Node version..
Sat Dec 29 16:05:02 2018 - [info] Version check ok.
Sat Dec 29 16:05:02 2018 - [info] Checking SSH publickey authentication settings on the current
master..
Sat Dec 29 16:05:03 2018 - [info] HealthCheck: SSH to server01 is reachable.
Sat Dec 29 16:05:04 2018 - [info] Master MHA Node version is 0.56.
Sat Dec 29 16:05:04 2018 - [info] Checking recovery script configurations on the current
master..
Sat Dec 29 16:05:04 2018 - [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=master-bin.000001 Sat Dec 29 16:05:04 2018 - [info]
Connecting to root@server01(server01)..
Creating /tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to master-bin.000001
Sat Dec 29 16:05:04 2018 - [info] Master setting check done.
Sat Dec 29 16:05:04 2018 - [info] Checking SSH publickey authentication and checking recovery
script configurations on all alive slave servers..
Sat Dec 29 16:05:04 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server02 --slave_ip=192.168.200.112
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:05:04 2018 - [info] Connecting to
root@192.168.200.112(server02:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000002
Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:05:05 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server03 --slave_ip=192.168.200.113
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:05:05 2018 - [info] Connecting to
root@192.168.200.113(server03:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to slave-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:05:07 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server04 --slave_ip=192.168.200.114
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:05:07 2018 - [info] Connecting to
root@192.168.200.114(server04:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to slave-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:05:08 2018 - [info] Slaves settings check done.
Sat Dec 29 16:05:09 2018 - [info]
server01 (current master)
+--server02
+--server03
+--server04
Sat Dec 29 16:05:09 2018 - [info] Checking replication health on server02..
Sat Dec 29 16:05:09 2018 - [info] ok.
Sat Dec 29 16:05:09 2018 - [info] Checking replication health on server03..
Sat Dec 29 16:05:09 2018 - [info] ok.
Sat Dec 29 16:05:09 2018 - [info] Checking replication health on server04..
Sat Dec 29 16:05:09 2018 - [info] ok.
Sat Dec 29 16:05:09 2018 - [info] Checking master_ip_failover_script status:
Sat Dec 29 16:05:09 2018 - [info] /usr/local/bin/master_ip_failover --command=status
--ssh_user=root --orig_master_host=server01 --orig_master_ip=192.168.200.111 --orig_m
aster_port=3306
IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===
Checking the Status of the script.. OK
bash: /etc/init.d/keepalived: 没有那个文件或目录
Sat Dec 29 16:05:09 2018 - [info] OK.
Sat Dec 29 16:05:09 2018 - [warning] shutdown_script is not defined.
Sat Dec 29 16:05:09 2018 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
九、VIP 配置管理
Master vip 配置有两种方式,一种是通过 keepalived 或者 heartbeat 类似的软件的方式管
理 VIP 的浮动,另一种为通过命令方式管理。
通过命令方式管理 VIP 地址:
打开在前面编辑过的文件/etc/masterha/app1.cnf,检查如下行是否正确,再检查集群状态。
[root@server05 ~]# grep -n 'master_ip_failover_script' /etc/masterha/app1.cnf
9:master_ip_failover_script= /usr/local/bin/master_ip_failover
Primary Master(192.168.200.111)
[root@server01 ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.100/24 brd 192.168.200.255 scope global secondary ens32:1
Server05(192.168.200.115)修改故障转移脚本
[root@server05 ~]# head -13 /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port,
);
my $vip = '192.168.200.100'; # 写入 VIP
my $key = "1"; #非 keepalived 方式切换脚本使用的
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip"; #若是使用 keepalived
my $ssh_stop_vip = "/sbin/ifconfig ens32:$key down"; #那么这里写服务的开关命令
/usr/local/bin/master_ip_failover 文件的内容意思是当主库发生故障时,会触发 MHA 切
换,MHA manager 会停掉主库上的 ens32:1 接口,触发虚拟 ip 漂移到备选从库,从而完成
切换。
Server05(192.168.200.115) 检查 manager 状态
masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
注意:如果正常会显示"PING_OK",否则会显示"NOT_RUNNING",代表 MHA 监控没有开启。
Server05(192.168.200.115) 开启 manager 监控
[root@server05 ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dea
d_master_conf --ignore_last_failover< /dev/null >/var/log/masterha/app1/manager.log 2>&1
&
[1] 65837
启动参数介绍:
--remove_dead_master_conf 该参数代表当发生主从切换后,老的主库的 ip 将会从配置
文件中移除。
--manger_log 日志存放位置
--ignore_last_failover 在缺省情况下,如果 MHA 检测到连续发生宕机,且两次宕
机间隔不足 8 小时的话,则不会进行 Failover,之所以这样限制是为了避免 ping-pong 效应。
该参数代表忽略上次 MHA 触发切换产生的文件,默认情况下,MHA 发生切换后会在日志目
录,也就是上面我设置的/data 产生 app1.failover.complete 文件,下次再次切换的时候如果
发现该目录下存在该文件将不允许触发切换,除非在第一次切换后收到删除该文件,为了方
便,这里设置为--ignore_last_failover。
Server05(192.168.200.115)查看 Server05 监控是否正常:
[root@monitor ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:65837) is running(0:PING_OK), master:server01
可以看见已经在监控了
Server05(192.168.200.115)查看启动日志
[root@server05 ~]# cat /var/log/masterha/app1/manager.log
Sat Dec 29 16:09:50 2018 - [warning] Global configuration file /etc/masterha_default.cnf not
found. Skipping.
Sat Dec 29 16:09:50 2018 - [info] Reading application default configurations from
/etc/masterha/app1.cnf..
Sat Dec 29 16:09:50 2018 - [info] Reading server configurations from /etc/masterha/app1.cnf..
Sat Dec 29 16:09:50 2018 - [info] MHA::MasterMonitor version 0.56.
Sat Dec 29 16:09:51 2018 - [info] Dead Servers:
Sat Dec 29 16:09:51 2018 - [info] Alive Servers:
Sat Dec 29 16:09:51 2018 - [info] server01(192.168.200.111:3306)
Sat Dec 29 16:09:51 2018 - [info] server02(192.168.200.112:3306)
Sat Dec 29 16:09:51 2018 - [info] server03(192.168.200.113:3306)
Sat Dec 29 16:09:51 2018 - [info] server04(192.168.200.114:3306)
Sat Dec 29 16:09:51 2018 - [info] Alive Slaves:
Sat Dec 29 16:09:51 2018 - [info] server02(192.168.200.112:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:09:51 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:09:51 2018 - [info] Primary candidate for the new Master
(candidate_master is set)
Sat Dec 29 16:09:51 2018 - [info] server03(192.168.200.113:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:09:51 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:09:51 2018 - [info] server04(192.168.200.114:3306) Version=5.5.56-MariaDB
(oldest major version between slaves) log-bin:enabled
Sat Dec 29 16:09:51 2018 - [info] Replicating from
192.168.200.111(192.168.200.111:3306)
Sat Dec 29 16:09:51 2018 - [info] Current Alive Master: server01(192.168.200.111:3306)
Sat Dec 29 16:09:51 2018 - [info] Checking slave configurations..
Sat Dec 29 16:09:51 2018 - [info] Checking replication filtering settings..
Sat Dec 29 16:09:51 2018 - [info] binlog_do_db= , binlog_ignore_db=
Sat Dec 29 16:09:51 2018 - [info] Replication filtering check ok.
Sat Dec 29 16:09:51 2018 - [info] Starting SSH connection tests..
Sat Dec 29 16:09:58 2018 - [info] All SSH connection tests passed successfully.
Sat Dec 29 16:09:58 2018 - [info] Checking MHA Node version..
Sat Dec 29 16:10:01 2018 - [info] Version check ok.
Sat Dec 29 16:10:01 2018 - [info] Checking SSH publickey authentication settings on the current
master..
Sat Dec 29 16:10:02 2018 - [info] HealthCheck: SSH to server01 is reachable.
Sat Dec 29 16:10:03 2018 - [info] Master MHA Node version is 0.56.
Sat Dec 29 16:10:03 2018 - [info] Checking recovery script configurations on the current
master..
Sat Dec 29 16:10:03 2018 - [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=master-bin.000001 Sat Dec 29 16:10:03 2018 - [info]
Connecting to root@server01(server01)..
Creating /tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/lib/mysql, up to master-bin.000001
Sat Dec 29 16:10:03 2018 - [info] Master setting check done.
Sat Dec 29 16:10:03 2018 - [info] Checking SSH publickey authentication and checking recovery
script configurations on all alive slave servers..
Sat Dec 29 16:10:03 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server02 --slave_ip=192.168.200.112
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:10:03 2018 - [info] Connecting to
root@192.168.200.112(server02:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000002
Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:10:04 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server03 --slave_ip=192.168.200.113
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:10:04 2018 - [info] Connecting to
root@192.168.200.113(server03:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to slave-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:10:05 2018 - [info] Executing command : apply_diff_relay_logs
--command=test --slave_user='root' --slave_host=server04 --slave_ip=192.168.200.114
--slave_po
rt=3306 --workdir=/tmp --target_version=5.5.56-MariaDB --manager_version=0.56
--relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/
--slave_pass=xxxSat Dec 29 16:10:05 2018 - [info] Connecting to
root@192.168.200.114(server04:22)..
Checking slave recovery environment settings..
Opening /var/lib/mysql/relay-log.info ... ok.
Relay log found at /var/lib/mysql, up to slave-relay-bin.000002
Temporary relay log file is /var/lib/mysql/slave-relay-bin.000002
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Sat Dec 29 16:10:06 2018 - [info] Slaves settings check done.
Sat Dec 29 16:10:06 2018 - [info]
server01 (current master)
+--server02
+--server03
+--server04
Sat Dec 29 16:10:06 2018 - [info] Checking master_ip_failover_script status:
Sat Dec 29 16:10:06 2018 - [info] /usr/local/bin/master_ip_failover --command=status
--ssh_user=root --orig_master_host=server01 --orig_master_ip=192.168.200.111 --orig_m
aster_port=3306
IN SCRIPT TEST====/etc/init.d/keepalived stop==/etc/init.d/keepalived start===
Checking the Status of the script.. OK
bash: /etc/init.d/keepalived: 没有那个文件或目录
Sat Dec 29 16:10:07 2018 - [info] OK.
Sat Dec 29 16:10:07 2018 - [warning] shutdown_script is not defined.
Sat Dec 29 16:10:07 2018 - [info] Set master ping interval 1 seconds.
Sat Dec 29 16:10:07 2018 - [warning] secondary_check_script is not defined. It is highly
recommended setting it to check master reachability from two or more routes.
Sat Dec 29 16:10:07 2018 - [info] Starting ping health check on
server01(192.168.200.111:3306)..
Thu Aug 31 21:55:23 2017 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't
respond..
注意:其中"Ping(SELECT) succeeded, waiting until MySQL doesn't respond.."说明整个系统已经
开始监控了。
关闭 MHA manager 监控,忽略操作
masterha_stop --conf=/etc/masterha/app1.cnf
发现已经将 VIP:192.168.200.100 绑定在网卡 ens32。
[root@server01 ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.100/24 brd 192.168.200.255 scope global secondary ens32:1
Primary Master(192.168.200.111) 模拟主库故障
[root@server01 ~]# systemctl stop mariadb
[root@server01 ~]# netstat -lnpt | grep :3306
[root@server01 ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
slave1(192.168.200.113)状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.112
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 1372
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
slave2(192.168.200.114)状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.112
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 1372
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Server05(192.168.200.115) 监控已经自动关闭:
[root@server05 ~]#
[1]+ 完成 nohup masterha_manager --conf=/etc/masterha/app1.cnf
--remove_dead_master_conf --ignore_last_failover < /dev/null >
/var/log/masterha/app1/manager.log 2>&1
Server05(192.168.200.115) 查看监控配置文件已经发生了变化(server01 的配置已被删除):
[root@server05 ~]# cat /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=repl
user=root
[server2]
candidate_master=1
check_repl_delay=0
hostname=server02
port=3306
[server3]
hostname=server03
port=3306
[server4]
hostname=server04
port=3306
Server05(192.168.200.115) 故障切换过程中的日志文件内容如下:
[root@server05 ~]# tail -f /var/log/masterha/app1/manager.log
Selected server02 as a new master.
server02: OK: Applying all logs succeeded.
server02: OK: Activated master IP address.
server04: This host has the latest relay log events.
server03: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
server04: OK: Applying all logs succeeded. Slave started, replicating from server02.
server03: OK: Applying all logs succeeded. Slave started, replicating from server02.
server02: Resetting slave info succeeded.
Master failover to server02(192.168.200.112:3306) completed successfully.
故障主库修复及 VIP 切回测试
Primary Master(192.168.200.111):
[root@server01 ~]# systemctl start mariadb
[root@server01 ~]# netstat -lnpt | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
6131/mysqld
Primary Master(192.168.200.111) 指向新的主库
[root@server01 ~]# mysql -u root -p123456
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.112',
MASTER_USER='repl',
MASTER_PASSWORD='123456';
start slave;
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.112
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 1372
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 1208
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Server05(192.168.200.115) 修改监控配置文件添加 server1 配置:
[root@server05 ~]# vim /etc/masterha/app1.cnf
[server01]
hostname=server01
port=3306
Server05(192.168.200.115) 检查集群状态:
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
-----------------------------------忽略部分信息-----------------------------------
Thu Aug 31 22:20:30 2017 - [info] Alive Servers:
Thu Aug 31 22:20:30 2017 - [info] server01(192.168.200.111:3306)
Thu Aug 31 22:20:30 2017 - [info] server02(192.168.200.112:3306)
Thu Aug 31 22:20:30 2017 - [info] server03(192.168.200.113:3306)
Thu Aug 31 22:20:30 2017 - [info] server04(192.168.200.114:3306)
-----------------------------------忽略部分信息-----------------------------------
server02 (current master)
+--server01
+--server03
+--server04
-----------------------------------忽略部分信息-----------------------------------
MySQL Replication Health is OK.
Server05(192.168.200.115) 开启监控
[root@server05 ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf QQ:981343876 34 / 50 Tel:13651028933
--remove_dead_master_conf --ignore_last_failover<
/dev/null >/var/log/masterha/app1/manager.log 2>&1
&[1] 68199
Secondary Master(192.168.200.112) 关闭现有主库 mysql
[root@server02 ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.112/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.100/24 brd 192.168.200.255 scope global secondary ens32:1
[root@server02 ~]# systemctl stop mariadb
[root@server02 ~]# netstat -lnpt | grep :3306
Primary Master(192.168.200.111)
[root@server01 ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.100/24 brd 192.168.200.255 scope global secondary ens32:1
slave1(192.168.200.113)状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 923
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
slave2(192.168.200.114)状态:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 923
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Server05(192.168.200.115) 配置文件变化(已经移除故障机 server2 配置):
[root@server05 ~]# cat /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=repl
user=root
[server1]
hostname=server01
port=3306
[server3]
hostname=server03
port=3306
[server4]
hostname=server04
port=3306
Server05(192.168.200.115) 监控日志:
-----------------------------------忽略部分信息-----------------------------------
Selected server01 as a new master.
server01: OK: Applying all logs succeeded.
server01: OK: Activated master IP address.
server03: This host has the latest relay log events.
server04: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
server04: OK: Applying all logs succeeded. Slave started, replicating from server01.
server03: OK: Applying all logs succeeded. Slave started, replicating from server01.
server01: Resetting slave info succeeded.
Master failover to server01(192.168.200.111:3306) completed successfully.
修复 Secondary Master(192.168.200.112)主机:
[root@server02 ~]# systemctl start mariadb
[root@server02 ~]# netstat -lnpt | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
5982/mysqld
Secondary Master(192.168.200.112) 指向新的主库
[root@server02 ~]# mysql -u root -p123456
stop slave;
CHANGE MASTER TO
MASTER_HOST='192.168.200.111',
MASTER_USER='repl',
MASTER_PASSWORD='123456';
start slave;
show slave status\G
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 923
Relay_Log_File: mariadb-relay-bin.000003
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Server05(192.168.200.115)修改监控配置文件添加 server2 配置:
[root@server05 ~]# vim /etc/masterha/app1.cnf
[server2]
hostname=server02
candidate_master=1
port=3306
check_repl_delay=0
Server05(192.168.200.115)检查集群状态:
[root@server05 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
server01 (current master)
+--server02
+--server03
+--server04
-----------------------------------忽略部分信息-----------------------------------
MySQL Replication Health is OK.
十、配置读请求负载均衡
10.1、安装 LVS 和 Keepalived
192.168.200.116 和 192.168.200.117 主机:
yum -y install ipvsadm kernel-devel openssl-devel keepalived
10.2、master 主机设置
192.168.200.116 lvs master 主机配置:
! Configuration File for keepalived
global_defs {
notification_email {
crushlinux@163.com
}
notification_email_from crushlinux@163.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.200
}
}
virtual_server 192.168.200.200 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 192.168.200.113 3306 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.200.114 3306 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
systemctl restart keepalived
检查 VIP 地址
[root@localhost ~]# ip add | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
inet 192.168.200.116/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.200/32 scope global ens32
查看负载策略
[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.200:3306 wrr
-> 192.168.200.113:3306 Route 1 0 0
-> 192.168.200.114:3306 Route 1 0 0
10.3、backup 主机设置
192.168.200.40 backup 主机配置:
! Configuration File for keepalived
global_defs {
notification_email {
crushlinux@163.com
}
notification_email_from crushlinux@163.com
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.200
}
}
virtual_server 192.168.200.200 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 192.168.200.113 3306 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.200.114 3306 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
systemctl restart keepalived
10.4、为数据库节点设置 VIP 地址及配置 ARP 相关参数
slave1(192.168.200.113)和 slave2(192.168.200.114)主机配置 realserver 脚本:
[root@server03 ~]# vim realserver.sh
#!/bin/bash
SNS_VIP=192.168.200.200
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
realserver1 检测 VIP 地址
[root@server03 ~]# bash realserver.sh
[root@server03 ~]# ip a | grep lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 192.168.200.200/32 brd 192.168.200.200 scope global lo:0
inet 192.168.200.113/24 brd 192.168.200.255 scope global ens32
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
远程复制到 realserver2 主机上
[root@server03 ~]# scp realserver.sh 192.168.200.114:/root/
realserver2 检测 VIP 地址
[root@server04 ~]# bash realserver.sh
[root@server04 ~]# ip a |grep lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 192.168.200.200/32 brd 192.168.200.200 scope global lo:0
inet 192.168.200.114/24 brd 192.168.200.255 scope global ens32
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
10.5、数据库读请求测试
Server05(192.168.200.115)担任 client 角色测试,连接 vip
[root@server05 ~]# mysql -uroot -p123456 -h192.168.200.200 -P3306
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 297
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
断开后在连接测试。。。。
[root@server05 ~]# mysql -uroot -p123456 -h192.168.200.200 -P3306
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 298
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
查看请求分流
[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.200:3306 wrr
-> 192.168.200.113:3306 Route 1 1 0
-> 192.168.200.114:3306 Route 1 0 1
[root@localhost ~]# ipvsadm -Lnc
IPVS connection entries
pro expire state source virtual destination
TCP 01:48 FIN_WAIT 192.168.200.115:35462 192.168.200.200:3306
192.168.200.114:3306
TCP 14:50 ESTABLISHED 192.168.200.115:35464 192.168.200.200:3306 192.168.200.113:3306
10.6、数据库连接故障处理
[root@localhost ~]# mysql -u root -p123456 -h192.168.200.113 -P3306
ERROR 1129 (HY000): Host '192.168.200.113' is blocked because of many connection errors;
unblock with 'mysqladmin flush-hosts'
[root@server03 ~]# mysqladmin -uroot -p123456 flush-hosts
附录
手动切换 MHA 脚本
[root@server05 ~]# vim /usr/local/bin/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
);
my $vip = '192.168.200.100/24'; #VIP
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens32:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens32:$key down";
my $orig_master_ssh_port = 22;
my $new_master_ssh_port = 22;
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,
'orig_master_ssh_port=i' => \$orig_master_ssh_port,
'new_master_ssh_port=i' => \$new_master_ssh_port,
);
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
eval {
`ssh -p$orig_master_ssh_port $orig_master_ssh_user\@$orig_master_host \"
$ssh_stop_vip \"`;
};
if ($@) {
warn $@;
}
$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 -p$new_master_ssh_port $new_master_ssh_user\@$new_master_host \"
$ssh_start_vip \"`;
$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\n"; die;
}
[root@server05 ~]# chmod +x /usr/local/bin/master_ip_online_change
编辑脚本 send_report
[root@server05 ~]# vim /usr/local/bin/send_report
#!/usr/bin/perl
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.163.com';
my $mail_from='xxxx';
my $mail_user='xxxxx';
my $mail_pass='xxxxx';
my $mail_to=['xxxx','xxxx'];
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;
[root@server05 ~]# chmod +x /usr/local/bin/send_report