MHA Manager(管理节点) MHA Node(数据节点)
服务器 CentOS7.4(64 位) MHA-manager/20.0.0.13 管理节点,安装 manager 组件 服务器 CentOS7.4(64 位) Mysql1/20.0.0.27 Master 节点,安装 node 组件 服务器 CentOS7.4(64 位) Mysql2/20.0.0.100 Slave 节点,安装 node 组件 服务器 CentOS7.4(64 位) Mysql3/20.0.0.12 Slave 节点,安装 node 组件
注意 由于操作系统是 CentOS7 版本,所以这里下载 MHA 版本是 0.57 版本
mysql-boost-5.7.20.tar.gz cmake-2.8.6.tar mha4mysql-node-0.57.tar.gz mha4mysql-manager-0.57.tar.gz
1) 安装 MySQL 数据库 2) 配置 MySQL 一主两从 3) 安装 MHA 软件 4) 配置无密码认证 5) 配置 MySQL MHA 高可用 6) 模拟 master 故障切换
传送门
主从复制 (1)修改 Master 的主配置文件/etc/my.cnf 文件
[root@Mysql1 ~]# vi /etc/my.cnf [client] #default-character-set=utf8 [mysqld] server-id = 1 log_bin = master-bin log-slave-updates = trueMysql1、Mysql2、Mysql3 分别做两个软链接
[root@mysql1 ~]#systemctl restart mysqld [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/ [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/配置 MySQL一主两从
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123'; mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager'; mysql> flush privileges; mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 154 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)(2)修改Slave1的主配置文件/etc/my.cnf 文件
[root@Mysql2 ~]# vim /etc/my.cnf [client] #default-character-set=utf8 [mysqld] server-id = 2 log_bin = master-bin relay-log = relay-log-bin relay-log-index = slave-relay-bin.index [root@mysql2 ~]#systemctl restart mysqld [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/ [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/配置 MySQL一主两从
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123'; mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager'; mysql> flush privileges;在 Mysql2 执行同步
mysql> change master to master_host='20.0.0.27',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=154; mysql> start slave; mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes mysql> set global read_only=1;(3)修改Slave2的主配置文件/etc/my.cnf 文件
[root@Mysql3 ~]# vim /etc/my.cnf [client] #default-character-set=utf8 [mysqld] server-id = 3 log_bin = master-bin relay-log = relay-log-bin relay-log-index = slave-relay-bin.index [root@mysql3 ~]#systemctl restart mysqld [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/ [root@Mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/配置 MySQL一主两从
mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123'; mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager'; mysql> flush privileges;在 Mysql3执行同步
mysql> change master to master_host='20.0.0.27',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=154; mysql> start slave; mysql> show slave status\G; Slave_IO_Running: Yes Slave_SQL_Running: Yes mysql> set global read_only=1;注意 (1)所有服务器上都要安装 MHA 依赖的环境,首先需要安装 epel 源。
[root@MHA-manager ~]# yum -y install epel-release --nogpgcheck yum install -y perl-DBD-MySQL \ perl-Config-Tiny \ perl-Log-Dispatch \ perl-Parallel-ForkManager \ perl-ExtUtils-CBuilder \ perl-ExtUtils-MakeMaker \ perl-CPAN(2)MHA 软件包对于每个操作系统版本不一样,这里 CentOS7.4 必须选择 0.57 版本 注意 所有服务器上必须先安装 node 组件,最后在MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件,下面只在 Mysql1 上操作演示安装 node 组件。
[root@Mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz [root@Mysql1 ~]# cd mha4mysql-node-0.57 [root@Mysql1 mha4mysql-node-0.57]# perl Makefile.PL [root@Mysql1 mha4mysql-node-0.57]# make [root@Mysql1 mha4mysql-node-0.57]# make install(3)在 MHA-manager 上安装 manager 组件 注意: 一定要先安装node 组件才能安装manager 组件
[root@MHA-manager ~]# tar zxvf mha4MHA-manager-0.57.tar.gz [root@MHA-manager ~]# cd mha4MHA-manager-0.57 [root@MHA-manager mha4MHA-manager-0.57]# perl Makefile.PL [root@MHA-manager mha4MHA-manager-0.57]# make [root@MHA-manager mha4MHA-manager-0.57]# make install完成manager安装后会在/usr/local/bin下面生成几个工具:
masterha_check_ssh 检查 MHA 的 SSH 配置状况 masterha_check_repl 检查 MySQL 复制状况 masterha_manger 启动 manager的脚本 masterha_check_status 检测当前 MHA 运行状态 masterha_master_monitor 检测 master 是否宕机 masterha_master_switch 控制故障转移(自动或者手动) masterha_conf_host 添加或删除配置的 server 信息 masterha_stop 关闭manager
node 安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由 MHA Manager 的脚本触发,无需人为操作)
save_binary_logs 保存和复制 master 的二进制日志 apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slave filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具) purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)
(4)配置无密码认证
在 manager 上配置到所有数据库节点的无密码认证 [root@MHA-manager ~]# ssh-keygen -t rsa //一路按回车键 [root@MHA-manager ~]# ssh-copy-id 20.0.0.27 [root@MHA-manager ~]# ssh-copy-id 20.0.0.100 [root@MHA-manager ~]# ssh-copy-id 20.0.0.12 在 Mysql1 上配置到数据库节点Mysql2和Mysql3的无密码认证 [root@Mysql1 ~]# ssh-keygen -t rsa [root@Mysql1 ~]# ssh-copy-id 20.0.0.100 [root@Mysql1 ~]# ssh-copy-id 20.0.0.12 在 Mysql2 上配置到数据库节点Mysql1和Mysql3的无密码认证 [root@Mysql2 ~]# ssh-keygen -t rsa [root@Mysql2 ~]# ssh-copy-id 20.0.0.27 [root@Mysql2 ~]# ssh-copy-id 20.0.0.12 在 Mysql3 上配置到数据库节点Mysql1和Mysql2的无密码认证 [root@Mysql3 ~]# ssh-keygen -t rsa [root@Mysql3 ~]# ssh-copy-id 20.0.0.27 [root@Mysql3 ~]# ssh-copy-id 20.0.0.100(5)配置MHA 在 manager 节点上复制相关脚本到/usr/local/bin 目录
[root@MHA-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin复制上述的自动切换时 VIP 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin修改/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.195.200'; my $brdc = '192.168.195.255'; my $ifdev = 'ens33'; my $key = '1'; my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig ens33:$key 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"; }创建 MHA 软件目录并拷贝配置文件
[root@MHA-manager ~]# mkdir /etc/masterha [root@MHA-manager ~]# cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/ [root@MHA-manager ~]# 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/data master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script=/usr/local/bin/master_ip_online_change password=manager ping_interval=1 remote_workdir=/tmp repl_password=123 repl_user=myslave secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.100 -s 20.0.0.12 shutdown_script="" ssh_user=root user=mha [server1] hostname=20.0.0.27 port=3306 [server2] candidate_master=1 check_repl_delay=0 hostname=20.0.0.100 port=3306 [server3] hostname=20.0.0.12 port=3306测试ssh无密码认证
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf健康检查
[root@MHA-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf注意 第一次配置需要去master上手动开启虚拟IP
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.200/24启动 MHA
[root@MHA-manager ~]# 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 &查看 MHA 状态,可以看到当前的 master 是 Mysql1 节点
[root@MHA-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf查看 MHA 日志
[root@MHA-manager ~]# cat /var/log/masterha/app1/manager.log