当我们做MySQL的读写分离与主从复制时,一台主服务器下面连着多台从服务器,如果当主服务器挂掉之后,就会出现单点故障的问题,所以我们需要用到 MHA 高可用的故障切换。
主机 | IP地址 | 操作系统 |
---|---|---|
mysql1(主) | 20.0.0.23 | centos-7.6-x86_64 |
mysql2(从/主备) | 20.0.0.24 | centos-7.6-x86_64 |
mysql3(从) | 20.0.0.25 | centos-7.6-x86_64 |
Manger | 20.0.0.22 | centos-7.6-x86_64 |
因为操作系统是centos7,所以要下载MHA 0.57版本,MHA对于版本要求很高!
三台数据库服务器需要先安装MySQL数据库,如果不会安装可以看一下我之前的博客
博客地址:安装数据库博客地址
mysql 1:
[root@mysql1 ~]# vi /etc/hosts ## 添加下面三句话
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
mysql 2:
[root@mysql2 ~]# vi /etc/hosts ## 添加下面三句话
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
mysql 3:
[root@mysql3 ~]# vi /etc/hosts ## 添加下面三句话
20.0.0.23 mysql1
20.0.0.24 mysql2
20.0.0.25 mysql3
==》》 wq
[root@mysql2 ~]# ping mysql1
PING mysql1 (20.0.0.23) 56(84) bytes of data.
64 bytes from mysql1 (20.0.0.23): icmp_seq=1 ttl=64 time=0.293 ms
64 bytes from mysql1 (20.0.0.23): icmp_seq=2 ttl=64 time=0.383 ms
[root@mysql1 cmake-2.8.6]# ping mysql2
PING mysql2 (20.0.0.24) 56(84) bytes of data.
64 bytes from mysql2 (20.0.0.24): icmp_seq=1 ttl=64 time=1.17 ms
64 bytes from mysql2 (20.0.0.24): icmp_seq=2 ttl=64 time=0.392 ms
[root@mysql1 cmake-2.8.6]# ping mysql3
PING mysql3 (20.0.0.25) 56(84) bytes of data.
64 bytes from mysql3 (20.0.0.25): icmp_seq=1 ttl=64 time=0.317 ms
64 bytes from mysql3 (20.0.0.25): icmp_seq=2 ttl=64 time=0.338 ms
mysql 1:
[root@Mysql1 ~]# cat /etc/my.cnf
#default-character-set=utf8 ## 这句话注释掉,不然健康检查过不去
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true ## 允许从服务器同步
==》》wq
[root@mysql1 ~]# systemctl restart mysqld
mysql 2:
配置从服务器1:
在/etc/my.cnf 中修改或者增加下面内容。
[root@Mysql2 ~]# vim /etc/my.cnf
#default-character-set=utf8 ## 这句话注释掉,不然健康检查过不去
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
==》》wq
[root@mysql1 ~]# systemctl restart mysqld
mysql 3:
配置从服务器2:
在/etc/my.cnf 中修改或者增加下面内容。
[root@Mysql2 ~]# vim /etc/my.cnf
#default-character-set=utf8 ## 这句话注释掉,不然健康检查过不去
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
==》》wq
[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 主从配置相对比较简单。需要注意的是授权。步骤如下:
在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是 manager 使用。
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'; ## 三个数据库可以被manger所管理
mysql> flush privileges;
② 在 Mysql1 主机上查看二进制文件和同步点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 895 | | | |
+-------------------+----------+--------------+------------------+-------------------+
③ 接下来在 Mysql2 和 Mysql3 分别执行同步。
mysql> change master to master_host='20.0.0.23',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=895;
mysql> start slave;
④ 查看 IO 和 SQL 线程都是 yes 代表同步是否正常。
分别在mysql 2 和 mysql 3 查看
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
⑤ 必须设置两个从库为只读模式:
mysql> set global read_only=1;
mysql> flush privileges;
[root@mysql1 ~]# cd /etc/yum.repos.d/
[root@mysql1 yum.repos.d]# mv backup/CentOS-Base.repo ./
[root@mysql1 yum.repos.d]# ll
total 8
drwxr-xr-x. 2 root root 163 Oct 21 02:21 backup
-rw-r--r--. 1 root root 1664 Nov 23 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 116 Sep 13 21:35 local.repo
[root@mysql1 yum.repos.d]# yum clean all
[root@mysql1 yum.repos.d]# yum makecache
[root@MHA-manager ~]# 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
[root@Mysql1 ~]# tar zxvf mha4mysql-node-0.57.tar.gz
[root@Mysql1 ~]# cd mha4mysql-node-0.57
[root@mysql1 mha4mysql-node-0.57]# ls
AUTHORS COPYING inc Makefile.PL META.yml rpm
bin debian lib MANIFEST README t
[root@Mysql1 mha4mysql-node-0.57]# perl Makefile.PL
[root@Mysql1 mha4mysql-node-0.57]# make && make install
[root@MHA-manager ~]# tar zxvf mha4MHA-manager-0.57.tar.gz
[root@MHA-manager ~]# cd mha4MHA-manager-0.57
[root@localhost mha4mysql-manager-0.57]# ls
AUTHORS COPYING inc Makefile.PL META.yml rpm t
bin debian lib MANIFEST README samples tests
[root@MHA-manager mha4MHA-manager-0.57]# perl Makefile.PL
[root@MHA-manager mha4MHA-manager-0.57]# make && make install
1. 在 manager 上配置到所有数据库节点的无密码认证
[root@MHA-manager ~]# ssh-keygen -t rsa //一路按回车键
[root@MHA-manager ~]# ssh-copy-id 20.0.0.23 ## 按yes,然后输入 root 密码
[root@MHA-manager ~]# ssh-copy-id 20.0.0.24
[root@MHA-manager ~]# ssh-copy-id 20.0.0.25
2. 在 Mysql1 上配置到数据库节点Mysql2和Mysql3的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa //一路按回车键
[root@Mysql1 ~]# ssh-copy-id 20.0.0.24 ## 按yes,然后输入 root 密码
[root@Mysql1 ~]# ssh-copy-id 20.0.0.25
3. 在 Mysql2 上配置到数据库节点Mysql1和Mysql3的无密码认证
[root@Mysql2 ~]# ssh-keygen -t rsa //一路按回车键
[root@Mysql2 ~]# ssh-copy-id 20.0.0.23 ## 按yes,然后输入 root 密码
[root@Mysql2 ~]# ssh-copy-id 20.0.0.25
4. 在 Mysql3 上配置到数据库节点Mysql1和Mysql2的无密码认证
[root@Mysql3 ~]# ssh-keygen -t rsa //一路按回车键
[root@Mysql3 ~]# ssh-copy-id 20.0.0.23 ## 按yes,然后输入 root 密码
[root@Mysql3 ~]# ssh-copy-id 20.0.0.24
[root@MHA-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
[root@atlas ~]# ll /usr/local/bin/scripts/
总用量 32
-rwxr-xr-x 1 mysql mysql 3648 5 月 31 2015 master_ip_failover #自动切换时 VIP 管理的脚本
-rwxr-xr-x 1 mysql mysql 9872 5 月 25 09:07 master_ip_online_change #在线切换时 vip 的管理
-rwxr-xr-x 1 mysql mysql 11867 5 月 31 2015 power_manager #故障发生后关闭主机的脚本
-rwxr-xr-x 1 mysql mysql 1360 5 月 31 2015 send_report #因故障切换后发送报警的脚本
[root@MHA-manager ~]# cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
[root@MHA-manager ~]#vim /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;
##################################################################################
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";
}
①、:% s/^#// 把全文的开头的#号删除
②、 第一行开头加上#号 #!/usr/bin/env perl
③、my $vip = '20.0.0.200'; ## 漂移地址,改成自己网段的IP
my $brdc = '20.0.0.255'; ## 改成自己网段
[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.24 -s 20.0.0.25
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.23
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.24
port=3306
[server3]
hostname=20.0.0.25
port=3306
[root@MHA-manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
Tue Nov 26 23:09:46 2019 - [debug] Connecting via SSH from [email protected](192.168.195.131:22) to [email protected](192.168.195.130:22)..
Tue Nov 26 23:09:47 2019 - [debug] ok.
Tue Nov 26 23:09:47 2019 - [info] All SSH connection tests passed successfully.
root@MHA-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf ## 测试健康状态
Checking the Status of the script.. OK
Tue Nov 26 23:10:29 2019 - [info] OK.
Tue Nov 26 23:10:29 2019 - [warning] shutdown_script is not defined.
Tue Nov 26 23:10:29 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
[root@Mysql1 ~]# ifconfig 查看一下
如果是最小化安装需要先安装网络组件
[root@mysql1 ~]# yum -y install net-tools
[root@Mysql1 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255
ether 00:0c:29:a4:3e:ec txqueuelen 1000 (Ethernet)
[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 &
[1] 129929
[root@localhost ~]# jobs ## 查看后台运行的任务
[1]+ Running 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 &
[root@MHA-manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:32493) is running(0:PING_OK), master:20.0.0.23 ## master是20.0.0.23,对的
[root@MHA-manager ~]# cat /var/log/masterha/app1/manager.log
Tue Nov 26 23:12:30 2019 - [info] Checking master_ip_failover_script status:
Tue Nov 26 23:12:30 2019 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=20.0.0.23 --orig_master_ip=20.0.0.23 --orig_master_port=3306
[root@MHA-manager ~]#tailf /var/log/masterha/app1/manager.log ## 启用监控观察日志记录
[root@mysql1 ~]# systemctl stop mysqld ## 关闭主数据库mysql1上的数据库
我们可以在日志里面看到漂移地址已经成功
20.0.0.25(20.0.0.25:3306): OK: Applying all logs succeeded. Slave started, replicating from 20.0.0.24(20.0.0.24:3306)
我们现在去mysql2查看一下网卡配置信息,漂移地址已经漂移成功!
[root@mysql2 ~]# ifconfig
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255
ether 00:0c:29:a4:3e:ec txqueuelen 1000 (Ethernet)
再查看一下mysql 1的网卡配置信息
[root@mysql2 ~]# ifconfig
已经没有 ens33:1 虚拟接口信息