MHA是MySQL高可用方面的一个相对成熟的解决方案,用于解决数据库故障切换(0-30秒),数据的保存以及所有节点日志的一致性
设备准备:一台manager,一台master和两台slave做主从复制
master:
[root@master ~]# vim /etc/my.cnf
[mysqld]
server-id = 10
log_bin = master-bin
log-slave-update = true
[root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin
[root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin
slave1:
[root@slave1 ~]# vim /etc/my.cnf
server-id = 12
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@slave1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin
[root@slave1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin
slave2:
[root@slave2 opt]# vim /etc/my.cnf
server-id = 13
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-log-bin.index
[root@slave2 opt]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@slave2 opt]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin
grant replication slave on *.* to 'myslave'@'192.168.118.%' identified by '123456';
grant all privileges on *.* to 'mha'@'192.168.118.%' identified by 'manager';
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
mster:
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 | 154 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
slave1:
mysql> change master to master_host='192.168.118.200',master_user='myslave',master_password='123456',master_log_file='masster-bin.000003',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
…………………………
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
…………………………
slave2:
mysql> change master to master_host='192.168.118.200',master_user='myslave',master_password='123456',master_log_file='masster-bin.000003',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
…………………………
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
…………………………
yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
tar zxvf /opt/mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
tar zxvf /opt/mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
##安装后在/usr/local/bin目录下会生成以下几个工具
[root@localhost mha4mysql-manager-0.57]# ls -l /usr/local/bin | grep masterha
-r-xr-xr-x. 1 root root 1995 七月 20 14:21 masterha_check_repl #表示MySQL复制状况
-r-xr-xr-x. 1 root root 1779 七月 20 14:21 masterha_check_ssh #表示MHA的SSH配置状况
-r-xr-xr-x. 1 root root 1865 七月 20 14:21 masterha_check_status
-r-xr-xr-x. 1 root root 3201 七月 20 14:21 masterha_conf_host
-r-xr-xr-x. 1 root root 2517 七月 20 14:21 masterha_manager
-r-xr-xr-x. 1 root root 2165 七月 20 14:21 masterha_master_monitor
-r-xr-xr-x. 1 root root 2373 七月 20 14:21 masterha_master_switch
-r-xr-xr-x. 1 root root 5171 七月 20 14:21 masterha_secondary_check
-r-xr-xr-x. 1 root root 1739 七月 20 14:21 masterha_stop
ssh-keygen -t rsa
ssh-copy-id 192.168.118.200
ssh-copy-id 192.168.118.100
ssh-copy-id 192.168.118.50
ssh-copy-id 192.168.118.55
/sbin/ifconfig ens33:1 192.168.118.100
在MHA节点上复制相关脚本到/usr/local/bin目录
[root@manager ~]# cp -ra /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin
[root@manager ~]# cd /usr/local/bin/scripts/
[root@manager scripts]# ls
master_ip_failover master_ip_online_change power_manager send_report
[root@manager scripts]# 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.118.100'; #浮动IP
my $brdc = '192.168.118.255'; #广播地址
my $ifdev = 'ens33'; #使用的网卡为ens33
my $key = '1'; #国际序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #使用ifoconfig命令将其启动,同时设置浮动地址
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #可以使用ifconfig命令将其down掉(关闭)
my $exit_code = 0; #正常退出(返回状态码)
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
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 \"`;
}
# 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=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
[root@manager scripts]# mkdir /etc/masterha
[root@manager scripts]# cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
[root@manager scripts]# vim /etc/masterha/app1.cnf #删除原本内容编辑
[server default]
#日志文件
manager_log=/var/log/masterha/app1/manager.log
#工作目录
manager_workdir=/var/log/masterha/app1
#二进制文件
master_binlog_dir=/usr/local/mysql
#故障转移切换的工具
master_ip_failover_script=/usr/local/bin/master_ip_failover
#在线切换VIP工具管理
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
#以下是密码账号的管理配置
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123456
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.118.50 -s 192.168.118.55
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.226.129
port=3306
[server2]
candidate_master=1
hostname=192.168.226.131
check_repl_delay=0
port=3306
[server3]
hostname=192.168.226.132
port=3306
[root@manager scripts]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Jul 20 15:26:01 2021 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Tue Jul 20 15:26:01 2021 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Tue Jul 20 15:26:01 2021 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Tue Jul 20 15:26:01 2021 - [info] Starting SSH connection tests..
Tue Jul 20 15:26:03 2021 - [debug]
Tue Jul 20 15:26:01 2021 - [debug] Connecting via SSH from [email protected](192.168.118.200:22) to [email protected](192.168.118.50:22)..
Tue Jul 20 15:26:02 2021 - [debug] ok.
Tue Jul 20 15:26:02 2021 - [debug] Connecting via SSH from [email protected](192.168.118.200:22) to [email protected](192.168.118.55:22)..
Tue Jul 20 15:26:03 2021 - [debug] ok.
Tue Jul 20 15:26:24 2021 - [debug]
Tue Jul 20 15:26:02 2021 - [debug] Connecting via SSH from [email protected](192.168.118.50:22) to [email protected](192.168.118.200:22)..
Tue Jul 20 15:26:12 2021 - [debug] ok.
Tue Jul 20 15:26:12 2021 - [debug] Connecting via SSH from [email protected](192.168.118.50:22) to [email protected](192.168.118.55:22)..
Tue Jul 20 15:26:23 2021 - [debug] ok.
Tue Jul 20 15:26:24 2021 - [debug]
Tue Jul 20 15:26:02 2021 - [debug] Connecting via SSH from [email protected](192.168.118.55:22) to [email protected](192.168.118.200:22)..
Tue Jul 20 15:26:13 2021 - [debug] ok.
Tue Jul 20 15:26:13 2021 - [debug] Connecting via SSH from [email protected](192.168.118.55:22) to [email protected](192.168.118.50:22)..
Tue Jul 20 15:26:24 2021 - [debug] ok.
Tue Jul 20 15:26:24 2021 - [info] All SSH connection tests passed successfully.
[root@manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
…………………………
Tue Jul 20 22:29:37 2021 - [info] Alive Servers:
Tue Jul 20 22:29:37 2021 - [info] 192.168.118.200(192.168.118.200:3306)
Tue Jul 20 22:29:37 2021 - [info] 192.168.118.50(192.168.118.50:3306)
Tue Jul 20 22:29:37 2021 - [info] 192.168.118.55(192.168.118.55:3306)
…………………………
[root@manager scripts]# 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 &
[1] 78897
#查看到当前的master节点是谁
[root@manager bin]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:78897) is running(0:PING_OK), master:192.168.118.200