注:本文基于MySQL高可用之MHA
配置VIPphp
vip配置能够采用两种方式:
一、经过keepalived的方式管理虚拟ip的浮动;
二、经过脚本方式启动虚拟ip 的方式(即不须要keepalived或者heartbeat相似的软件)mysql
一、keepalived方式管理虚拟ipsql
#在编译安装 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漂移次数,一般是把修复好的主库当作新的备库。数据库
二、MHA引入keepalived(MySQL服务进程挂掉时经过MHA 中止keepalived)
要想把keepalived服务引入 MHA,咱们只须要修改切换时触发的脚本文件master_ip_failover便可,在该脚本中添加在master发生宕机时对 keepalived的处理vim
#编辑脚本,修改以下 [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
二、经过脚本实现VIP切换
修改/scripts/master_ip_failover,也可使用其余的语言完成,好比php语 言。使用php脚本编写的failover这里就不介绍了。修改完成后内容以下:app
#若是使用脚本管理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:less
[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必备技能掌握:一、MySQL架构:对mysql的架构,总体有个印象,才能不断的加深对mysql的理解和后继 的学习。 二、用各类姿式备份MySQL数据库 数据备份是DBA或运维工程师平常工做之一,若是让你来备份,你 会用什么方式备份,在时间时间备份,使用什么策略备份 三、mysql主从复制及读写分离 mysql的主从复制及读 写分离是DBA必备技能之一 四、MySQL/MariaDB数据库基于SSL实现主从复制 增强主从复制的安全性 五、 MySQL高可用 数据的高可用如何保证 六、数据库Sharding的基本思想和切分策略 随着数据量的不断攀升,从性 能和可维护的角度,须要进行一些Sharding,也就是数据库的切分,有垂直切分和水平切分 七、 MySQL/MariaDB 性能调整和优化技巧 掌握优化思路和技巧,对数据库的不断优化是一项长期工程