服务器 CentOS 7 mha-manager/20.0.0.17 管理节点,安装manager组件
服务器 CentOS 7 master/20.0.0.20 Master节点,安装node组件
服务器 CentOS 7 slave-master/20.0.0.19 Slave/Master节点,安装node组件
服务器 CentOS 7 slave/20.0.0.18 Slave节点,安装node组件
操作系统版本 CentOS 7版本,MHA版本 0.57 版本.
在三台MySQL节点上分别安装数据库,MySQL版本使用,5.6.36,cmake版本请使用2.8.6.
master、slave-master、slave三台设备上安装MySQL数据库
安装编译依赖的环境
//安装依赖环境//
yum -y install ncurses-devel gcc-c++ perl-Module-Install
安装gmake编译软件
//将源码包放到家目录下//
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake&&gmake install
安装MySQL数据库
//回到家目录//
cd
tar zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
make&&make install
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
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
修改master的著配置文件/etc/mycnf文件,三台服务器的server-id不能相同
vim /etc/my.cnf
...
server-id = 20 //master写20;slave-master写19;slave写18//
log-bin=master-bin //开启二进制日志文件//
log-slave-updates=true
vim /etc/my.cnf
...
server-id = 19|18
log_bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
systemctl restart mysqld
三台服务器分别做两个软链接
ln -s /usr/local/mysql/bin/mysql /usr/sbin
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin
启动MySQL
/usr/local/mysql/bin/mysqld_safe --user=mysql &
配置MySQL、一主两从
grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123';
grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';
flush privileges;
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave-master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave' identified by 'manager';
flush privileges;
show master status;
change master to master_host='20.0.0.20',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1289;
start slave;
setenforce 0
systemctl stop firewalld
mysql> show slave status\G;
//必须设置两个从库为只读模式//
mysql> set global read_only=1;
//注意:设置完成直接验证主从复制功能//
mysql> flush privileges;
所有服务器上都安装MHA依赖的环境,首先安装epel源
yum -y install epel-release --nogpgcheck
yum -y install perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
MHA软件包对于每个操作系统版本不一样,这里CentOS 7 必须选择0.57版本,在<注意:所有服务器>上必须先安装node组件,最后在MHA-manager节点上安装manager组件,因为manager依赖node组件,下面都是在master上操作演示安装node组件
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make&&make install
在mha-manager上安装manager组件(!注意:一定要先安装node组件才能安装manager组件)
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make&&make install
//mha-manager安装后/usr/local/bin 下面会生成几个工具,主要包括以下几个//
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manager 启动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线程)
//在mha-manager上配置到所有数据库节点的无密码认证//
ssh-keygen -t rsa //一直回车无密码
ssh-copy-id 20.0.0.20
ssh-copy-id 20.0.0.19
ssh-copy-id 20.0.0.18
//在master上配置到数据库节点slave-master和slave的无密码认证//
ssh-keygen -t rsa
ssh-copy-id 20.0.0.19
ssh-copy-id 20.0.0.18
//在slave-master上配置到数据库节点master和slave的无密码认证//
ssh-keygen -t rsa
ssh-copy-id 20.0.0.20
ssh-copy-id 20.0.0.18
//在slave上配置到数据库节点slave-master和master的无密码认证//
ssh-keygen -t rsa
ssh-copy-id 20.0.0.20
ssh-copy-id 20.0.0.19
配置mha
cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后有四个执行文件
ls /usr/local/bin/scripts/
master_ip_failover master_ip_online_change power_manager send_report
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
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 = '20.0.0.200';
my $brdc = '20.0.0.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";
}
mkdir /etc/masterha
cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
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.19 -s 20.0.0.18
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=20.0.0.20
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=20.0.0.19
port=3306
[server3]
hostname=20.0.0.18
port=3306
[root@mha-manager scripts]# cp /usr/local/bin/scripts/master_ip_online_change /usr/local/bin/
[root@mha-manager scripts]# cp /usr/local/bin/scripts/send_report /usr/local/
masterha_check_ssh --conf=/etc/masterha/app1.cnf
masterha_check_repl --conf=/etc/masterha/app1.cnf
注意
[root@master ~]# /sbin/ifconfig ens33:1 20.0.0.200/24
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 &
masterha_check_status --conf=/etc/masterha/app1.cnf
cat /var/log/masterha/app1/manager.log
[root@master ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 20.0.0.20 netmask 255.255.255.0 broadcast 20.0.0.255
inet6 fe80::f3d9:2d42:9596:bfb9 prefixlen 64 scopeid 0x20
ether 00:0c:29:81:28:de txqueuelen 1000 (Ethernet)
RX packets 610896 bytes 855719895 (816.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 205699 bytes 16844848 (16.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163 mtu 1500
inet 20.0.0.200 netmask 255.255.255.0 broadcast 20.0.0.255
ether 00:0c:29:81:28:de txqueuelen 1000 (Ethernet)
验证
//mha-manager服务器操作//
[root@mha-manager ~]# tailf /var/log/masterha/app1/manager.log
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===
Checking the Status of the script.. OK
Sat Aug 29 20:13:33 2020 - [info] OK.
Sat Aug 29 20:13:33 2020 - [warning] shutdown_script is not defined.
Sat Aug 29 20:13:33 2020 - [info] Set master ping interval 1 seconds.
Sat Aug 29 20:13:33 2020 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 20.0.0.19 -s 20.0.0.18
Sat Aug 29 20:13:33 2020 - [info] Starting ping health check on 20.0.0.20(20.0.0.20:3306)..
Sat Aug 29 20:13:33 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
^[[A^[[B^[[B^[[C^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^[[D^C
[root@localhost scripts]# tailf /var/log/masterha/app1/manager.log
IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===
Checking the Status of the script.. OK
Sat Aug 29 20:13:33 2020 - [info] OK.
Sat Aug 29 20:13:33 2020 - [warning] shutdown_script is not defined.
Sat Aug 29 20:13:33 2020 - [info] Set master ping interval 1 seconds.
Sat Aug 29 20:13:33 2020 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 20.0.0.19 -s 20.0.0.18
Sat Aug 29 20:13:33 2020 - [info] Starting ping health check on 20.0.0.20(20.0.0.20:3306)..
Sat Aug 29 20:13:33 2020 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
//master服务器操作//
[root@master ~]# pkill -9 mysql
//slave-master服务器操作//
[root@slave-master ~]# 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:55:a0:0c txqueuelen 1000 (Ethernet)