Ubuntu22 mysql8 MHA高可用
mha-manager地址:https://github.com/yoshinorim/mha4mysql-manager
mha-node地址:https://github.com/yoshinorim/mha4mysql-node
mha有mha-manager和mha-node 2个组件
由于主节点有可能会被切换成从节点,而每一个从节点也有可能会被切换成主节点
所以 MHA 对 Mysql 复制环境有特殊要求。
(1)各节点 都要开启 二进制日志 及 中继日志,(master 也要开启中继日志);
(2)当前各从节 点必须 显式 注明其为 read_only=ON;
(3)关闭各从节点的 relay_log_purge=OFF 中继日志 自动清理功能
(4)各个节点要开启免密互信
各个mysql提前主从,并创建好账号是root 密码是Sykj@2024的用户
一、mha-node
1、下载mha-manager的deb包
https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
2、安装
apt install -y https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58-0.el7.centos.noarch.rpm
二、mha-manager
1、下载mha-manager的deb包
wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
2、安装
apt install -y ./mha4mysql-manager_0.58-0_all.deb
3、创建配置文件。
mkdir /etc/mha
vim /etc/mha/app1.cnf [server default] user=root password=Sykj@2024 ssh_user=root #master_binlog_dir=/log-bin ping_interval=3 repl_user=root repl_password=Sykj@2024 manager_log=/var/log/mha/app1/manager.log master_ip_failover_script=/etc/mha/master_ip_failover [server1] hostname=192.168.31.30 candidate_master=1 #候选master check_repl_delay=0 port=3306 [server2] hostname=192.168.31.31 candidate_master=1 #候选master check_repl_delay=0 port=3306 [server3] hostname=192.168.31.31 port=3307 no_master=1
mkdir -p /var/log/mha/app1/
4、编辑故障ip切换脚本
vim /etc/mha/master_ip_failover #!/usr/bin/env perl # Copyright (C) 2011 DeNA Co.,Ltd. # 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.31.39/24'; my $key = '8'; my $ssh_start_vip = "/usr/sbin/ifconfig eth0:$key $vip"; my $ssh_stop_vip = "/usr/sbin/ifconfig eth0:$key down"; 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 { 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、测试ssh连接
masterha_check_ssh --conf=/etc/mha/app1.cnf
6、测试主从是否健康
masterha_check_repl --conf=/etc/mha/app1.cnf
7、开启集群
masterha_manager --conf=/etc/mha/app1.cnf &
三、验证故障IP切换
1、在主服务器添加vip
ifconfig eth0:8 192.168.31.39/24
2、关闭主服务器查看从服务器有没有ip切换过来。
## MHA的监控进程,在执行一次切换后,就会结束退出,并留下一个禁止文件 /var/tmp/app1.failover.complete
## 该文件存在的话,不会执行下一次切换
关闭的主库重新加入集群要以从的身份加入