mysql> grant replication slave,replication client on *.* to 'repl'@'192.168.86.%' identified by '123456';
$ yum install perl-DBD-MySQL -y
$ rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]
$ ll /usr/bin/ | grep logs
-rwxr-xr-x. 1 root root 15977 Dec 1 2012 apply_diff_relay_logs
-rwxr-xr-x. 1 root root 4807 Dec 1 2012 filter_mysqlbinlog
-rwxr-xr-x. 1 root root 7401 Dec 1 2012 purge_relay_logs
-rwxr-xr-x. 1 root root 7263 Dec 1 2012 save_binary_logs
$ ls
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MailTools-2.04-4.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Lite-HTML-1.23-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
perl-Params-Validate-0.92-3.el6.x86_64.rpm
perl-TimeDate-1.16-13.el6.noarch.rpm
$ yum localinstall perl-* -y
$ rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-manager ########################################### [100%]
$ ls /usr/bin/ | grep "masterha_*"
masterha_check_repl
masterha_check_ssh
masterha_check_status
masterha_conf_host
masterha_manager
masterha_master_monitor
masterha_master_switch
masterha_secondary_check
masterha_stop
$ vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.86.148 mha-master
192.168.86.149 mha-manager
192.168.86.150 mha-bak
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:
5a:26:2d:d2:63:48:52:d1:0f:54:b0:fc:8c:6e:93:9e root@mha-master
The key's randomart image is:
+--[ RSA 2048]----+
| o+oo. |
| . .o. |
| . . oo |
| o o =. |
| o B S |
| + O |
| * |
| o o |
| E |
+-----------------+
$ cd /root/.ssh/
$ cat id_rsa.pub >> authorized_keys
$ ssh-copy-id -i id_rsa.pub [email protected]
The authenticity of host '192.168.86.149 (192.168.84.149)' can't be established.
RSA key fingerprint is 22:00:37:a2:be:c4:98:12:72:57:d4:74:bb:07:02:50.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.86.149' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
ssh-copy-id -i id_rsa.pub [email protected]
The authenticity of host '192.168.86.150 (192.168.86.150)' can't be established.
RSA key fingerprint is 22:00:37:a2:be:c4:98:12:72:57:d4:74:bb:07:02:50.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.86.150' (RSA) to the list of known hosts.
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
#!/bin/bash
node=( 192.168.86.148 192.168.86.149 192.168.86.150) # hostname for each node
username=root # username to be interconnected
homename=$username # home dir, i.e. home/zhangyang
if [ "$username" = "root" ];
then
homename=root
else
homename=home/$username
fi
for((i=0; i<${#node[*]}; i++))
do
ssh $username@${node[i]} 'ssh-keygen -t rsa; chmod 755 ~/.ssh'
done
#cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
echo "batch authorized_keys created..."
echo "start scp..."
#scp node003:/$homename/.ssh/authorized_keys /$homename/.ssh/node003.key
for((i=0; i<${#node[*]}; i++))
do
scp ${node[i]}:/$homename/.ssh/id_rsa.pub /$homename/.ssh/${node[i]}.key
echo "scp from ${node[i]} finished..."
done
echo "append key to authorized_keys..."
for((i=0; i<${#node[*]}; i++))
do
cat /$homename/.ssh/${node[i]}.key >> /$homename/.ssh/authorized_keys
echo "append ${node[i]}.key finished..."
done
echo "append all key finished..."
loop=${#node[*]}
let subloop=loop-1
echo "starting scp complete authorized_keys to ${node[0]}~${node[subloop]}"
for((i=0; i<${#node[*]}; i++))
do
scp /$homename/.ssh/authorized_keys ${node[i]}:/$homename/.ssh/authorized_keys
echo "scp to ${node[i]} finished..."
done
echo "scp all nodes finished..."
# delete intermediate files
rm -rf /$homename/.ssh/*.key
echo "all configuration finished..."
$ ssh mha-manager
$ ssh mha-bak
$ ssh mha-master
$ ssh mha-manager
$ ssh mha-master
$ ssh mha-manager
mysql> grant all privileges on *.* to 'mha_monitor'@'192.168.86.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.21 sec)
$ mysql -umha_monitor -p123456 -h192.168.86.148
$ mkdir -p /etc/masterha
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/data
remote_workdir=/tmp
ping_interval=1
repl_user=repl
repl_password=123456
repl_user=repl
ssh_user=root
user=mha_monitor
password=123456
[server1]
hostname=192.168.86.148
port=3306
[server2]
hostname=192.168.86.149
port=3306
[server3]
candidate_master=1
check_repl_delay=0
hostname=192.168.86.150
port=3306
mysql> set global relay_log_purge=0;(在 2 个 slave 上执行)
Query OK, 0 rows affected (0.00 sec)
vim /root/purge_relay_log.sh
#!/bin/bash
user=root
passwd=123456
port=3306
log_dir='/var/log/masterha/app1/log'
work_dir='/var/log/masterha/app1'
purge='/usr/local/bin/purge_relay_logs'
if [ ! -d $log_dir ]
then
mkdir $log_dir -p
fi
$purge --user=$user --password=$passwd --disable_relay_log_purge --port=$port
--workdir=$work_dir >> $log_dir/purge_relay_logs.log 2>&1
$ crontab -l
0 4 * * * /bin/bash /root/purge_relay_log.sh
$ ifconfig eth0:1 192.168.86.155/24
$ vim /usr/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.86.155/24'; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $exit_code = 0;
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
#print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {
print "\n\n\n***************************************************************\n";
print "Disabling the VIP - $vip on old master: $orig_master_host\n";
print "***************************************************************\n\n\n\n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
print "\n\n\n***************************************************************\n";
print "Enabling the VIP - $vip on new master: $new_master_host \n";
print "***************************************************************\n\n\n\n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`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=po
rt –new_master_host=host –new_master_ip=ip –new_master_port=port\n";
}
#/bin/bash
source /root/.bash_profile
vip=`echo '192.168.86.155/24'` # Virtual IP
key=`echo '1'`
command=`echo "$1" | awk -F = '{print $2}'`
orig_master_host=`echo "$2" | awk -F = '{print $2}'`
new_master_host=`echo "$7" | awk -F = '{print $2}'`
orig_master_ssh_user=`echo "${12}" | awk -F = '{print $2}'`
new_master_ssh_user=`echo "${13}" | awk -F = '{print $2}'`
stop_vip=`echo "ssh root@$orig_master_host /sbin/ifconfig eth0:$key down"`
start_vip=`echo "ssh root@$new_master_host /sbin/ifconfig eth0:$key $vip"`
if [ $command = 'stop' ]
then
echo -e "\n\n\n***************************************************************\n"
echo -e "Disabling the VIP - $vip on old master: $orig_master_host\n"
$stop_vip
if [ $? -eq 0 ]
then
echo "Disabled the VIP successfully"
else
echo "Disabled the VIP failed"
fi
echo -e "***************************************************************\n\n\n\n"
fi
if [ $command = 'start' -o $command = 'status' ]
then
echo -e "\n\n\n***************************************************************\n"
echo -e "Enabling the VIP - $vip on new master: $new_master_host \n"
$start_vip
if [ $? -eq 0 ]
then
echo "Enabled the VIP successfully"
else
echo "Enabled the VIP failed"
fi
echo -e "***************************************************************\n\n\n\n"
fi
$ chmod +x /usr/bin/master_ip_failover /usr/bin/master_ip_online_change
$ masterha_check_ssh --conf=/etc/masterha/app1.cnf
Fri Sep 1 01:31:11 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep 1 01:31:11 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Sep 1 01:31:11 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Sep 1 01:31:11 2017 - [info] Starting SSH connection tests..
Fri Sep 1 01:31:12 2017 - [debug]
Fri Sep 1 01:31:11 2017 - [debug] Connecting via SSH from [email protected](192.168.86.148:22) to [email protected](192.168.86.149:22)..
Fri Sep 1 01:31:11 2017 - [debug] ok.
Fri Sep 1 01:31:11 2017 - [debug] Connecting via SSH from [email protected](192.168.86.148:22) to [email protected](192.168.86.150:22)..
Fri Sep 1 01:31:11 2017 - [debug] ok.
Fri Sep 1 01:31:12 2017 - [debug]
Fri Sep 1 01:31:11 2017 - [debug] Connecting via SSH from [email protected](192.168.86.149:22) to [email protected](192.168.86.148:22)..
Fri Sep 1 01:31:11 2017 - [debug] ok.
Fri Sep 1 01:31:11 2017 - [debug] Connecting via SSH from [email protected](192.168.86.149:22) to [email protected](192.168.86.150:22)..
Fri Sep 1 01:31:12 2017 - [debug] ok.
Fri Sep 1 01:31:12 2017 - [debug]
Fri Sep 1 01:31:12 2017 - [debug] Connecting via SSH from [email protected](192.168.86.150:22) to [email protected](192.168.86.148:22)..
Fri Sep 1 01:31:12 2017 - [debug] ok.
Fri Sep 1 01:31:12 2017 - [debug] Connecting via SSH from [email protected](192.168.86.150:22) to [email protected](192.168.86.149:22)..
Fri Sep 1 01:31:12 2017 - [debug] ok.
Fri Sep 1 01:31:12 2017 - [info] All SSH connection tests passed successfully.
$ masterha_check_repl --conf=/etc/masterha/app1.cnf
Fri Sep 1 01:36:29 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Sep 1 01:36:29 2017 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Sep 1 01:36:29 2017 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Sep 1 01:36:29 2017 - [info] MHA::MasterMonitor version 0.56.
Fri Sep 1 01:36:29 2017 - [info] GTID failover mode = 0
Fri Sep 1 01:36:29 2017 - [info] Dead Servers:
Fri Sep 1 01:36:29 2017 - [info] Alive Servers:
Fri Sep 1 01:36:29 2017 - [info] 192.168.86.148(192.168.86.148:3306)
Fri Sep 1 01:36:29 2017 - [info] 192.168.86.149(192.168.86.149:3306)
Fri Sep 1 01:36:29 2017 - [info] 192.168.86.150(192.168.86.150:3306)
Fri Sep 1 01:36:29 2017 - [info] Alive Slaves:
Fri Sep 1 01:36:29 2017 - [info] 192.168.86.149(192.168.86.149:3306) Version=5.6.20-log (oldest major version between slaves) log-bin:enabled
Fri Sep 1 01:36:29 2017 - [info] Replicating from 192.168.86.148(192.168.86.148:3306)
Fri Sep 1 01:36:29 2017 - [info] 192.168.86.150(192.168.86.150:3306) Version=5.6.20-log (oldest major version between slaves) log-bin:enabled
Fri Sep 1 01:36:29 2017 - [info] Replicating from 192.168.86.148(192.168.86.148:3306)
Fri Sep 1 01:36:29 2017 - [info] Primary candidate for the new Master (candidate_master is set)
Fri Sep 1 01:36:29 2017 - [info] Current Alive Master: 192.168.86.148(192.168.86.148:3306)
Fri Sep 1 01:36:29 2017 - [info] Checking slave configurations..
Fri Sep 1 01:36:29 2017 - [info] read_only=1 is not set on slave 192.168.86.150(192.168.86.150:3306).
Fri Sep 1 01:36:29 2017 - [info] Checking replication filtering settings..
Fri Sep 1 01:36:29 2017 - [info] binlog_do_db= , binlog_ignore_db=
Fri Sep 1 01:36:29 2017 - [info] Replication filtering check ok.
Fri Sep 1 01:36:29 2017 - [info] GTID (with auto-pos) is not supported
Fri Sep 1 01:36:29 2017 - [info] Starting SSH connection tests..
Fri Sep 1 01:36:31 2017 - [info] All SSH connection tests passed successfully.
Fri Sep 1 01:36:31 2017 - [info] Checking MHA Node version..
Fri Sep 1 01:36:31 2017 - [info] Version check ok.
Fri Sep 1 01:36:31 2017 - [info] Checking SSH publickey authentication settings on the current master..
Fri Sep 1 01:36:31 2017 - [info] HealthCheck: SSH to 192.168.86.148 is reachable.
Fri Sep 1 01:36:32 2017 - [info] Master MHA Node version is 0.56.
Fri Sep 1 01:36:32 2017 - [info] Checking recovery script configurations on 192.168.86.148(192.168.86.148:3306)..
Fri Sep 1 01:36:32 2017 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/ --output_file=/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-server.000007
Fri Sep 1 01:36:32 2017 - [info] Connecting to [email protected](192.168.86.148:22)..
Creating /tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/, up to mysql-server.000007
Fri Sep 1 01:36:32 2017 - [info] Binlog setting check done.
Fri Sep 1 01:36:32 2017 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Sep 1 01:36:32 2017 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='monitor' --slave_host=192.168.86.149 --slave_ip=192.168.86.149 --slave_port=3306 --workdir=/tmp --target_version=5.6.20-log --manager_version=0.56 --relay_log_info=/data/relay-log.info --relay_dir=/data/ --slave_pass=xxx
Fri Sep 1 01:36:32 2017 - [info] Connecting to [email protected](192.168.86.149:22)..
Checking slave recovery environment settings..
Opening /data/relay-log.info ... ok.
Relay log found at /data, up to mysqld-relay-bin.000003
Temporary relay log file is /data/mysqld-relay-bin.000003
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.
Fri Sep 1 01:36:32 2017 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='monitor' --slave_host=192.168.86.150 --slave_ip=192.168.86.150 --slave_port=3306 --workdir=/tmp --target_version=5.6.20-log --manager_version=0.56 --relay_log_info=/data/relay-log.info --relay_dir=/data/ --slave_pass=xxx
Fri Sep 1 01:36:32 2017 - [info] Connecting to [email protected](192.168.86.150:22)..
Checking slave recovery environment settings..
Opening /data/relay-log.info ... ok.
Relay log found at /data, up to mysqld-relay-bin.000003
Temporary relay log file is /data/mysqld-relay-bin.000003
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.
Fri Sep 1 01:36:33 2017 - [info] Slaves settings check done.
Fri Sep 1 01:36:33 2017 - [info]
192.168.86.148(192.168.86.148:3306) (current master)
+--192.168.86.149(192.168.86.149:3306)
+--192.168.86.150(192.168.86.150:3306)
Fri Sep 1 01:36:33 2017 - [info] Checking replication health on 192.168.86.149..
Fri Sep 1 01:36:33 2017 - [info] ok.
Fri Sep 1 01:36:33 2017 - [info] Checking replication health on 192.168.86.150..
Fri Sep 1 01:36:33 2017 - [info] ok.
Fri Sep 1 01:36:33 2017 - [info] Checking master_ip_failover_script status:
Fri Sep 1 01:36:33 2017 - [info] /usr/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.86.148 --orig_master_ip=192.168.86.148 --orig_master_port=3306
Checking the Status of the script.. OK
Fri Sep 1 01:36:33 2017 - [info] OK.
Fri Sep 1 01:36:33 2017 - [warning] shutdown_script is not defined.
Fri Sep 1 01:36:33 2017 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
$ masterha_check_status --conf=/etc/masterha/app1.cnf
app1 is stopped(2:NOT_RUNNING).
$ masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:15469) is running(0:PING_OK), master:192.168.86.148
$ masterha_check_status --conf=/etc/masterha/app1.cnf
app1 monitoring program is now on initialization phase(10:INITIALIZING_MONITOR). Wait for a while and try checking again.
$ nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master --ignore_last_failover /masterha/app1/manager.log 2>&1 &
$ masterha_stop --conf=/etc/masterha/app1.cnf
$ masterha_conf_host --command=add --conf=/etc/masterha/app1.cnf --hostname=192.168.68.102 --block=server1
$ masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.86.151 --orig_master_is_new_slave --running_updates_limit=10000;
$ cat /var/log/masterha/app1/manager.log |grep -i "All other slaves should start"
Fri Dec 30 18:09:35 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER
TO MASTER_HOST='192.168.86.150', MASTER_PORT=3306, MASTER_LOG_FILE='mha-server.000001', MASTER_LOG_POS=154,
MASTER_USER='repl', MASTER_PASSWORD='xxx';
$ nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master --ignore_last_failover /masterha/app1/manager.log 2>&1 &
$ masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:15469) is running(0:PING_OK), master:192.168.86.148
$ pkill -9 mysqld
$ cat /masterha/app1/manager.log
$ nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master --ignore_last_failover /masterha/app1/manager.log 2>&1 &
$ masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:15469) is running(0:PING_OK), master:192.168.86.150
$ masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.86.148 --dead_master_port=3306 --new_master_host=192.168.86.150 --new_master_port=3306 --ignore_last_failover
$ cat /var/log/masterha/app1/manager.log |grep -i "All other slaves should start"
Fri Dec 30 18:09:35 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER
TO MASTER_HOST='192.168.86.150', MASTER_PORT=3306, MASTER_LOG_FILE='mha-server.000001', MASTER_LOG_POS=154,
MASTER_USER='repl', MASTER_PASSWORD='xxx';
mysql> CHANGE MASTER
TO MASTER_HOST='192.168.86.150', MASTER_PORT=3306, MASTER_LOG_FILE='mha-server.000001', MASTER_LOG_POS=154,
MASTER_USER='repl', MASTER_PASSWORD='123456';
$ masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.86.148 --orig_master_is_new_slave --running_updates_limit=10000;