1、 安装MySQL数据库
2、 配置MySQl一主两从
3、 安装MHA软件
4、 配置无密码认证
5、 配置MySQL MHA 高可用
6、 模拟 master 故障切换
实验环境
主机名 | IP地址 | 系统 | 软件包 |
---|---|---|---|
mysql1(master) | 192.168.100.150(虚拟:192.168.100.200) | centos7.6 | mysql-5.6.36.tar.gz cmake-2.8.6.tar.gz mha4mysql-node-0.57.tar.gz |
mysql2(slave1) | 192.168.100.160 | centos7.6 | |
mysql3(slave2) | 192.168.100.170 | centos7.6 | |
manager | 192.168.100.130 | centos7.6 | mha4mysql-node-0.57.tar.gz mha4mysql-manager-0.57.tar.gz |
[root@Mysql1~]# yum install ncurses-devel gcc-c++ perl-Module-Install -y
//上传mysql-5.6.36.tar.gz cmake-2.8.6.tar.gz mha4mysql-node-0.57.tar.gz mha4mysql-manager-0.57.tar.gz
[root@Mysql1~]# tar zxvf cmake-2.8.6.tar.gz
[root@Mysql1 ~]# cd cmake-2.8.6
[root@Mysql1 cmake-2.8.6]# ./configure
[root@Mysql1 cmake-2.8.6]# gmake && gmake install
[root@Mysql1~ ]# tar zxvf mysql-5.6.36.tar.gz
[root@Mysql1 ~]# cd mysql-5.6.36
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITII_EXTRA_CHARSETS=all \
-DSYSCONFDTR=/etc
make && make install
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
cd ~
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
[root@Mysql1 ~]# cat /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
配置从服务器
[root@Mysql2 ~]# vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@Mysql3 ~]# vim /etc/my.cnf
server-id = 3
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
systemctl stop firewalld
setenforce 0
/usr/local/mysql/bin/mysqld_safe --user=mysql &
netstat -ntap | grep 3306
MySQL主从配置相对比较简单。需要注意的是授权。步骤如下:
在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是manager使用。
grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '123';
grant all privileges on *.* to 'mha'@'192.168.100.%' identified by 'manager';
flush privileges;
下面三条授权按理论是不用添加的,但是做案例实验环境时候通过MHA检查MySQL主从有报错,
报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权。
grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';
grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';
flush privileges;
在Mysq1主机上查看二进制文件和同步点
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1294 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
接下来在Mysql2和Mysql3分别执行同步。
change master to master_host='192.168.100.150',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1294;
flush privileges;
start slave;
查看IO和SQL线程都是yes代表同步是否正常。
show slave status\G;
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
必须设置两个从库为只读模式:
set global read_only=1;
flush privileges;
设置完成直接验证主从复制功能
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
MHA 软件包对于每个操作系统版本不一样,这里CentOS7.4必须选择0.57版本,注意:所有服务器上必须先安装node组件,最后在MHA-manager节点上安装manager组件,因为manager依赖node组件,下面都是在Mysql1上操作演示安装node组件。
//上传mha4mysql-node-0.57.tar.gz 到家目录
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
仅在MHA-manager上安装manager组件(!注意:一定要先安装node组件才能安装manager组件)
cd ~
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && 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线程)
在manager上配置到所有数据库节点的无密码认证
[root@mha-manager ~]# ssh-keygen -t rsa //一路按回车键
[root@mha-manager ~]# ssh-copy-id 192.168.100.150 //输入yes 及150的root密码
[root@mha-manager ~]# ssh-copy-id 192.168.100.160
[root@mha-manager ~]# ssh-copy-id 192.168.100.170
在Mysql1上配置到数据库节点Mysql2和Mysql3的无密码认证
[root@Mysql1 ~]# ssh-keygen -t rsa
[root@MysqI1 ~]# ssh-copy-id 192.168.100.160
[root@Mysql1 ~]# ssh-copy-id 192.168.100.170
在Mysql2上配置到数据库节点MysqI1和Mysql3的无密码认证
[root@Mysql2 ~]# ssh-keygen -t rsa
[root@Mysql2 ~]# ssh-copy-id 192.168.100.150
[root@Mysql2 ~]# ssh-copy-id 192.168.100.170
在Mysql3上配置到数据库节点Mysql1和Mysql2的无密码认证
[root@Mysql3 ~]# ssh-keygen -t rsa
[root@Mysql3 ~]# ssh-copy-id 192.168.100.150
[root@Mysql3 ~]# ssh-copy-id 192.168.100.160
在manager节点上复制相关脚本到/usr/local/bin目录
[root@mha-manager~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后会有四个执行文件
[root@MHA-manager ~]#ll/usr/local/bin/scripts/
总用量32
-rwxr-xr-x 1 mysql mysql 36485月312015 master_ip_failover #自动切换时VIP管理的脚本
-rwxr-xr-x 1 mysql mysql 98725月2509:07 master_ip_online_change #在线切换时vip的管理
-rwxr-xr-x 1 mysql mysql118675月312015 power_manager #故障发生后关闭主机的脚本
-rwxr-Xr-x 1 mysql mysql 13605月312015 send_report #因故障切换后发送报警的脚本
复制上述的自动切换时VIP管理的脚本到/usr/local/bin目录,这里使用脚本管理VIP
[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'; #指定虚拟VIP地址,注意这里要和master主机一个网段
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 ~]# cd /usr/local/bin/scripts/
[root@MHA-manager scripts]# cp master_ip_online_change /usr/local/bin/
[root@MHA-manager scripts]# cp send_report /usr/local/
[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日志'
manager_workdir=/var/log/masterha/app1 'manager工作目录'
master_binlog_dir=/usr/local/mysql/data 'master保存binlog的位置,也是master的数据存放路径
master_ip_failover_script=/usr/local/bin/master_ip_failover '设置自动failover时候的切换脚本,也就是上边的那个脚本'
master_ip_online_change_script=/usr/local/bin/master_ip_online_change '设置手动切换时候的切换脚本'
user=mha '设置监控用户root'
password=manager '#设置mysql中root用户的密码,这个密码是前文中创建监控用户的那个密码'
ping_interval=1 '设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行rail
remote_workdir=/tmp '设置远端mysql在发生切换时binlog的保存位置'
repl_password=123 '设置复制用户的密码,这里对应主从复制授权的账户和密码'
repl_user=myslave '设置复制用户的用户'
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.100.160 -s 192.168.100.170 '设置从服务器的地址'
shutdown_script="" '设置故障发生后关闭故障主机脚本(该脚本的主要作用是关闭主机放在发生脑裂,这里没有使用)'
ssh_user=root '设置ssh的登录用户名'
[server1] 'master节点'
hostname=192.168.100.150
port=3306
[server2] 'salve1'
hostname=192.168.100.160
port=3306
candidate_master=1 '设置为候选master'
check_repl_delay=0 '默认情况下如果一个slave落后master 100M的relay logs 的话,mha将不会选择该slave作为一个新的master'
[server3] 'slave2'
hostname=192.168.100.170
port=3306
测试ssh无密码认证,如果正常最后会输出successfully
[root@mha-manager~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
......
Fri Aug 28 12:42:39 2020 - [info] All SSH connection tests passed successfully.
'这一步有问题说明秘钥分发问题'
检查健康状况(关键)
[root@mha-manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
Fri Aug 28 16:20:51 2020 - [info] Checking replication health on 192.168.100.160..
Fri Aug 28 16:20:51 2020 - [info] ok.
Fri Aug 28 16:20:51 2020 - [info] Checking replication health on 192.168.100.170..
Fri Aug 28 16:20:51 2020 - [info] ok.
Fri Aug 28 16:20:51 2020 - [info] Checking master_ip_failover_script status:
Fri Aug 28 16:20:51 2020 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.100.150 --orig_master_ip=192.168.100.150 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.100.200===
Checking the Status of the script.. OK
Fri Aug 28 16:20:51 2020 - [info] OK.
Fri Aug 28 16:20:51 2020 - [warning] shutdown_script is not defined.
Fri Aug 28 16:20:51 2020 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
注意:第一次配置需要去master上手动开启虚拟IP
[root@Mysql1 ~]# /sbin/ifconfig ens33:1 192.168.100.200/24
[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 &
--remove_dead_master_conf该参数代表当发生主从切换后,老的主库的ip将会从配置文件中移除
--manger_log日志存放位置
--ignore_last_failover在缺省情况下,如果MHA 检测到连续发生宕机,且两次宕机间隔不足8小时的话,则不会进行Failover,之所以这样限制是为了避免ping-pong效应。
[root@mha-manager~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:70465) is running(0:PING_OK), master:192.168.100.150
[root@mha-manager ~]# cat /var/log/masterha/app1/manager.log
.......
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.100.200===
.........
[root@mysql1 mha4mysql-node-0.57]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.150 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::7264:7578:de4e:190 prefixlen 64 scopeid 0x20<link>
inet6 fe80::9433:6cdb:ee1d:7b3d prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:17:9c:75 txqueuelen 1000 (Ethernet)
RX packets 101492 bytes 130137031 (124.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43150 bytes 7281255 (6.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.200 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:17:9c:75 txqueuelen 1000 (Ethernet)
在mha服务器上启动监控观察日志记录
[root@mha-manager ~]# tailf /var/log/masterha/app1/manager.log
关闭mysql1中的mysql服务,此时再次查看VIP地址,mysql1中已消失,飘到主备服务器
[root@mysql1 ~]# pkill -9 mysql
[root@mysql2 mha4mysql-node-0.57]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.160 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::1bbb:ba05:a579:42dd prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9f:ee:79 txqueuelen 1000 (Ethernet)
RX packets 100963 bytes 130156228 (124.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40046 bytes 6979445 (6.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.200 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:9f:ee:79 txqueuelen 1000 (Ethernet)
mha日志查看
Invalidated master IP address on 192.168.100.150(192.168.100.150:3306)
The latest slave 192.168.100.160(192.168.100.160:3306) has all relay logs for recovery.
Selected 192.168.100.160(192.168.100.160:3306) as a new master.
192.168.100.160(192.168.100.160:3306): OK: Applying all logs succeeded.
192.168.100.160(192.168.100.160:3306): OK: Activated master IP address.
192.168.100.170(192.168.100.170:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.100.170(192.168.100.170:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.100.160(192.168.100.160:3306)
192.168.100.160(192.168.100.160:3306): Resetting slave info succeeded.
Master failover to 192.168.100.160(192.168.100.160:3306) completed successfully. 'master已经成功切换到备用160上面'
manager上查看
[root@mha-manager bin]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:72487) is running(0:PING_OK), master:192.168.100.160
以上说明mha功能已经实现当master故障,会把主服务器到主-备服务器上,继续维持主从关系