数据库教程:MySQL 搭建MHA架构部署的步骤

mah一:mah架构介绍 mha (master high availability)目前在mysql高可用方面是一个相对成熟的解决方案,它由日本人youshimaton开发,是一套优秀的作为mys


mah

一:mah架构介绍

  • mha (master high availability)目前在mysql高可用方面是一个相对成熟的解决方案,它由日本人youshimaton开发,是一套优秀的作为mysql高可用性环境下故障切换和主从提升的高可用软件。在mysql故障切换过程中,mha能做到0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,mha能最大程度上保证数据库的一致性,以达到真正意义上的高可用。
  • mha由两部分组成:mha manager (管理节点)和mhanode(数据节点)。mha manager可以独立部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave上。当master 出现故障是,它可以自动将最新数据的slave 提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序是完全透明的。

二:适用场景

目前mha主要支持一主多从的架构,要搭建mha,要求一个复制集群必须最少有3台数据库服务器,一主二从,即一台充当master ,一台充当备用master,另一台充当从库。出于成本考虑,淘宝在此基础上进行了改造,目前淘宝开发的 tmha 已经支持一主一从。

三:mha工作原理

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

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

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

4.应用从 master 保存的二进制日志事件;

5.提升—个 slave为新的master;

6.使其他的 slave 连接新的 master 进行复制;

四:mha的组成

  •  manager工具包
  • node工具包

1:manager工具包

  • masterha_check_ssh:检查mha的 ssh配置情况
  • masterha_check_repl:检查mysql复制状况
  • masterha_manager:启动mha
  • masterha_check_status:检测当前mha运行状态
  • masterha_master_monitor:检测master是否宕机
  • masterha_master_switch:控制故障转移(自动或手动)
  • masterha_conf_host:添加或删除配置的server 信息

2:node工具包

通常由mha manager的脚本触发,无需人工操作

  • save_binary_logs:保存和复制master 的 binlog日志
  • apply_diff_relay_logs:识别差异的中级日志时间并将其应用到其他 slave
  • filter_mysqlbinlog:去除不必要的roolback事件(已经废弃)
  • purge_relay_logs:清除中继日志(不阻塞sql线程)

五:mha 特点

  • 自动故障切换过程中,mha试图从宕机的主服务器上保存进制日志,最大程度的保证数据不丢失
  • 使用半同步复制,可以大大降低数据丢失的风险
  • 目前mha支持一主多从架构,最少三台服务,即一主两从

mha 架构部署

一:拓扑图

MySQL 搭建MHA架构部署的步骤

二:数据库安装

mysql 版本使用 5.6.36、cmake 版本使用 2.8.6

1:安装编译依赖的环境

  [root@master ~]# yum -y install ncurses-devel gcc-c++ perl-module-install

2.:安装 gmake 编译软件

  [root@master ~]# tar zxvf cmake-2.8.6.tar.gz  [root@master ~]# cd cmake-2.8.6  [root@master cmake-2.8.6]# ./configure  [root@master cmake-2.8.6]# gmake && gmake install

3:安装 mysql 数据库

  [root@master ~]# tar -zxvf mysql-5.6.36.tar.gz  [root@master ~]# cd mysql-5.6.36  [root@master mysql-5.6.36]# cmake -dcmake_install_prefix=/usr/local/mysql -ddefault_charset=utf8 -ddefault_collation=utf8_general_ci -dwith_extra_charsets=all -dsysconfdir=/etc  [root@master mysql-5.6.36]# make && make install  [root@master mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf  [root@master mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld  [root@master ~]# chmod +x /etc/rc.d/init.d/mysqld  [root@master ~]# chkconfig --add mysqld  [root@master ~]# echo "path=$path:/usr/local/mysql/bin" >> /etc/profile  [root@master ~]# source /etc/profile  chown -r mysql.mysql /usr/local/mysql groupadd mysql  [root@master ~]# useradd -m -s /sbin/nologin mysql -g mysql  [root@master ~]# chown -r mysql.mysql /usr/local/mysql  [root@master ~]# mkdir -p /data/mysql  [root@master ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

4:修改 master 的主配置文件/etc/my.cnf 文件

将原来配置全部删除

  [root@master ~]# vi /etc/my.cnf  [client]  port = 3306  socket = /usr/local/mysql/mysql.sock    [mysql]  port = 3306  socket = /usr/local/mysql/mysql.sock    [mysqld]  user = mysql  basedir = /usr/local/mysql  datadir = /usr/local/mysql/data  port = 3306  pid-file = /usr/local/mysql/mysqld.pid  socket = /usr/local/mysql/mysql.sock  server-id = 1  log_bin = master-bin  log-slave-updates = true    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

另外两台 slave 数据库

三台服务器的 server-id 不能一样,其余一样正常写入

  server-id = 2  log_bin = master-bin  relay-log = relay-log-bin   relay-log-index = slave-relay-bin.index 

  server-id = 3  log_bin = master-bin  relay-log = relay-log-bin   relay-log-index = slave-relay-bin.index 

5:三台数据库分别做两个软链接,软链接是为 hma 服务的

  [root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/  [root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/

6:三台数据库启动 mysql

  [root@master ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &  [root@master ~]# service mysqld restart   shutting down mysql.. success!   starting mysql. success!

三:数据库配置主从同步

登录数据库

  [root@master ~]# mysql

1:在所有数据库节点上授权两个用户,一个是从库同步使用,另外一个是 manager 使用

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

2:下面三条授权按理论是不用添加的,但是做案例实验环境时候通过 mha 检查mysql 主从有报错,报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权。

  mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';  mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';  mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';

3:在 master 主机上查看二进制文件和同步点

  mysql> show master status;  +-------------------+----------+--------------+------------------+-------------------+  | file    | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |  +-------------------+----------+--------------+------------------+-------------------+  | master-bin.000001 |  608 |    |     |     |  +-------------------+----------+--------------+------------------+-------------------+  1 row in set (0.00 sec)

4:在 slave1 和 slave2 分别执行同步

  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  mysql> start slave;

5:查看 io 和 sql 线程都是 yes 代表同步是否正常

  mysql> show slave statusg;  *************************** 1. row ***************************      slave_io_state: waiting for master to send event       master_host: 20.0.0.10       master_user: myslave       master_port: 3306      connect_retry: 60      master_log_file: master-bin.000001     read_master_log_pos: 608      relay_log_file: relay-log-bin.000002      relay_log_pos: 284    relay_master_log_file: master-bin.000001      slave_io_running: yes     slave_sql_running: yes      replicate_do_db:      replicate_ignore_db: 

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

  mysql> set global read_only=1;

6:在 master 主库插入两条数据,测试是否同步

  mysql> create database test_db;  query ok, 1 row affected (0.00 sec)  mysql> use test_db;  database changed  mysql> create table test(id int);  query ok, 0 rows affected (0.13 sec)  mysql> insert into test(id) values (1);  query ok, 1 row affected (0.03 sec)

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

  mysql> select * from test_db.test;  +------+  | id |  +------+  | 1 |  +------+  1 row in set (0.00 sec)

四:安装 mha 软件

1:所有服务器上都安装 mha 依赖的环境,首先安装 epel 源(3+1)

  [root@master ~]# cd /etc/yum.repos.d/  [root@master yum.repos.d]# ll  总用量 20  drwxr-xr-x. 2 root root 187 10月 10 18:08 backup  -rw-r--r--. 1 root root 1458 12月 28 23:07 centos7-base-163.repo  -rw-r--r--. 1 root root 951 12月 29 14:52 epel.repo  -rw-r--r--. 1 root root 1050 11月 1 04:33 epel.repo.rpmnew  -rw-r--r--. 1 root root 1149 11月 1 04:33 epel-testing.repo  -rw-r--r--. 1 root root 228 10月 27 18:43 local.repo

三台数据库加上一台 mha-manager

  [root@mha-manager ~]# yum install epel-release --nogpgcheck  [root@mha-manager ~]# yum install -y perl-dbd-mysql perl-config-tiny perl-log-dispatch perl-paralextutils-cbuilder perl-extutils-makemaker perl-cpan

2:在所有服务器上必须先安装 node 组件 (3+1)

  [root@mha-manager ~]# tar zxvf mha4mysql-node-0.57.tar.gz  [root@mha-manager ~]# cd mha4mysql-node-0.57  [root@mha-manager mha4mysql-node-0.57]# perl makefile.pl  [root@mha-manager mha4mysql-node-0.57]# make && make install

3:在 mha-manager 上安装 manager 组件

  [root@mha-manager ~]# tar zxvf mha4mysql-manager-0.57.tar.gz   [root@mha-manager ~]# cd mha4mysql-manager-0.57/  [root@mha-manager mha4mysql-manager-0.57]# perl makefile.pl  [root@mha-manager mha4mysql-manager-0.57]# make && make install

manager 安装后在/usr/local/bin 下面会生成几个工具

MySQL 搭建MHA架构部署的步骤

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

node 安装后也会在/usr/local/bin 下面会生成几个脚本

MySQL 搭建MHA架构部署的步骤

save_binary_logs 保存和复制 master 的二进制日志

apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的 slave

filter_mysqlbinlog 去除不必要的 rollback 事件(mha 已不再使用这个工具)

purge_relay_logs 清除中继日志(不会阻塞 sql 线程)

五:配置无密码认证

1:在 manager 上配置到所有节点的无密码认证

(1)生成密钥

  [root@mha-manager ~]# ssh-keygen -t rsa     # 一直回车

(2)生成密钥之后发送给其他3个数据库

  [root@mha-manager ~]# ssh-copy-id 20.0.0.10  # 输入:yes 密码:123456  [root@mha-manager ~]# ssh-copy-id 20.0.0.11  [root@mha-manager ~]# ssh-copy-id 20.0.0.12 

(3)登录测试

  [root@mha-manager ~]# ssh root@20.0.0.10  last login: tue dec 29 14:52:09 2020 from 20.0.0.1  [root@master ~]# exit  登出  connection to 20.0.0.10 closed.  [root@mha-manager ~]# ssh root@20.0.0.11  last login: tue dec 29 13:20:07 2020 from 20.0.0.1  [root@slave1 ~]# exit  登出  connection to 20.0.0.11 closed.  [root@mha-manager ~]# ssh root@20.0.0.12  last login: tue oct 27 19:45:24 2020 from 20.0.0.1  [root@slave2 ~]# exit  登出  connection to 20.0.0.12 closed.

2:在 master 上配置到数据库节点的无密码认证

(1)生成密钥

  [root@master ~]# ssh-keygen -t rsa

(2)生成密钥之后发送给其他2个数据库

  [root@master ~]# ssh-copy-id 20.0.0.11  [root@master ~]# ssh-copy-id 20.0.0.12

(3)登录测试

  [root@master ~]# ssh root@20.0.0.11  last login: tue dec 29 16:40:06 2020 from 20.0.0.13  [root@slave1 ~]# exit  登出  connection to 20.0.0.11 closed.  [root@master ~]# ssh root@20.0.0.12  last login: tue oct 27 23:05:20 2020 from 20.0.0.13  [root@slave2 ~]# exit  登出  connection to 20.0.0.12 closed.

3:在 slave1 上配置到数据库节点的无密码认证

(1)生成密钥

  [root@slave1 ~]# ssh-keygen -t rsa

(2)生成密钥之后发送给其他2个数据库

  [root@slave1 ~]# ssh-copy-id 20.0.0.10  [root@slave1 ~]# ssh-copy-id 20.0.0.12

(3)登录测试

  [root@slave1 ~]# ssh root@20.0.0.10  last login: tue dec 29 16:39:55 2020 from 20.0.0.13  [root@master ~]# exit  登出  connection to 20.0.0.10 closed.  [root@slave1 ~]# ssh root@20.0.0.12  last login: tue oct 27 23:14:06 2020 from 20.0.0.10  [root@slave2 ~]# exit  登出  connection to 20.0.0.12 closed.

4:在 slave2 上配置到数据库节点的无密码认证

(1)生成密钥

  [root@slave2 ~]# ssh-keygen -t rsa

(2)生成密钥之后发送给其他2个数据库

  [root@slave2 ~]# ssh-copy-id 20.0.0.10  [root@slave2 ~]# ssh-copy-id 20.0.0.11

(3)登录测试

  [root@slave2 ~]# ssh root@20.0.0.10  last login: tue dec 29 16:59:43 2020 from 20.0.0.11  [root@master ~]# exit  登出  connection to 20.0.0.10 closed.  [root@slave2 ~]# ssh root@20.0.0.11  last login: tue dec 29 16:48:51 2020 from 20.0.0.10  [root@slave1 ~]# exit  登出  connection to 20.0.0.11 closed.

六:配置 mha

1:在 manager 节点上复制相关脚本到/usr/local/bin 目录

(1)拷贝

  [root@mha-manager ~]# cp -ra /root/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/

(2)拷贝后会有四个执行文件

MySQL 搭建MHA架构部署的步骤

master_ip_failover #自动切换时 vip 管理的脚本

master_ip_online_change #在线切换时 vip 的管理

power_manager #故障发生后关闭主机的脚本

send_report #因故障切换后发送报警的脚本

(3)复制上述的自动切换时 vip 管理的脚本到/usr/local/bin 目录,这里使用脚本管理 vip

  [root@mha-manager scripts]# cp master_ip_failover /usr/local/bin/  [root@mha-manager scripts]# cd ..  [root@mha-manager bin]# ll  总用量 88

MySQL 搭建MHA架构部署的步骤

2:修改自动切换脚本

  [root@mha-manager ~]# vi /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 = '20.0.0.200';  my $brdc = '20.0.0.255';  my $ifdev = 'ens33';  my $key = '1';  my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";  my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";  my $exit_code = 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 "nnin script test====$ssh_stop_vip==$ssh_start_vip===nn";    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=portn";  }

3:创建 mha 软件目录并拷贝配置文件

  [root@mha-manager ~]# mkdir /etc/mha  [root@mha-manager ~]# cp mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/mha  [root@mha-manager ~]# vi /etc/mha/app1.cnf  [server default]  manager_workdir=/var/log/masterha/app1  manager_log=/var/log/masterha/app1/manager.log  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  user=mha  ping_interval=1  remote_workdir=/tmp  repl_password=123  repl_user=myslave  secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.11 -s 20.0.0.12  shutdown_script=""  ssh_user=root    [server1]  hostname=20.0.0.10  port=3306  [server2]  hostname=20.0.0.11  port=3306  candidate_master=1  check_repl_delay=0  [server3]  hostname=20.0.0.12  port=3306

七:健康检查

1:测试 ssh 无密码认证,如果正常最后会输出 successfully

  [root@mha-manager ~]# masterha_check_ssh  --conf=<server_config_file> must be set.  [root@mha-manager ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf  tue dec 29 20:19:16 2020 - [warning] global configuration file /etc/masterha_default.cnf not found. skipping.  tue dec 29 20:19:16 2020 - [info] reading application default configuration from /etc/mha/app1.cnf..  tue dec 29 20:19:16 2020 - [info] reading server configuration from /etc/mha/app1.cnf..  tue dec 29 20:19:16 2020 - [info] starting ssh connection tests..  tue dec 29 20:19:17 2020 - [debug]   tue dec 29 20:19:16 2020 - [debug] connecting via ssh from root@20.0.0.10(20.0.0.10:22) to root@20.0.0.11(20.0.0.11:22)..  tue dec 29 20:19:16 2020 - [debug] ok.  tue dec 29 20:19:16 2020 - [debug] connecting via ssh from root@20.0.0.10(20.0.0.10:22) to root@20.0.0.12(20.0.0.12:22)..  tue dec 29 20:19:17 2020 - [debug] ok.  tue dec 29 20:19:18 2020 - [debug]   tue dec 29 20:19:17 2020 - [debug] connecting via ssh from root@20.0.0.12(20.0.0.12:22) to root@20.0.0.10(20.0.0.10:22)..  tue dec 29 20:19:17 2020 - [debug] ok.  tue dec 29 20:19:17 2020 - [debug] connecting via ssh from root@20.0.0.12(20.0.0.12:22) to root@20.0.0.11(20.0.0.11:22)..  tue dec 29 20:19:18 2020 - [debug] ok.  tue dec 29 20:19:18 2020 - [debug]   tue dec 29 20:19:16 2020 - [debug] connecting via ssh from root@20.0.0.11(20.0.0.11:22) to root@20.0.0.10(20.0.0.10:22)..  tue dec 29 20:19:17 2020 - [debug] ok.  tue dec 29 20:19:17 2020 - [debug] connecting via ssh from root@20.0.0.11(20.0.0.11:22) to root@20.0.0.12(20.0.0.12:22)..  tue dec 29 20:19:17 2020 - [debug] ok.  tue dec 29 20:19:18 2020 - [info] all ssh connection tests passed successfully.

2:测试 mysql 主从连接情况,最后出现 mysql replication health is ok 字样说明

  [root@mha-manager ~]# masterha_check_repl --conf=/etc/mha/app1.cnf  tue dec 29 20:30:29 2020 - [warning] global configuration file /etc/masterha_default.cnf not found. skipping.  tue dec 29 20:30:29 2020 - [info] reading application default configuration from /etc/mha/app1.cnf..  tue dec 29 20:30:29 2020 - [info] reading server configuration from /etc/mha/app1.cnf..  tue dec 29 20:30:29 2020 - [info] mha::mastermonitor version 0.57.  tue dec 29 20:30:30 2020 - [info] gtid failover mode = 0  tue dec 29 20:30:30 2020 - [info] dead servers:  tue dec 29 20:30:30 2020 - [info] alive servers:  tue dec 29 20:30:30 2020 - [info] 20.0.0.10(20.0.0.10:3306)  tue dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306)  tue dec 29 20:30:30 2020 - [info] 20.0.0.12(20.0.0.12:3306)  tue dec 29 20:30:30 2020 - [info] alive slaves:  tue dec 29 20:30:30 2020 - [info] 20.0.0.11(20.0.0.11:3306) version=5.6.36-log (oldest major version between slaves) log-bin:enabled  .......省略  checking the status of the script.. ok   tue dec 29 20:30:55 2020 - [info] ok.  tue dec 29 20:30:55 2020 - [warning] shutdown_script is not defined.  tue dec 29 20:30:55 2020 - [info] got exit code 0 (not master dead).    mysql replication health is ok.

八:查看 master1 的 vip 地址

查看 20.0.0.200 是否存在

这个 vip 地址不会因为manager 节点停止 mha 服务而消失

第一次启动mha,主库上并不会主动的生成vip地址,需要手动开启

  [root@master ~]# ifconfig ens33:1 20.0.0.200/24 up  [root@master ~]# ip addr  2: ens33: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000   link/ether 00:0c:29:8d:e2:af brd ff:ff:ff:ff:ff:ff   inet 20.0.0.10/24 brd 20.0.0.255 scope global ens33    valid_lft forever preferred_lft forever   inet 20.0.0.200/24 brd 20.0.0.255 scope global secondary ens33:1    valid_lft forever preferred_lft forever   inet6 fe80::a6c1:f3d4:160:102a/64 scope link     valid_lft forever preferred_lft forever

九:启动 mha 并查看状态

  [root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &  [1] 57152  [root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf  app1 (pid:57152) is running(0:ping_ok), master:20.0.0.10

故障模拟与修复

一:故障模拟

1:将 master 主服务器 down 掉

  [root@master ~]# pkill mysqld

2:查看日志信息

  [root@mha-manager ~]# cat /var/log/masterha/app1/manager.log    master 20.0.0.10(20.0.0.10:3306) is down!  # 20.0.0.10 以停掉    check mha manager logs at mha-manager:/var/log/masterha/app1/manager.log for details.    started automated(non-interactive) failover.  invalidated master ip address on 20.0.0.10(20.0.0.10:3306)  the latest slave 20.0.0.11(20.0.0.11:3306) has all relay logs for recovery.  selected 20.0.0.11(20.0.0.11:3306) as a new master.  # 20.0.0.11 成为主服务器  20.0.0.11(20.0.0.11:3306): ok: applying all logs succeeded.  20.0.0.11(20.0.0.11:3306): ok: activated master ip address.  20.0.0.12(20.0.0.12:3306): this host has the latest relay log events.  generating relay diff files from the latest slave succeeded.

3:查看虚拟地址

虚拟地址已到 20.0.0.11 上面

  [root@slave1 ~]# ip addr  2: ens33: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000   link/ether 00:0c:29:49:77:39 brd ff:ff:ff:ff:ff:ff   inet 20.0.0.11/24 brd 20.0.0.255 scope global ens33    valid_lft forever preferred_lft forever   inet 20.0.0.200/8 brd 20.255.255.255 scope global ens33:1    valid_lft forever preferred_lft forever   inet6 fe80::5cbb:1621:4281:3b24/64 scope link     valid_lft forever preferred_lft forever

4:查看主从状态

查看主服务器的二进制文件

  [root@slave1 ~]# mysql    mysql> show master status;  +-------------------+----------+--------------+------------------+-------------------+  | file    | position | binlog_do_db | binlog_ignore_db | executed_gtid_set |  +-------------------+----------+--------------+------------------+-------------------+  | master-bin.000003 |  120 |    |     |     |  +-------------------+----------+--------------+------------------+-------------------+  1 row in set (0.00 sec)

查看从2的状态

  [root@slave2 ~]# mysql    mysql> show slave statusg;  *************************** 1. row ***************************      slave_io_state: waiting for master to send event       master_host: 20.0.0.11       master_user: myslave       master_port: 3306      connect_retry: 60      master_log_file: master-bin.000003     read_master_log_pos: 120      relay_log_file: relay-log-bin.000002      relay_log_pos: 284    relay_master_log_file: master-bin.000003      slave_io_running: yes     slave_sql_running: yes      replicate_do_db:      replicate_ignore_db: 

二:故障修复

1:开启 down 掉的数据库

  [root@master ~]# systemctl start mysqld  [root@master ~]# systemctl status mysqld  ● mysqld.service - lsb: start and stop mysql   loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)   active: active (running) since 二 2020-12-29 21:50:03 cst; 25s ago    docs: man:systemd-sysv-generator(8)   process: 977 execstart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/success)   cgroup: /system.slice/mysqld.service     ├─1026 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-fi...     └─1358 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/m

2:down 掉的数据库进行主从复制

主从复制

  [root@master ~]# mysql    mysql> change master to master_host='20.0.0.11',master_user='myslave',master_password='123',master_log_file='master-bin.000003',master_log_pos=120;   query ok, 0 rows affected, 2 warnings (0.01 sec)  # 20.0.0.11 是主服务器 down 掉之后成为的主服务器    mysql> start slave;  query ok, 0 rows affected (0.01 sec)

查看状态

  mysql> show slave statusg;  *************************** 1. row ***************************      slave_io_state: waiting for master to send event       master_host: 20.0.0.11       master_user: myslave       master_port: 3306      connect_retry: 60      master_log_file: master-bin.000003     read_master_log_pos: 120      relay_log_file: mysqld-relay-bin.000002      relay_log_pos: 284    relay_master_log_file: master-bin.000003      slave_io_running: yes     slave_sql_running: yes      replicate_do_db:      replicate_ignore_db: 

3:修改 mha 配置文件

  [root@mha-manager ~]# vi /etc/mha/app1.cnf    secondary_check_script=/usr/local/bin/masterha_secondary_check -s 20.0.0.10 -s 20.0.0.12  # 由于 20.0.0.11 成为主服务器,所以要添加 20.0.0.10 和 20.0.0.12 为从服务器  [server1]  hostname=20.0.0.10  candidate_master=1  check_repl_delay=0  port=3306  [server2]  hostname=20.0.0.11  port=3306  # 由于 20.0.0.10 down 掉,所以 server1 文件自动删除,重新添加 server1,并设为备选主服务器,server2 修改

4:进入数据库重新授权

  [root@master ~]# mysql    mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';  query ok, 0 rows affected (0.00 sec)    mysql> flush privileges;  query ok, 0 rows affected (0.00 sec)

5:再次启动 mha

  [root@mha-manager ~]# nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &  [1] 58927  [root@mha-manager ~]# masterha_check_status --conf=/etc/mha/app1.cnf  app1 (pid:58927) is running(0:ping_ok), master:20.0.0.11

6:再次查看日志

  [root@mha-manager ~]# cat /var/log/masterha/app1/manager.log  ......  tue dec 29 22:16:53 2020 - [info] dead servers:  # 停掉的服务  tue dec 29 22:16:53 2020 - [info] alive servers:  # 存活的服务  tue dec 29 22:16:53 2020 - [info] 20.0.0.10(20.0.0.10:3306)  tue dec 29 22:16:53 2020 - [info] 20.0.0.11(20.0.0.11:3306)  tue dec 29 22:16:53 2020 - [info] 20.0.0.12(20.0.0.12:3306)  .......

7:主数据库写入数据同步并查看

其他数据库都可以查到

  mysql> create database ooo;  query ok, 1 row affected (0.00 sec)    mysql> show databases;  +--------------------+  | database   |  +--------------------+  | information_schema |  | mysql    |  | ooo    |  | performance_schema |  | test    |  | test_db   |  +--------------------+  6 rows in set (0.00 sec)

到此这篇关于mysql 搭建mha架构部署的步骤的文章就介绍到这了,更多相关mysql 搭建mha架构部署内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:MySQL 搭建MHA架构部署的步骤,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/809739.html

(0)
上一篇 2021年9月11日
下一篇 2021年9月11日

精彩推荐