MHA相关知识
mha使用的是半同步复制
MHA实验部署
节点服务器 | 主机名 | IP | 使用组件 |
master | master | 20.0.0.100 | MHA node |
slave1 | slave1 | 20.0.0.110 | MHA node |
slave2 | slave2 | 20.0.0.10 | MHA node |
MHA manager | MHA | 20.0.0.140 | MHA node 和 manager 组件 |
vip-----20.0.0.105 |
- 关闭防火墙
- systemctl stop firewalld
- setenforce 0
- hostnamectl set-hostname slave1/slave2/master
- 修改主机名
- su
-
- 直接Ping主机名,确保能通信
-
- 先实现主从复制!!!!!
- master
- vim /etc/my.cnf
- server-id = 1
- log_bin=master-bin
- log-slave-updates=true
- wq
-
-
- slave1
- vim /etc/my.cnf
- server-id = 2
- log_bin=master-bin
- relay-log=relay-log-bin
- relay-log-index=slave-relay-bin.index
- wq
-
-
- slave2
- vim /etc/my.cnf
- server-id = 3
- relay-log=relay-log-bin
- relay-log-index=slave-relay-bin.index
- wq
-
- master/slave1/slave2
- systemctl restart mysqld
-
- 所有服务器都要做软连接!!!!!!!
-
- ln -s /usr/local/mysql/bin/mysql /usr/sbin/
- ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
-
- master/slave1/slave2 数据库同步赋权(让slave访问主库,每台服务器都要操作)
- mysql -uroot -p123456
-
-
- flush privileges;
设置位置偏移
- show mater status\G;
-
- 设置位置偏移
- start slave;
- show slave status;
-
- slave1,2
- 两个从库的数据库设置只读模式
- set global read_only=1
- mater/slave1,2
- 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
先安装node组件,再安装MHA
MHA拖入node和manager组件
masterha_check_ssh:检查MHA的SSH的配置状况
masterha_check_epel:检查mysql的复制情况
masterha_manager:启动manager的脚本
masterha_check_status:检查MHA运行状态
masterha_master_monitior:检测master的主机情况,master是否宕机
masterha_master_switch:控制故障转移
masterha_conf_host:添加或者删除配置的server信息。
masterha_stop:停止MAH脚本
node组件
save_binary_logs:保存和复制master的二进制日志
apply_diff_relay_logs:识别二进制日志当中的差异时间,然后发送给其他的slave
filter_mysqlbinlog:去除不必要的回滚(MHA已经不用了)
purge_relay_log:同步之后清除中继日志(不会阻塞sql线程)
node组件作用
save_binary_logs 保存和复制mater的二进制日志
apply_diff_relay_logs:识别二进制日志当中的差异事件,然后发送给其他的slave
filter_mysqlbinlog: 去除不必要的回滚(MHA已经不用了)
purge_relay_logs:同步之后清楚中继日志(不会阻塞sql的线程)
- (1)在 manager 节点上配置到所有数据库节点的无密码认证
- ssh-keygen -t rsa #一路按回车键
- ssh-copy-id 20.0.0.100
- ssh-copy-id 20.0.0.110
- ssh-copy-id 20.0.0.10
-
- (2)在 master 上配置到数据库节点 slave1 和 slave2 的无密码认证
- ssh-keygen -t rsa
- ssh-copy-id 20.0.0.110
- ssh-copy-id 20.0.0.10
-
- (3)在 slave1 上配置到数据库节点 master 和 slave2 的无密码认证
- ssh-keygen -t rsa
- ssh-copy-id 20.0.0.100
- ssh-copy-id 20.0.0.10
-
- (4)在 slave2 上配置到数据库节点 master 和 slave1 的无密码认证
- ssh-keygen -t rsa
- ssh-copy-id 20.0.0.100
- ssh-copy-id 20.0.0.110
-
- 8.在 manager 节点上配置 MHA
- (1)在 manager 节点上复制相关脚本到/usr/local/bin 目录
- 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 #因故障切换后发送报警的脚本
- ----------------------------------------------------------------------------------------------------------
-
- 这里使用master_ip_failover脚本来管理 VIP 和故障切换
-
- cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
-
- (3)修改内容如下:(删除原有内容,直接复制并修改vip相关参数)
- 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.233.100'; #指定vip的地址
- my $brdc = '192.168.233.255'; #指定vip的广播地址
- my $ifdev = 'ens33'; #指定vip绑定的网卡
- my $key = '1'; #指定vip绑定的虚拟网卡序列号
- my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip"; #代表此变量值为ifconfig ens33:1 192.168.233.100
- my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down"; #代表此变量值为ifconfig ens33:1 192.168.233.100 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";
- }
管理 MySQL 主从复制设置中的虚拟 IP 故障切换而设计的,可能用于主服务器需要切换到另一台服务器的情景。
它使用 SSH 在远程服务器上执行命令来控制虚拟 IP 地址
- cp /opt/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha
-
- vim /etc/masterha/app1.cnf #删除原有内容,直接复制并修改节点服务器的IP地址
- [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=123456
- repl_user=myslave
- secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.110 -s 20.0.0.10
- #从对主监听
- shutdown_script=""
- ssh_user=root
- user=mha
-
- [server1]
- hostname=20.0.0.100
- #主服务器
- port=3306
-
- [server2]
- candidate_master=1
- check_repl_delay=0
- hostname=20.0.0.110
- #备用主服务器
- port=3306
-
- [server3]
- hostname=20.0.0.10
- #从服务器2
- port=3306
-
- Tue Nov 26 23:09:45 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
- Tue Nov 26 23:09:45 2020 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
- Tue Nov 26 23:09:45 2020 - [info] Reading server configuration from /etc/masterha/app1.cnf..
- Tue Nov 26 23:09:45 2020 - [info] Starting SSH connection tests..
- Tue Nov 26 23:09:46 2020 - [debug]
- Tue Nov 26 23:09:45 2020 - [debug] Connecting via SSH from root@192.168.80.11(192.168.80.11:22) to root@192.168.80.12(192.168.80.12:22)..
- Tue Nov 26 23:09:46 2020 - [debug] ok.
- Tue Nov 26 23:09:47 2020 - [debug]
- Tue Nov 26 23:09:46 2020 - [debug] Connecting via SSH from root@192.168.80.12(192.168.80.12:22) to root@192.168.80.11(192.168.80.11:22)..
- Tue Nov 26 23:09:47 2020 - [debug] ok.
- Tue Nov 26 23:09:47 2020 - [info] All SSH connection tests passed successfully.
- Tue Nov 26 23:10:29 2020 - [info] Slaves settings check done.
- Tue Nov 26 23:10:29 2020 - [info]
- 192.168.80.11(192.168.80.11:3306) (current master)
- +--192.168.80.12(192.168.80.12:3306)
-
- Tue Nov 26 23:10:29 2020 - [info] Checking replication health on 192.168.80.12..
- Tue Nov 26 23:10:29 2020 - [info] ok.
- Tue Nov 26 23:10:29 2020 - [info] Checking master_ip_failover_script status:
- Tue Nov 26 23:10:29 2020 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.80.11 --orig_master_ip=192.168.80.11 --orig_master_port=3306
-
-
- IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 192.168.80.200===
-
- Checking the Status of the script.. OK
- Tue Nov 26 23:10:29 2020 - [info] OK.
- Tue Nov 26 23:10:29 2020 - [warning] shutdown_script is not defined.
- Tue Nov 26 23:10:29 2020 - [info] Got exit code 0 (Not master dead).
-
- MySQL Replication Health is OK.
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_stop --conf=/etc/masterha/app1.cnf
故障模拟恢复
在 manager 节点上监控观察日志记录
tail -f /var/log/masterha/app1/manager.log
在 Master 节点 master 上停止mysql服务
systemctl stop mysqld
或
pkill -9 mysql
故障切换备选主库的算法:
1.一般判断从库的是从(position/GTID)判断优劣,数据有差异,最接近于master的slave,成为备选主。
2.数据一致的情况下,按照配置文件顺序,选择备选主库。
3.设定有权重(candidate_master=1),按照权重强制指定备选主。
(1)默认情况下如果一个slave落后master 100M的relay logs的话,即使有权重,也会失效。
(2)如果check_repl_delay=0的话,即使落后很多日志,也强制选择其为备选主。
在旧主服务器上配置虚拟网卡
ifconfig ens33:1 20.0.0.105/24
接下来设置位置偏移
第一次一定要重置服务
reset slave;
- server-id = 1
- log_bin = master-bin
- relay-log = relay-log-bin
- relay-log-index = slave-relay-bin.index
- server-id = 2
- log_bin = master-bin
- log-slave-updates = true
在 manager 节点上修改配置文件app1.cnf(再把这个记录添加进去,因为它检测掉失效时候会自动消失)
- vim /etc/masterha/app1.cnf
- .....
- secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.100 -s 20.0.0.10
- ......
- [server1]
- hostname=20.0.0.110
- port=3306
-
- [server2]
- candidate_master=1
- check_repl_delay=0
- hostname=20.0.0.100
- port=3306
-
- [server3]
- hostname=20.0.0.10
- port=3306
虚拟地址漂移
- 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 &
新主服务器更新,其他两个从也会更新,达到主从复制+高可用
总结:
1.搭建高可用服务之后,主故障之后,就相当于从,他的配置文件等都要和从的配置一样,还得对新从进行赋权(赋予从的权限)。
2.与此同时,MHA内部的配置文件也得修改主从配置。(将之前从的配置修改为新主,旧主配置文件修改为新从)
3.虚拟网卡只能在主服务器上配置,否则会出现脑裂导致实验失败