1、集群信息
[root@es3 local]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.56.14 es1 192.168.56.15 es2 192.168.56.16 es3
2、MySQL配置:
[root@es3 local]# grep -v ^# /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid server_id = 3 log-bin=mysqlbin binlog_format = row log_slave_updates = 1 enforce_gtid_consistency = ON gtid_mode = ON slave-parallel-type=LOGICAL_CLOCK slave-parallel-workers=16 master_info_repository=TABLE relay_log_info_repository=TABLE relay_log_recovery=ON rpl_semi_sync_master_enabled = 1 rpl_semi_sync_master_timeout = 1000 rpl_semi_sync_slave_enabled = 1 [root@es3 local]#
3、app1.cnf信息
[root@es3 ~]# cat app1.cnf [server default] master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script= /usr/local/bin/master_ip_online_change report_script=/usr/local/bin/send_report shutdown_script=/usr/local/bin/stop_report user=repl password=123456 ssh_user=root manager_workdir=/data/manager remote_workdir=/tmp repl_user=repl repl_password=123456 secondary_check_script=masterha_secondary_check -s 192.168.56.16 -s 192.168.56.15 [server1] hostname=es1 port=3306 [server2] hostname=es2 port=3306 [server3] hostname=es3 port=3306 [root@es3 ~]#
4、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.56.191/24'; # Virtual IP my $key = "1"; my $ssh_start_vip = "/sbin/ifconfig enp0s8:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig enp0s8:$key down"; $ssh_user = "root"; GetOptions( 'command=s' => \$command, 'ssh_user=s' => \$ssh_user, 'orig_master_host=s' => \$orig_master_host, 'orig_master_ip=s' => \$orig_master_ip, 'orig_master_port=i' => \$orig_master_port, 'new_master_host=s' => \$new_master_host, 'new_master_ip=s' => \$new_master_ip, 'new_master_port=i' => \$new_master_port, ); exit &main(); sub main { print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host \n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host \n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
5、master_ip_online_change脚本
#!/usr/bin/env perl use strict; use warnings FATAL =>'all'; use Getopt::Long; my $vip = '192.168.56.191/24'; # Virtual IP my $key = "1"; my $ssh_start_vip = "/sbin/ifconfig enp0s8:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig enp0s8:$key down"; my $exit_code = 0; 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 main { #print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; if ( $command eq "stop" || $command eq "stopssh" ) { # $orig_master_host, $orig_master_ip, $orig_master_port are passed. # If you manage master ip address at global catalog database, # invalidate orig_master_ip here. my $exit_code = 1; eval { print "\n\n\n***************************************************************\n"; print "Disabling the VIP - $vip on old master: $orig_master_host\n"; print "***************************************************************\n\n\n\n"; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@\n"; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { # all arguments are passed. # If you manage master ip address at global catalog database, # activate new_master_ip here. # You can also grant write access (create user, set read_only=0, etc) here. my $exit_code = 10; eval { print "\n\n\n***************************************************************\n"; print "Enabling the VIP - $vip on new master: $new_master_host \n"; print "***************************************************************\n\n\n\n"; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK \n"; `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_start_vip \"`; exit 0; } else { &usage(); exit 1; } } # A simple system call that enable the VIP on the new master sub start_vip() { `ssh $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`; } # A simple system call that disable the VIP on the old_master sub stop_vip() { `ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n"; }
6、report_script脚本
#!/bin/bash echo `hostname` `hostname -i` >> mail.txt echo `date` >mail.txt echo "the mha has been switched" >>mail.txt mail -s "the mha has been switched on `hostname -i` " xxxxxxx@163.com <mail.txt
7、stop_report脚本
#!/bin/bash echo `hostname` `hostname -i` >> mail.txt echo `date` >mail.txt echo "the mha has been stoped" >>mail.txt mail -s "the mha has been stoped on `hostname -i` " xxxxxxx@163.com <mail.txt
8、遇到问题
[root@es3 bin]# masterha_check_repl --conf=/root/app1.cnf Tue Aug 20 10:45:29 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Aug 20 10:45:29 2019 - [info] Reading application default configuration from /root/app1.cnf.. Tue Aug 20 10:45:29 2019 - [info] Reading server configuration from /root/app1.cnf.. Tue Aug 20 10:45:29 2019 - [info] MHA::MasterMonitor version 0.58. Tue Aug 20 10:45:30 2019 - [info] GTID failover mode = 1 Tue Aug 20 10:45:30 2019 - [info] Dead Servers: Tue Aug 20 10:45:30 2019 - [info] Alive Servers: Tue Aug 20 10:45:30 2019 - [info] es1(192.168.56.14:3306) Tue Aug 20 10:45:30 2019 - [info] es2(192.168.56.15:3306) Tue Aug 20 10:45:30 2019 - [info] es3(192.168.56.16:3306) Tue Aug 20 10:45:30 2019 - [info] Alive Slaves: Tue Aug 20 10:45:30 2019 - [info] es2(192.168.56.15:3306) Version=5.7.24-log (oldest major version between slaves) log-bin:enabled Tue Aug 20 10:45:30 2019 - [info] GTID ON Tue Aug 20 10:45:30 2019 - [info] Replicating from 192.168.56.14(192.168.56.14:3306) Tue Aug 20 10:45:30 2019 - [info] es3(192.168.56.16:3306) Version=5.7.24-log (oldest major version between slaves) log-bin:enabled Tue Aug 20 10:45:30 2019 - [info] GTID ON Tue Aug 20 10:45:30 2019 - [info] Replicating from es1(192.168.56.14:3306) Tue Aug 20 10:45:30 2019 - [info] Current Alive Master: es1(192.168.56.14:3306) Tue Aug 20 10:45:30 2019 - [info] Checking slave configurations.. Tue Aug 20 10:45:30 2019 - [info] read_only=1 is not set on slave es2(192.168.56.15:3306). Tue Aug 20 10:45:30 2019 - [info] read_only=1 is not set on slave es3(192.168.56.16:3306). Tue Aug 20 10:45:30 2019 - [info] Checking replication filtering settings.. Tue Aug 20 10:45:30 2019 - [info] binlog_do_db= , binlog_ignore_db= Tue Aug 20 10:45:30 2019 - [info] Replication filtering check ok. Tue Aug 20 10:45:30 2019 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking. Tue Aug 20 10:45:30 2019 - [info] Checking SSH publickey authentication settings on the current master.. Tue Aug 20 10:45:31 2019 - [info] HealthCheck: SSH to es1 is reachable. Tue Aug 20 10:45:31 2019 - [info] es1(192.168.56.14:3306) (current master) +--es2(192.168.56.15:3306) +--es3(192.168.56.16:3306) Tue Aug 20 10:45:31 2019 - [info] Checking replication health on es2.. Tue Aug 20 10:45:31 2019 - [info] ok. Tue Aug 20 10:45:31 2019 - [info] Checking replication health on es3.. Tue Aug 20 10:45:31 2019 - [info] ok. Tue Aug 20 10:45:31 2019 - [info] Checking master_ip_failover_script status: Tue Aug 20 10:45:31 2019 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=es1 --orig_master_ip=192.168.56.14 --orig_master_port=3306 /usr/local/bin/master_ip_failover:行3: use: 未找到命令 /usr/local/bin/master_ip_failover:行4: use: 未找到命令 /usr/local/bin/master_ip_failover:行6: use: 未找到命令 /usr/local/bin/master_ip_failover:行8: 未预期的符号 `newline' 附近有语法错误 /usr/local/bin/master_ip_failover:行8: `my (' Tue Aug 20 10:45:31 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln229] Failed to get master_ip_failover_script status with return code 2:0. Tue Aug 20 10:45:31 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. at /usr/bin/masterha_check_repl line 48. Tue Aug 20 10:45:31 2019 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers. Tue Aug 20 10:45:31 2019 - [info] Got exit code 1 (Not master dead). MySQL Replication Health is NOT OK! [root@es3 bin]#
原因:文件多了一个空行,这个错误我犯了好多次,哎……
[root@es3 bin]# masterha_check_repl --conf=/root/app1.cnf Tue Aug 20 10:49:24 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Aug 20 10:49:24 2019 - [info] Reading application default configuration from /root/app1.cnf.. Tue Aug 20 10:49:24 2019 - [info] Reading server configuration from /root/app1.cnf.. Tue Aug 20 10:49:24 2019 - [info] MHA::MasterMonitor version 0.58. Tue Aug 20 10:49:25 2019 - [info] GTID failover mode = 1 Tue Aug 20 10:49:25 2019 - [info] Dead Servers: Tue Aug 20 10:49:25 2019 - [info] Alive Servers: Tue Aug 20 10:49:25 2019 - [info] es1(192.168.56.14:3306) Tue Aug 20 10:49:25 2019 - [info] es2(192.168.56.15:3306) Tue Aug 20 10:49:25 2019 - [info] es3(192.168.56.16:3306) Tue Aug 20 10:49:25 2019 - [info] Alive Slaves: Tue Aug 20 10:49:25 2019 - [info] es2(192.168.56.15:3306) Version=5.7.24-log (oldest major version between slaves) log-bin:enabled Tue Aug 20 10:49:25 2019 - [info] GTID ON Tue Aug 20 10:49:25 2019 - [info] Replicating from 192.168.56.14(192.168.56.14:3306) Tue Aug 20 10:49:25 2019 - [info] es3(192.168.56.16:3306) Version=5.7.24-log (oldest major version between slaves) log-bin:enabled Tue Aug 20 10:49:25 2019 - [info] GTID ON Tue Aug 20 10:49:25 2019 - [info] Replicating from es1(192.168.56.14:3306) Tue Aug 20 10:49:25 2019 - [info] Current Alive Master: es1(192.168.56.14:3306) Tue Aug 20 10:49:25 2019 - [info] Checking slave configurations.. Tue Aug 20 10:49:25 2019 - [info] read_only=1 is not set on slave es2(192.168.56.15:3306). Tue Aug 20 10:49:25 2019 - [info] read_only=1 is not set on slave es3(192.168.56.16:3306). Tue Aug 20 10:49:25 2019 - [info] Checking replication filtering settings.. Tue Aug 20 10:49:25 2019 - [info] binlog_do_db= , binlog_ignore_db= Tue Aug 20 10:49:25 2019 - [info] Replication filtering check ok. Tue Aug 20 10:49:25 2019 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking. Tue Aug 20 10:49:25 2019 - [info] Checking SSH publickey authentication settings on the current master.. Tue Aug 20 10:49:26 2019 - [info] HealthCheck: SSH to es1 is reachable. Tue Aug 20 10:49:26 2019 - [info] es1(192.168.56.14:3306) (current master) +--es2(192.168.56.15:3306) +--es3(192.168.56.16:3306) Tue Aug 20 10:49:26 2019 - [info] Checking replication health on es2.. Tue Aug 20 10:49:26 2019 - [info] ok. Tue Aug 20 10:49:26 2019 - [info] Checking replication health on es3.. Tue Aug 20 10:49:26 2019 - [info] ok. Tue Aug 20 10:49:26 2019 - [info] Checking master_ip_failover_script status: Tue Aug 20 10:49:26 2019 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=es1 --orig_master_ip=192.168.56.14 --orig_master_port=3306 IN SCRIPT TEST====/sbin/ifconfig enp0s8:1 down==/sbin/ifconfig enp0s8:1 192.168.56.191/24=== Checking the Status of the script.. OK Tue Aug 20 10:49:26 2019 - [info] OK. Tue Aug 20 10:49:26 2019 - [info] Checking shutdown script status: Tue Aug 20 10:49:26 2019 - [info] /usr/local/bin/stop_report --command=status --ssh_user=root --host=es1 --ip=192.168.56.14 Tue Aug 20 10:49:26 2019 - [info] OK. Tue Aug 20 10:49:26 2019 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK. [root@es3 bin]#