主机 | 操作系统 | 地址 | 安装包 |
---|---|---|---|
Master节点服务器 | CentOS7(64位) | 192.168.71.12 | mysql-boost-5.7.20.tar.gz、MHA node组件 |
Slave1 节点服务器 | CentOS7(64位) | 192.168.71.13 | mysql-boost-5.7.20.tar.gz、MHA node组件 |
Slave2 节点服务器 | CentOS7(64位) | 192.168.71.14 | mysql-boost-5.7.20.tar.gz、MHA node组件 |
MHA manager节点服务器 | CentOS7(64位) | 192.168.71.15 | MHAmanager组件、MHA node组件 |
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
在数据库提权时需要使用
hostnamectl set-hostname Mysql1
su -
hostnamectl set-hostname Mysql2
su -
hostnamectl set-hostname Mysql3
su -
Master 节点(192.168.71.12)
vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
systemctl restart mysqld
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
slave1(192.168.71.13)、slave2(192.168.71.14)
vim /etc/my.cnf
server-id = 2 #三台服务器的 server-id 不能一样
(server-id = 3) #slave2配置文件id
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/
在master slave1 和 slave2 上数据库提权
在所有数据库节点上授权两个用户,一个是从库同步使用,另一个是manager使用,第四个账户managter管理mysql集群使用(监控)
mysql -uroot -p123
#允许使用myslave用户登入主服务器对数据库复制
grant replication slave on *.* to 'myslave'@'192.168.71.%' identified by '123';
#允许mha用户登入数据库通过71网段登入对数据库操作
grant all privileges on *.* to 'mha'@'192.168.71.%' identified by 'manager'; #这里一定要使用manager不能更改
#这三条不用授权理论上不用添加,但是实验环境通过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;
Master 节点查看二进制文件和同步点
查看二进制文件及位置点
show master status;
slave1(192.168.71.13) slave2(192.168.71.14)
change master to master_host='192.168.71.12',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=1745;
start slave;
在 Slave1、Slave2 节点查看数据同步结果
show slave status\G #确保 IO 和 SQL 线程都是 Yes,代表同步正常
set global read_only=1;
所有服务器上都安装 MHA 依赖的环境,首先安装 epel 源
mv /etc/yum.repos.d/repo.bak/CentOS-* /etc/yum.repos.d/
yum list
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
报错一:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - “Could not resolve host: mirrorlist.centos.org; 未知的错误”
解决思路:
1.首先保证网络联通性 可以ping 8.8.8.8——>如果不通检查玩网配置文件
2.到/etc目录下配置resolv.conf加入nameserver IP,如:
nameserver 8.8.8.8
nameserver 8.8.4.4
search localdomain
3.修改 /etc/yum.repos.d/epel.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
4.或者删除/etc/yum.repos.d/*所有文件
或 rm -f /etc/yum.repos.d/CentOS-Base.repo
然后重新下载阿里的:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
清理缓存:
yum clean all
报错二:网卡无法重启之前网卡是正常只用的而且也能ping通外网
对于每个操作系统版本不一样,这里 CentOS7.4 必须选择 0.57 版本。
在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件。
cd /opt #将软件包mha4mysql-node-0.57.tar.gz放入/opt目录下
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make && make install
cd /opt #将软件包mha4mysql-manager-0.57.tar.gz放入/opt目录下
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make && make install
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 |
save_binary_logs | 保存和复制 master 的二进制日志 |
---|---|
apply_diff_relay_logs | 识别差异的中继日志事件并将其差异的事件应用于其他的 slave |
filter_mysqlbinlog | 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具) |
purge_relay_logs | 清除中继日志(不会阻塞 SQL 线程) |
MHA节点 192.168.71.15
ssh-keygen -t rsa #一直按回车键
ssh-copy-id 192.168.71.12
ssh-copy-id 192.168.71.13
ssh-copy-id 192.168.71.14
mysql1(192.168.71.12)
ssh-keygen -t rsa
ssh-copy-id 192.168.71.13
ssh-copy-id 192.168.71.14
mysql2(192.168.71.13)
ssh-keygen -t rsa
ssh-copy-id 192.168.71.12
ssh-copy-id 192.168.71.14
mysql3(192.168.71.14)
ssh-keygen -t rsa
ssh-copy-id 192.168.71.12
ssh-copy-id 192.168.71.13
cp -rp /opt/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
#复制后会有四个执行文件
ll /usr/local/bin/scripts/
master_ip_failover | 自动切换时 VIP 管理的脚本 |
---|---|
master_ip_online_change | 在线切换时 vip 的管理 |
power_manager | 故障发生后关闭主机的脚本 |
send_report | 因故障切换后发送报警的脚本 |
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
重写
echo '' > /usr/local/bin/master_ip_failover #清空配置文件的所有代码
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.71.200'; #指定vip的地址
my $brdc = '192.168.71.255'; #指定vip的广播地址
my $ifdev = 'ens33'; #指定vip绑定的网卡
my $key = '1'; #指定vip绑定的虚拟网卡序列号
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.153.200
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.153.200 down
my $exit_code = 0; #指定退出状态码为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 /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
echo '' > /etc/masterha/app1.cnf #删除原有内容,直接复制并修改节点服务器的IP地址
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 192.168.71.13 -s 192.168.71.14
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.71.12
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.71.13
port=3306
[server3]
hostname=192.168.153.14
port=3306
/sbin/ifconfig ens33:1 192.168.71.200/24
masterha_check_ssh -conf=/etc/masterha/app1.cnf
MHAmanager(192.168.71.15)
masterha_check_repl -conf=/etc/masterha/app1.cnf
#最后出现 MySQL Replication Health is OK 字样说明正常;
#出现MySQL Replication Health is NOT OK!的,可以去看一下mysql服务器上的软链接是否少创建-->本文位置:2、修改三台MySQL服务
MHAmanager(192.168.71.15)
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, 之所以
MHAmanager(192.168.71.5)
查看 MHA 状态
masterha_check_status --conf=/etc/masterha/app1.cnf
也以看到当前的 master 是 192.168.71.12
cat /var/log/masterha/app1/manager.log | grep "current master"
192.168.71.200 是否存在,这个 VIP 地址不会因为 manager 节点停止 MHA 服务而消失。
ifconfig
可以使用如下命令,或者可以直接采用 kill 进程 ID 的方式关闭
masterha_stop --conf=/etc/masterha/app1.cnf
tail -f /var/log/masterha/app1/manager.log
mysql1(192.168.71.12)
systemctl stop mysqld
或
pkill -9 mysql
在slave查看网卡信息及MHA manager节点查看动态日志
mysql1(192.168.71.12)
systemctl restart mysqld
在现主库服务器 Mysql2 查看二进制文件和同步点
mysql2(192.168.71.13)
mysql -uroot -p123 -e 'show master status;'
#在数据库中执行show master status;
mysql1(192.168.163.11)
mysql -uroot -p123
change master to master_host='192.168.71.13',master_user='myslave',master_password='123',master_log_file='master-bin.000002',master_log_pos=3336; #注意这里备份的是slave1的地址,因为这时候slave1是master服务器
start slave;
show slave status\G
MHAmanager(192.168.71.15)
再把这个记录添加进去,因为它检测掉失效时候会自动消失
vim /etc/masterha/app1.cnf
......
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.163.12 -s 192.168.163.14
......
[server1]
hostname=192.168.163.13
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.71.12
port=3306
[server3]
hostname=192.168.71.14
port=3306
MHAmanager(192.168.71.15)
masterha_stop --conf=/etc/masterha/app1.cnf
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