当前位置:   article > 正文

mysql群集网络故障_MySQL高可用群集MHA部署及故障测试

rx packets 2748636 bytes 1313975447 (1.2 gib)

摘要

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本人youshimation开发,是一套优秀的MySQL故障切换和主从提升的高可用软件,解决mysql主服务器单点故障的方法一般常用的有两个mmm和mha,但是一般从真正的高可用的场合,其实最常用的是mha。如何避免mysql单点故障利用mysql主从复制来解决mysql单点故障如何解决主服务器的单点问题主服务器切换后,如何通知应用新的主服务器的ip地址如何检查mysql主服务器是否可用如何处理从服务器和新主服务器之间的那种复制关系

一、案例前置知识

1.1、MHA概述

1.1.1、一套优秀的MySQL高可用环境下故障切换和主从复制的软件

1.1.2、MySQL故障过程中,MHA能做到0-30秒内自动完成故障切换

1.2、MHA的组成

1.2.1、MHA Manager(管理节点)

Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。

1.2.2、MHA Node(数据节点)

Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。

1.3、MHA特点

1.3.1、自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失

1.3.2、使用半同步复制,可以大大降低数据丢失的风险

1.3.3、目前MHA支持一主多从架构,最少三台服务,即一主两从

1.4、MHA缺点

1.4.1、需要编写脚本或利用第三方工具来实现vip的配置

1.4.2、MHA启动后只会对数据库进行监控,需要基于ssh免认证配置,存在一定的安全隐患

1.4.3、没有提供从服务器的读负载均很的功能。

二、案例拓扑图

6fc08bf259030116c2f94ba1c7c334c7.png

三、案例部署

3.1、部署思路

3.1.1、MHA架构

①数据库安装

②一主两从

③MHA搭建

3.1.2、故障模拟

①主库失效

②备选主库成为主库

③从库2将备选主库指向为主库

3.1.3、故障修复

①坏库修复,启动

②在修复好的库上建立新主从关系

③修改manager配置文件,添加修好的库的记录

④重启mha

主服务器:mysql-server 20.0.0.10,从1服务器:mysql-slave1 20.0.0.20,从2服务器:mysql-slave2 20.0.0.30,mha:20.0.0.40

3.2、在三台 MySQL 节点上分别安装数据库

MySQL 版本请使用 5.6.36,cmake 版本请使用 2.8.6。安装过程如下。

1 主服务器2 yum -y install ncurses-devel gcc-c++ perl-Module-Install3 tar zxvf cmake-2.8.6.tar.gz4 cd cmake-2.8.6

5 ./configure6 gmake && gmake install

7 cd8 tar zxvf mysql-5.6.36.tar.gz9 cd mysql-5.6.36

10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc11 make && make install

12 cp support-files/my-default.cnf /etc/my.cnf13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld14 chmod +x /etc/rc.d/init.d/mysqld15 chkconfig --add mysqld16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile17 source /etc/profile18 groupadd mysql19 useradd -M -s /sbin/nologin mysql -g mysql20 chown -R mysql.mysql /usr/local/mysql21 mkdir -p /data/mysql22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql23 echo '24 [client]25 port = 3306

26 socket = /usr/local/mysql/mysql.sock27

28 [mysql]29 port = 3306

30 socket = /usr/local/mysql/mysql.sock31

32 [mysqld]33 user =mysql34 basedir = /usr/local/mysql35 datadir = /usr/local/mysql/data36 port = 3306

37 pid-file = /usr/local/mysql/mysqld.pid38 socket = /usr/local/mysql/mysql.sock39 server-id = 1 #三台服务器不能一样

40 log_bin = master-bin41 log-slave-updates = true

42

43 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES'> /etc/my.cnf

44 ln -s /usr/local/mysql/bin/mysql /usr/sbin/

45 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

46 systemctl start mysqld47 netstat -anpt | grep 3306

1 从1服务器作为主服务器的备选服务器2 yum -y install ncurses-devel gcc-c++ perl-Module-Install3 tar zxvf cmake-2.8.6.tar.gz4 cd cmake-2.8.6

5 ./configure6 gmake && gmake install

7 cd8 tar zxvf mysql-5.6.36.tar.gz9 cd mysql-5.6.36

10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc11 make && make install

12 cp support-files/my-default.cnf /etc/my.cnf13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld14 chmod +x /etc/rc.d/init.d/mysqld15 chkconfig --add mysqld16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile17 source /etc/profile18 groupadd mysql19 useradd -M -s /sbin/nologin mysql -g mysql20 chown -R mysql.mysql /usr/local/mysql21 mkdir -p /data/mysql22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql23 echo '24 [client]25 port = 3306

26 socket = /usr/local/mysql/mysql.sock27

28 [mysql]29 port = 3306

30 socket = /usr/local/mysql/mysql.sock31

32 [mysqld]33 user =mysql34 basedir = /usr/local/mysql35 datadir = /usr/local/mysql/data36 port = 3306

37 pid-file = /usr/local/mysql/mysqld.pid38 socket = /usr/local/mysql/mysql.sock39 server-id = 2

40 log_bin = master-bin #添加了主服务器日志41 relay-log = relay-log-bin42 relay-log-index = slave-relay-bin.index43

44 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES'> /etc/my.cnf

45 ln -s /usr/local/mysql/bin/mysql /usr/sbin/

46 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

47 systemctl start mysqld48 netstat -anpt | grep 3306

1 从2服务器2 yum -y install ncurses-devel gcc-c++ perl-Module-Install3 tar zxvf cmake-2.8.6.tar.gz4 cd cmake-2.8.6

5 ./configure6 gmake && gmake install

7 cd8 tar zxvf mysql-5.6.36.tar.gz9 cd mysql-5.6.36

10 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc11 make && make install

12 cp support-files/my-default.cnf /etc/my.cnf13 cp support-files/mysql.server /etc/rc.d/init.d/mysqld14 chmod +x /etc/rc.d/init.d/mysqld15 chkconfig --add mysqld16 echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile17 source /etc/profile18 groupadd mysql19 useradd -M -s /sbin/nologin mysql -g mysql20 chown -R mysql.mysql /usr/local/mysql21 mkdir -p /data/mysql22 /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql23 echo '24 [client]25 port = 3306

26 socket = /usr/local/mysql/mysql.sock27

28 [mysql]29 port = 3306

30 socket = /usr/local/mysql/mysql.sock31

32 [mysqld]33 user =mysql34 basedir = /usr/local/mysql35 datadir = /usr/local/mysql/data36 port = 3306

37 pid-file = /usr/local/mysql/mysqld.pid38 socket = /usr/local/mysql/mysql.sock39 server-id = 3

40 relay-log = relay-log-bin41 relay-log-index = slave-relay-bin.index42

43 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES'> /etc/my.cnf

44 ln -s /usr/local/mysql/bin/mysql /usr/sbin/

45 ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

46 systemctl start mysqld47 netstat -anpt | grep 3306

3.3、配置 MySQL一主两从

3.3.1、MySQL 主从配置相对比较简单,需要注意的是授权。步骤如下:在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是 manager 使用。

1 mysql> grant replication slave on *.* to 'myslave'@'20.0.0.%' identified by '123';2 mysql> grant all privileges on *.* to 'mha'@'20.0.0.%' identified by 'manager';3 mysql> flush privileges;

3.3.2、在主服务器上查看二进制文件和同步点

1 mysql>show master status;2 +-------------------+----------+--------------+------------------+-------------------+

3 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

4 +-------------------+----------+--------------+------------------+-------------------+

5 | master-bin.000001 | 608 | | | |

6 +-------------------+----------+--------------+------------------+-------------------+

7 1 row in set (0.00 sec)

3.3.3、接下来在从1 和 从2 分别执行同步,查看 IO 和 SQL 线程都是 yes 代表同步是否正常

1 mysql> change master to master_host='20.0.0.10',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=608;2 mysql>start slave;3 mysql>show slave status\G;4 Slave_IO_Running: Yes5 Slave_SQL_Running: Yes

1 I/O线程显示为NO: 主库与从库网络不通、主库未授权给从库2 SQL线程显示为NO:从库日志和位置点与主不同步3 若从库查看连接主库I/0线程状态为conneting,一直是这个状态,考虑双方的防火墙是否开启。

3.3.4、必须设置两个从库为只读模式

1 mysql> set global read_only=1;

3.3.5、在主库插入两条数据,测试是否同步

1 mysql>create database test_xuhao;2 mysql>use test_xuhao;3 mysql> create table test(id int);4 mysql> insert into test(id) values (1);5 mysql> select * from test;

3.3.6、在两个从库分别查询如下所示说明主从同步正常

1 从12 mysql> select *from test_xuhao.test;3 +------+

4 | id |

5 +------+

6 | 1 |

7 +------+

8 1 row in set (0.00sec)9 从210 mysql> select *from test_xuhao.test;11 +------+

12 | id |

13 +------+

14 | 1 |

15 +------+

16 1 row in set (0.00 sec)

3.4、安装MHA软件

3.4.1、在所有服务器上都安装 MHA 依赖的环境,每个服务器上都需要两个源(epel.repo和CentOS7-Base-163.repo),将这两个源放入/etc/yum.repos.d的目录下,在主服务器上操作演示安装

1 yum install epel-release --nogpgcheck2 yum install -y perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN

3.4.2、在所有服务器上必须先安装 node 组件,最后在 MHA-manager 节点上安装 manager 组件,因为 manager 依赖 node 组件

1 tar zxvf mha4mysql-node-0.57.tar.gz2 cd mha4mysql-node-0.57

3 perlMakefile.PL4 make && make install

3.4.3、在mha-manager 服务器上安装 manager 组件

1 tar zxvf mha4mysql-manager-0.57.tar.gz2 cd mha4mysql-manager-0.57

3 perlMakefile.PL4

5 *** Module::AutoInstall version 1.06

6 *** Checking forPerl dependencies...7 [Core Features]8 - DBI ...loaded. (1.627)9 - DBD::mysql ...loaded. (4.023)10 - Time::HiRes ...loaded. (1.9725)11 - Config::Tiny ...loaded. (2.14)12 - Log::Dispatch ...loaded. (2.41)13 - Parallel::ForkManager ...loaded. (1.18)14 - MHA::NodeConst ...loaded. (0.57)15 ***Module::AutoInstall configuration finished.16 Checking ifyour kit is complete...17 Looks good18 Writing Makefile formha4mysql::manager19

20 make && make install

1 manager 安装后在/usr/local/bin 下面会生成几个工具,主要包括以下几个:2 masterha_check_ssh 检查 MHA 的 SSH 配置状况3 masterha_check_repl 检查 MySQL 复制状况4 masterha_manger 启动 manager的脚本5 masterha_check_status 检测当前 MHA 运行状态6 masterha_master_monitor 检测 master 是否宕机7 masterha_master_switch 控制故障转移(自动或者手动)8 masterha_conf_host 添加或删除配置的 server 信息9 masterha_stop 关闭manager

1 node 安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由 MHA-Manager 的脚本触发,无需人为操作)主要如下:2 save_binary_logs 保存和复制 master 的二进制日志3 apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slave4 filter_mysqlbinlog 去除不必要的 ROLLBACK 事件(MHA 已不再使用这个工具)5 purge_relay_logs 清除中继日志(不会阻塞 SQL 线程)

3.5、配置无密码认证

3.5.1、 在 manager服务器 上配置到所有节点的无密码认证

1 ssh-keygen -t rsa #一路按回车键2 ssh-copy-id 20.0.0.10 #先输入yes,再输入20.0.0.10服务器的密码3 ssh-copy-id 20.0.0.20

4 ssh-copy-id 20.0.0.30

5 验证6 ssh root@20.0.0.10

7 ssh root@20.0.0.20

8 ssh root@20.0.0.30

3.5.2、在主服务器上配置到数据库节点的无密码认证

1 ssh-keygen -t rsa2 ssh-copy-id 20.0.0.20

3 ssh-copy-id 20.0.0.30

4 验证5 ssh root@20.0.0.20

6 ssh root@20.0.0.30

3.5.3、在从1服务器上配置到数据库节点的无密码认证

1 ssh-keygen -t rsa2 ssh-copy-id 20.0.0.10

3 ssh-copy-id 20.0.0.30

4 验证5 ssh root@20.0.0.10

6 ssh root@20.0.0.30

3.5.4、在从2服务器上配置到数据库节点的无密码认证

1 ssh-keygen -t rsa2 ssh-copy-id 20.0.0.10

3 ssh-copy-id 20.0.0.20

4 验证5 ssh root@20.0.0.10

6 ssh root@20.0.0.20

3.6、 配置 MHA

3.6.1、在 manager 节点上复制相关脚本到/usr/local/bin 目录,复制上述的自动切换时 VIP 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP

1 cp -ra mha4mysql-manager-0.57/samples/scripts /usr/local/bin2 ll /usr/local/bin/scripts/

3 cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin4 vi /usr/local/bin/master_ip_failover5 删除文件里的所有内容,复制下面的内容6 #!/usr/bin/env perl7 use strict;8 use warnings FATAL => 'all';9

10 use Getopt::Long;11

12 my (13 $command, $ssh_user, $orig_master_host, $orig_master_ip,14 $orig_master_port, $new_master_host, $new_master_ip, $new_master_port15 );16 #############################添加内容部分#########################################17 my $vip = '20.0.0.200';18 my $brdc = '20.0.0.255';19 my $ifdev = 'ens33';20 my $key = '1';21 my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";22 my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";23 my $exit_code = 0;24 #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;";25 #my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";26 ##################################################################################27 GetOptions(28 'command=s' =>\$command,29 'ssh_user=s' =>\$ssh_user,30 'orig_master_host=s' =>\$orig_master_host,31 'orig_master_ip=s' =>\$orig_master_ip,32 'orig_master_port=i' =>\$orig_master_port,33 'new_master_host=s' =>\$new_master_host,34 'new_master_ip=s' =>\$new_master_ip,35 'new_master_port=i' =>\$new_master_port,36 );37

38 exit &main();39

40 sub main {41

42 print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";43

44 if ( $command eq "stop" || $command eq "stopssh") {45

46 my $exit_code = 1;47 eval {48 print "Disabling the VIP on old master: $orig_master_host \n";49 &stop_vip();50 $exit_code = 0;51 };52 if($@) {53 warn "Got Error: $@\n";54 exit $exit_code;55 }56 exit $exit_code;57 }58 elsif ( $command eq "start") {59

60 my $exit_code = 10;61 eval {62 print "Enabling the VIP - $vip on the new master - $new_master_host \n";63 &start_vip();64 $exit_code = 0;65 };66 if($@) {67 warn $@;68 exit $exit_code;69 }70 exit $exit_code;71 }72 elsif ( $command eq "status") {73 print "Checking the Status of the script.. OK \n";74 exit 0;75 }76 else{77 &usage();78 exit 1;79 }80 }81 sub start_vip() {82 `ssh $ssh_user\@$new_master_host \"$ssh_start_vip \"`;

83 }84 # A simple system call that disable the VIP on the old_master85 sub stop_vip() {86 `ssh $ssh_user\@$orig_master_host \"$ssh_stop_vip \"`;

87 }88

89 sub usage {90 print91 "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";92 }

拷贝后会有四个执行文件,文件的含义如下:

1 master_ip_failover #自动切换时 VIP 管理的脚本2 master_ip_online_change #在线切换时 vip 的管理3 power_manager #故障发生后关闭主机的脚本4 send_report #因故障切换后发送报警的脚本

3.6.2、创建 MHA 软件目录并拷贝配置文件

1 mkdir /etc/masterha2 cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/

3 vim /etc/masterha/app1.cnf4 [server default]5 manager_workdir=/var/log/masterha/app16 manager_log=/var/log/masterha/app1/manager.log7 master_binlog_dir=/usr/local/mysql/data8 master_ip_failover_script= /usr/local/bin/master_ip_failover9 master_ip_online_change_script= /usr/local/bin/master_ip_online_change10 password=manager11 user=mha12 ping_interval=1

13 remote_workdir=/tmp14 repl_password=123

15 repl_user=myslave16 secondary_check_script= /usr/local/bin/masterha_secondary_check -s 20.0.0.20 -s 20.0.0.30

17 shutdown_script=""

18 ssh_user=root19 [server1]20 hostname=20.0.0.10

21 port=3306

22 [server2]23 hostname=20.0.0.20

24 port=3306

25 candidate_master=1 #从1服务器作为主服务器的备选服务器

26 check_repl_delay=0

27 [server3]28 hostname=20.0.0.30

29 port=3306

3.6.3、测试 ssh 无密码认证,如果正常最后会输出 successfully,如下所示

1 masterha_check_ssh -conf=/etc/masterha/app1.cnf2 ......3 Wed Oct 28 00:07:09 2020 - [debug] Connecting via SSH from root@20.0.0.30(20.0.0.30:22) to root@20.0.0.20(20.0.0.20:22)..4 Wed Oct 28 00:07:10 2020 -[debug] ok.5 Wed Oct 28 00:07:10 2020 -[debug]6 Wed Oct 28 00:07:08 2020 - [debug] Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.10(20.0.0.10:22)..7 Wed Oct 28 00:07:09 2020 -[debug] ok.8 Wed Oct 28 00:07:09 2020 - [debug] Connecting via SSH from root@20.0.0.20(20.0.0.20:22) to root@20.0.0.30(20.0.0.30:22)..9 Wed Oct 28 00:07:09 2020 -[debug] ok.10 Wed Oct 28 00:07:10 2020 - [info] All SSH connection tests passed successfully.

3.6.4、测试 MySQL 主从连接情况,最后出现 MySQL Replication Health is OK 字样说明正常

1 masterha_check_repl -conf=/etc/masterha/app1.cnf2 ......3 IN SCRIPT TEST====/sbin/ifconfig ens33:1 down==/sbin/ifconfig ens33:1 20.0.0.200===

4

5 Checking the Status of the script.. OK6 Wed Oct 28 09:25:28 2020 - [info] OK.7 Wed Oct 28 09:25:28 2020 -[warning] shutdown_script is not defined.8 Wed Oct 28 09:25:28 2020 - [info] Got exit code 0(Not master dead).9

10 MySQL Replication Health is OK.

3.6.5、启动 MHA

1 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 &

3.6.6、查看 MHA 状态,可以看到当前的 master 是主服务器节点

1 masterha_check_status --conf=/etc/masterha/app1.cnf2 app1 (pid:24405) is running(0:PING_OK), master:20.0.0.10

3.6.7、查看 MHA 日志,也以看到当前的 master 是 20.0.0.10

1 cat /var/log/masterha/app1/manager.log2 ......3 Wed Oct 28 09:27:04 2020 - [info] Replicating from 20.0.0.10(20.0.0.10:3306)4 Wed Oct 28 09:27:04 2020 - [info] Current Alive Master: 20.0.0.10(20.0.0.10:3306)5 Wed Oct 28 09:27:04 2020 - [info] Checking slave configurations..6 Wed Oct 28 09:27:04 2020 - [warning] relay_log_purge=0 is not set on slave 20.0.0.20(20.0.0.20:3306).7 Wed Oct 28 09:27:04 2020 - [warning] relay_log_purge=0 is not set on slave 20.0.0.30(20.0.0.30:3306).8 Wed Oct 28 09:27:04 2020 - [warning] log-bin is not set on slave 20.0.0.30(20.0.0.30:3306). This host cannot be a master.9 ......

3.6.8、第一次配置vip的时候,需要在主服务器上创建虚拟IP地址

1 ifconfig ens33:1 20.0.0.200/24

2 ifconfig

四、故障测试

4.1、关闭主服务器的mysql服务

1 pkill mysqld

4.2、查看从1服务器的虚拟IP地址有没有转移过来,从2上查看主服务器是不是从1

1 从1服务器2 ifconfig

3 ens33: flags=4163 mtu 1500

4 inet 20.0.0.20 netmask 255.255.255.0 broadcast 20.0.0.255

5 inet6 fe80::a140:18a2:d866:8f33 prefixlen 64 scopeid 0x20

6 ether 00:0c:29:42:10:0a txqueuelen 1000(Ethernet)7 RX packets 1315993 bytes 1324408060 (1.2GiB)8 RX errors 0 dropped 0 overruns 0 frame 0

9 TX packets 795190 bytes 69519345 (66.2MiB)10 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

11

12 ens33:1: flags=4163 mtu 1500

13 inet 20.0.0.200 netmask 255.0.0.0 broadcast 20.255.255.255

14 ether 00:0c:29:42:10:0a txqueuelen 1000(Ethernet)15

16 从2服务器17 mysql18 mysql>show slave status \G19 *************************** 1. row ***************************

20 Slave_IO_State: Waiting formaster to send event21 Master_Host: 20.0.0.20

22 Master_User: myslave23 Master_Port: 3306

24 Connect_Retry: 60

25 Master_Log_File: master-bin.000003

26 Read_Master_Log_Pos: 120

27 Relay_Log_File: relay-log-bin.000002

28 Relay_Log_Pos: 284

29 Relay_Master_Log_File: master-bin.000003

30 Slave_IO_Running: Yes31 Slave_SQL_Running: Yes32 ......

4.3、重新开启manager服务器

4.3.1、主服务器开启mysql服务

1 systemctl restart mysqld

4.3.2、查看从1服务器查看二进制文件和同步点

1 mysql>show master status;2 +-------------------+----------+--------------+------------------+-------------------+

3 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

4 +-------------------+----------+--------------+------------------+-------------------+

5 | master-bin.000003 | 120 | | | |

6 +-------------------+----------+--------------+------------------+-------------------+

7 1 row in set (0.00 sec)

4.3.3、在主服务器上执行同步,查看 IO 和 SQL 线程都是 yes 代表同步是否正常

1 mysql>change master to2 master_host='20.0.0.20',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120;3 mysql>start slave;4 mysql>show slave status \G5 *************************** 1. row ***************************

6 Slave_IO_State: Waiting formaster to send event7 Master_Host: 20.0.0.20

8 Master_User: myslave9 Master_Port: 3306

10 Connect_Retry: 60

11 Master_Log_File: master-bin.000003

12 Read_Master_Log_Pos: 120

13 Relay_Log_File: mysqld-relay-bin.000002

14 Relay_Log_Pos: 284

15 Relay_Master_Log_File: master-bin.000003

16 Slave_IO_Running: Yes17 Slave_SQL_Running: Yes

18 ......

4.3.4、在从1上创建,主服务器和从2上查看

1 从1服务器2 mysql>create database aa;3 mysql>use aa;4 mysql> create table test(id int(3));5 mysql> insert into test values(1);6 mysql> select *from test;7 +------+

8 | id |

9 +------+

10 | 1 |

11 +------+

12

13 主服务器14 mysql>use aa;15 mysql> select *from test;16 +------+

17 | id |

18 +------+

19 | 1 |

20 +------+

21

22 从2服务器23 mysql>use aa;24 mysql> select *from test;25 +------+

26 | id |

27 +------+

28 | 1 |

29 +------+

4.3.5、在manager服务器上修改配置文件(再把这个记录添加进去,因为它检测掉失效时候会自动消失)

1 vi /etc/masterha/app1.cnf2 ......3 [server1]4 hostname=20.0.0.10

5 port=3306

6 ......

4.3.6、在manager服务器上启动manager

1 masterha_check_ssh -conf=/etc/masterha/app1.cnf2 masterha_check_repl -conf=/etc/masterha/app1.cnf3 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 &

4 masterha_check_status --conf=/etc/masterha/app1.cnf

总结

MHA目前在MySQL高可用方面是一个相对成熟的解决方案,在进行故障切换的过程中,MHA能最大程度上保证数据库的一致性,以达到真正意义上的高可用

1、从宕机崩溃的master保存二进制日志事件(binlog events);

2、识别含有最新更新的slave;

3、应用差异的中继日志(relay log)到其他slave;

4、应用从master保存的二进制日志事件(binlog events);

5、提升一个slave为新master;

6、使用其他的slave连接新的master进行复制。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/294821
推荐阅读
相关标签
  

闽ICP备14008679号