标签:usr 状态 host ica 查看ip 返回 run lse nopreempt
**vip配置可以采用两种方式:**
1、通过keepalived的方式管理虚拟ip的浮动;
2、通过脚本方式启动虚拟ip 的方式(即不需要keepalived或者heartbeat类似的软件)
1、keepalived方式管理虚拟ip
#在编译安装 Keepalived之前,必须先安装内核开发包kernel-devel以及openssl-devel、popt-devel等支持库
[root@master ~]# yum -y install kernel-devel popt-devel openssl-devel
#在两台master上进行安装
[root@master ~]# wget https://www.keepalived.org/software/keepalived-2.1.3.tar.gz
[root@master ~]# tar zxf keepalived-2.1.3.tar.gz
[root@master ~]# cd keepalived-2.1.3/
[root@master keepalived-2.1.3]# ./configure --prefix=/ && make && make install
#修改配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql-ha1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.171.250
}
}
[root@master ~]# scp /etc/keepalived/keepalived.conf root@192.168.171.152:/etc/keepalived/
#master2配置修改
[root@slave1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id mysql-ha2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.171.250
}
}
[root@master ~]# systemctl start keepalived # 启动服务
[root@master ~]# ip a | grep ens33 # 检测IP 可以发现 VIP已经抢占
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.171.151/24 brd 192.168.171.255 scope global ens33
inet 192.168.171.250/32 scope global ens33
注意: 上面两台服务器的keepalived都设置为了BACKUP模式,在keepalived中2种模式,分别是master>backup模式和backup->backup模式。这两种模式有很大区别。在master->backup模式下,一旦主库 宕机,虚拟ip会自动漂移到从库,当主库修复后,keepalived启动后,还会把虚拟ip抢占过来,即使设置 了非抢占模式(nopreempt)抢占ip的动作也会发生。在backup->backup模式下,当主库宕机后虚拟ip 会自动漂移到从库上,当原主库恢复和keepalived服务启动后,并不会抢占新主的虚拟ip,即使是优先 级高于从库的优先级别,也不会发生抢占。为了减少ip漂移次数,通常是把修复好的主库当做新的备库。
2、MHA引入keepalived(MySQL服务进程挂掉时通过MHA 停止keepalived)
要想把keepalived服务引入 MHA,我们只需要修改切换时触发的脚本文件master_ip_failover即可,在该脚本中添加在master发生宕机时对 keepalived的处理
#编辑脚本,修改如下
[root@manager ~]# vim /scripts/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.171.250‘;
my $ssh_start_vip = "systemctl start keepalived.service";
my $ssh_stop_vip = "systemctl stop keepalived.service";
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";
#`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
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() {
return 0 unless ($ssh_user);
`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";
}
#在配置文件中添加如下内容
[root@manager ~]# vim /etc/masterha/app1.cnf # 在[server default]下面添加
master_ip_failover_script=/scripts/master_ip_failover
[root@manager ~]# masterha_stop --conf=/etc/masterha/app1.cnf # 停止MHA服务
Stopped app1 successfully.
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log & # 再启动
[root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf # 查看状态
app1 (pid:8560) is running(0:PING_OK), master:192.168.171.151
测试:关闭master1,模拟宕机
#slave上查看,已经转移主
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.171.152
Master_User: mharep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#查看VIP绑定
[root@slave1 ~]# ip a |grep ens33 # 可以看到VIP地址已经漂移到了master2上
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.171.152/24 brd 192.168.171.255 scope global ens33
inet 192.168.171.250/32 scope global ens33
主从切换后续工作--重构: 重构就是你的主挂了,切换到Candicate master上,Candicate master变成了主,因此重构的一种方案原主库修复成一个新的slave 主库 切换后,把原主库修复成新从库,原主库数据文件完整的情况下,可通过以下方式找出最后执行的CHANGE MASTER命令:
[root@manager ~]# grep "CHANGE MASTER TO MASTER" /masterha/app1/manager.log | tail -1
Fri Jul 3 09:38:31 2020 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST=‘192.168.171.152‘, MASTER_PORT=3306, MASTER_LOG_FILE=‘mysql-bin.000001‘, MASTER_LOG_POS=746, MASTER_USER=‘mharep‘, MASTER_PASSWORD=‘xxx‘;
将原主库修复成从库
[root@master ~]# systemctl start mysqld
mysql> change master to master_host=‘192.168.171.152‘,master_port=3306,master_log_file=‘mysql-bin.000001‘,master_log_pos=746,master_user=‘mharep‘,master_password=‘123‘;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.171.152
Master_User: mharep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 746
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
[root@master ~]# systemctl start keepalived # 将Keepalived启动
#启动mha manager
[root@manager ~]# rm -rf /masterha/app1/app1.failover.complete # 首先需要将这个failover删除掉,要不启动不了
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --ignore_fail_on_start &>/tmp/mha_manager.log &
[root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:49782) is running(0:PING_OK), master:192.168.171.152
[root@manager ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
#然后这样就恢复原样了,当备master宕机后,原主master会再次启动抢占VIP
2、通过脚本实现VIP切换
修改/scripts/master_ip_failover,也可以使用其他的语言完成,比如php语 言。使用php脚本编写的failover这里就不介绍了。修改完成后内容如下:
#如果使用脚本管理vip的话,需要 手动在master服务器上绑定一个vip
[root@master ~]# ifconfig ens33:0 192.168.171.250/24
[root@master ~]# ip a | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.171.151/24 brd 192.168.171.255 scope global ens33
inet 192.168.171.250/24 brd 192.168.171.255 scope global secondary ens33:0
#修改/scripts/ master_ip_failover
[root@manager ~]# vim /scripts/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.171.250‘;
my $key = ‘0‘;
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; # 照之前的脚本就修改了几个变量,上下
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
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";
#`ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
exit 0;
}
else {
&usage();
exit 1;
}
}
# A simple system call that enable the VIP on the new master
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() {
return 0 unless ($ssh_user);
`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";
}
[root@manager ~]# grep "master_ip_failover_script" /etc/masterha/app1.cnf
master_ip_failover_script=/scripts/master_ip_failover
#在/etc/masterha/app1.cnf 填入上方返回内容,其实在之前已经添加过了,这里提示只做了脚本的人切记添加
停止MHA:
[root@manager ~]# masterha_stop --conf=/etc/masterha/app1.cnf
启动MHA
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf &>/tmp/mha_manager.log &
[root@manager ~]# masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:50564) is running(0:PING_OK), master:192.168.171.151
#再检查集群状态,看是否会报错
[root@manager ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
测试: 在master上停掉mysql服务
#在slave主机上查看状态
mysql> show slave status\G # 发现已经变为了备主IP
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.171.152
Master_User: mharep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
#在备主上查看IP
[root@slave1 ~]# ip a | grep ens33 # 可以看到VIP已经漂移过来了
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.171.152/24 brd 192.168.171.255 scope global ens33
inet 192.168.171.250/24 brd 192.168.171.255 scope global secondary ens33:0
注:为了防止脑裂发生,推荐生产环境采用脚本的方式来管理虚拟ip,而不是使用keepalived来完成。
总结:
MHA软件由两部分组成,Manager工具包和Node工具包,具体的说明如下:
Manager工具包主要包括 以下几个工具:
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manger 启动MHA
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测 master是否宕机
masterha_master_switch 控制故障转移(自动或者手动)
masterha_conf_host 添加或删除配置 的server信息
Node工具包(这些工具通常由MHA Manager的脚本触发,无需人为操作)主要包括以下几个工具:
save_binary_logs 保存和复制master的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差 异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志(不会阻塞SQL线程)
mysql必备技能掌握:
1、MySQL架构:对mysql的架构,整体有个印象,才能不断的加深对mysql的理解和后继 的学习。
2、用各种姿势备份MySQL数据库 数据备份是DBA或运维工程师日常工作之一,如果让你来备份,你 会用什么方式备份,在时间时间备份,使用什么策略备份
3、mysql主从复制及读写分离 mysql的主从复制及读 写分离是DBA必备技能之一
4、MySQL/MariaDB数据库基于SSL实现主从复制 加强主从复制的安全性
5、 MySQL高可用 数据的高可用如何保证
6、数据库Sharding的基本思想和切分策略 随着数据量的不断攀升,从性 能和可维护的角度,需要进行一些Sharding,也就是数据库的切分,有垂直切分和水平切分
7、 MySQL/MariaDB 性能调整和优化技巧 掌握优化思路和技巧,对数据库的不断优化是一项长期工程
标签:usr 状态 host ica 查看ip 返回 run lse nopreempt
原文地址:https://blog.51cto.com/14306186/2511600