MHA数据库集群部署

目录

 

一、MHA概述

二、部署MHA案例

三、测试集群环境

四、测试高可用

五、故障恢复


一、MHA概述

1.MHA简介

MHA(Master High Availability)是由日本DeNA公司的youshimaton开发的,是一条优秀的实现Mysql高可用的解决方案。它能在0-30秒内完成自动故障切换。MHA可以确保在故障切换过程中最大限度保证数据的一致性,已达到真正意义上的高可用。

 

2.MHA原理

一主三从架构,主库挂了,但主库能被从库ssh上去的情况下,MHA从三个从库中选择同步最接近的作为新主,然后新主和s2(slave2从2),s3都ssh到原主上通过binlog补上还没有同步的数据,io_thread读取到binlog位置,传到save_binary_logs(存储二进制日志),然后回放,达到s1,s2,s3和原主一致。

mysql5.6之后,如果有一主三从的架构,使用GTID复制,keepalived+一主两从,并且其中一个从库使用增强半同步复制,这样完全可以取代MHA,这就是为什么MHA代码为什么不更新,out的原因

3.MHA组成

由两个部分组成,一个数据管理节点一个数据节点。

MHA Manager(管理节点)

- 管理所有数据库服务器

- 可以单数部署在一台独立的机器上

- 也可以部署在某台数据库服务器上(这样做会有一定风险,且高耦合)

MHA Node(数据节点)

- 存储数据的MySQL服务器

- 运行在每台Mysql服务器上

 

4.MHA管理集群命令

命令作用
masterha_check_ssh检查MHA的SSH配置状况
masterha_check_repl检查MySQl复制状况
master_manager启动MHA
masterha_check_status检测MHA运行状态
master_stop停止MHA

 

5.MHA集群架构

通过一个节点管理三个数据库集群,每个集群有一台主服务器两台从服务器,当主机宕机后会自动将从服务器升级为主服务器

 

二、部署MHA案例

1.拓扑图与ip规划

一台管理主机,3台数据库服务器,1台客户端

ip规划

2.数据库服务器之间实现ssh无密访问

 mha要求一个集群中的服务器可以实现互相访问

1)配置数据库服务器192.168.4.51

[root@host51 ~]# ssh-keygen //创建秘钥对

[root@host51 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机.自己也需要传

[root@host51 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host52主机

[root@host51 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机

[root@host51 ~]# ssh root@192.168.4.52 //可以无密码连接52主机

[root@host52 ~]# exit //断开连接

登出

Connection to 192.168.4.52 closed.

[root@host51 ~]# ssh root@192.168.4.53 //可以无密码连接52主机

[root@host53 ~]# exit//断开连接

登出

Connection to 192.168.4.53 closed.

2)配置数据库服务器192.168.4.52

[root@host52 ~]# ssh-keygen //创建秘钥对

[root@host52 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机

[root@host51 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host51主机.自己也需要传

[root@host52 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机

[root@host51 ~]# exit //断开连接

登出

Connection to 192.168.4.52 closed.

[root@host52 ~]# ssh root@192.168.4.53 //可以无密码连接53主机

[root@host53 ~]# exit//断开连接

登出

Connection to 192.168.4.53 closed.

3)配置数据库服务器192.168.4.53

[root@host53 ~]# ssh-keygen //创建秘钥对

[root@host53 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机

[root@host53 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host52主机

[root@host51 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机.自己也需要传

[root@host53 ~]# ssh root@192.168.4.51 //可以无密码连接51主机

[root@host51 ~]# exit //断开连接

登出

Connection to 192.168.4.51 closed.

[root@host53 ~]# ssh root@192.168.4.52 //可以无密码连接52主机

[root@host52 ~]# exit//断开连接

登出

Connection to 192.168.4.52 closed.

4)配置管理服务器服务器192.168.4.57

[root@mgm57 ~]# ssh-keygen //创建秘钥对

[root@mgm57 ~]# ssh-copy-id root@192.168.4.51 //传递公钥给host51主机

[root@mgm57 ~]# ssh-copy-id root@192.168.4.52 //传递公钥给host52主机

[root@mgm57 ~]# ssh-copy-id root@192.168.4.53 //传递公钥给host53主机

[root@mgm57 ~]# ssh root@192.168.4.51 //可以无密码连接51主机

[root@host51 ~]# exit //断开连接

登出

Connection to 192.168.4.51 closed.

[root@host53 ~]# ssh root@192.168.4.52 //可以无密码连接52主机

[root@host52 ~]# exit//断开连接

登出

Connection to 192.168.4.52 closed.

 

3.安装依赖包

 Perl 是 Practical Extraction and Report Language 的缩写,可翻译为 "实用报表提取语言"。

MHA需要Perl语言支持【Perl官网 http://www.perl.org/get.html

1)配置数据库服务器192.168.4.51

1)配置数据库服务器192.168.4.51

[root@host51 ~]# tar -xvf mha-soft-student.tar.gz

[root@host51 ~]# cd mha-soft-student

[root@host51 ~]# yum –y install perl-* //安装共享的perl软件包

2)配置数据库服务器192.168.4.52

[root@host52 ~]# yum -y install perl-* //安装系统自带的perl软件包

[root@host52 ~]# cd mha-soft-student

3)配置数据库服务器192.168.4.53

[root@host51 ~]# tar -xvf mha-soft-student.tar.gz

[root@host53 ~]# cd mha-soft-student

[root@host53 ~]# yum –y install perl-* //安装共享的perl软件包

4)配置管理服务器192.168.4.57

[root@host51 ~]# tar -xvf mha-soft-student.tar.gz

[root@mgm57 ~]# cd mha-soft-student

[root@host53 ~]# yum –y install perl-* //安装共享的perl软件包

3. 配置MySQL一主多从结构

 主服务器开启binlog日志,从服务器定义主服务器信息,实现主从同步

1)配置主服务器192.168.4.51

  1. [root@host51 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. log-bin=master51 //日志名
  4. server_id=51 //指定server_id
  5. :wq
  6. [root@host51 ~]# systemctl restart mysqld
  7. [root@host51 ~]# mysql -uroot -p123qqq…A
  8.     mysql> grant replication slave on *.* to repluser@"%" identified by “123qqq…A"; //添加从服务器同步数据连接用户
  9. mysql> show master status; //查看日志信息
  10. mysql: [Warning] Using a password on the command line interface can be insecure.
  11. +-----------------+----------+--------------+------------------+-------------------+
  12. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  13. +-----------------+----------+--------------+------------------+-------------------+
  14. | master51.000001 | 441 | | | |
  15. +-----------------+----------+--------------+------------------+-------------------+

 

2)配置从服务器192.168.4.52

[root@host52 ~]# vim /etc/my.cnf

[mysqld]

server_id=52 //指定server_id

:wq

[root@host52 ~]# systemctl restart mysqld //重启数据库服务

[root@host52 ~]# mysql -uroot –p123qqq…A //数据库管理员登录

mysql> change master to //指定主服务器信息

master_host="192.168.4.51", //IP地址

master_user="repluser", //授权用户

master_password="123qqq...A", //授权用户密码

master_log_file="master51.000001", //binlog日志

master_log_pos=441; //偏移量

mysql> start slave; //启动slave进程

mysql> exit ; //断开连接

[root@host52 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep 192.168.4.51

Master_Host: 192.168.4.51 //主服务器Ip地址

[root@host52 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep –i yes

Slave_IO_Running: Yes //I0线程正常

Slave_SQL_Running: Yes //SQL线程正常

3)配置从服务器192.168.4.53

[root@host53 ~]# vim /etc/my.cnf

[mysqld]

server_id=53 //指定server_id

:wq

[root@host53 ~]# systemctl restart mysqld //重启数据库服务

[root@host53 ~]# mysql -uroot –p123qqq…A //数据库管理员登录

mysql> change master to //指定主服务器信息

master_host="192.168.4.51", //IP地址

master_user="repluser", //授权用户

master_password="123qqq...A", //授权用户密码

master_log_file="master51.000001", //binlog日志

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep 192.168.4.51

Master_Host: 192.168.4.51 //主服务器Ip地址

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep –i yes

Slave_IO_Running: Yes //I0线程正常

Slave_SQL_Running: Yes //SQL线程正常

master_log_pos=441; //偏移量

mysql> start slave; //启动slave进程

mysql> exit ; //断开连接

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep 192.168.4.51

Master_Host: 192.168.4.51 //主服务器Ip地址

[root@host53 ~]# mysql -uroot –p123qqq…A –e “show slave status\G” | grep –i yes

Slave_IO_Running: Yes //I0线程正常

Slave_SQL_Running: Yes //SQL线程正常

 

4.配置管理服务器

1)安装软件

[MHA管理软件下载地址]https://directory.fsf.org/wiki/Mha4mysql-manager

  1. [root@mgm57 ~] tar -xf mha4mysql-manager-0.55.tar.gz
  2. [root@mgm57 ~]# cd mha-soft-student/
  3. [root@mgm57 mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm//安装mha-node软件包
  4. 准备中... ################################# [100%]
  5. 正在升级/安装...
  6. 1:mha4mysql-node-0.56-0.el6 ################################# [100%]
  7.  
  8. [root@mgm57 mha-soft-student]# rpm -qa | grep mha //查看是否安装成功
  9. mha4mysql-node-0.56-0.el6.noarch
  10. [root@mgm57 mha-soft-student]#
  11.  
  12. [root@mgm57 mha-soft-student]# tar -zxvf mha4mysql-manager-0.56.tar.gz //解压mha-manager软件包
  13. [root@mgm57 mha-soft-student]# ls
  14. app1.cnf mha4mysql-manager-0.56
  15. mha4mysql-node-0.56-0.el6.noarch.rpm
  16. master_ip_failover mha4mysql-manager-0.56.tar.gz
  17.  
  18. [root@mgm57 mha-soft-student]# cd mha4mysql-manager-0.56 //进入源码目录
  19. [root@mgm57 mha4mysql-manager-0.56]# ls //查看文件列表
  20. AUTHORS COPYING inc Makefile.PL META.yml rpm t
  21. bin debian lib MANIFEST README samples tests
  22. [root@mgm57 mha4mysql-manager-0.56]#
  23.  
  24. [root@mgm57 mha4mysql-manager-0.56]# perl Makefile.PL  //使用perl语言配置
  25. *** Module::AutoInstall version 1.03
  26. *** Checking for Perl dependencies...
  27. [Core Features]
  28. - DBI ...loaded. (1.627)
  29. - DBD::mysql ...loaded. (4.023)
  30. - Time::HiRes ...loaded. (1.9725)
  31. - Config::Tiny ...loaded. (2.14)
  32. - Log::Dispatch ...loaded. (2.41)
  33. - Parallel::ForkManager ...loaded. (1.18)
  34. - MHA::NodeConst ...loaded. (0.56)
  35. *** Module::AutoInstall configuration finished.
  36. Checking if your kit is complete...
  37. Looks good
  38. Writing Makefile for mha4mysql::manager
  39. Writing MYMETA.yml and MYMETA.json
  40. [root@mgm57 mha4mysql-manager-0.56]# make //编译
  41. [root@mgm57 mha4mysql-manager-0.56]# make install //安装
  42.  
  43. [root@mgm57 mha4mysql-manager-0.56]# ls /root/perl5/bin //查看安装的命令
  44. masterha_check_repl masterha_conf_host masterha_master_switch
  45. masterha_check_ssh masterha_manager masterha_secondary_check
  46. masterha_check_status masterha_master_monitor masterha_stop

2)编辑主配置文件

  1. [root@mgm57 ~ ]# mkdir /etc/mha //创建工作目录
  2. [root@mgm57 ~ ]# cp mha4mysql-manager-0.56/sample/conf/app1.cnf /etc/mha/ //拷贝模板文件,防止配置出错
  3. [root@mgm57 ~ ]# vim /etc/mha/app1.cnf //编辑主配置文件
  4. [server default]               //管理服务默认配置
  5. manager_workdir=/etc/mha         //工作目录
  6. manager_log=/etc/mha/manager.log     //日志文件
  7. master_ip_failover_script=/etc/mha/master_ip_failover //故障切换脚本
  8. ssh_user=root               //访问ssh服务用户
  9. ssh_port=22               //ssh服务端口
  10.  
  11. repl_user=repluser              //主服务器数据同步授权用户
  12. repl_password=123qqq…A       //密码
  13.  
  14. user=root                     //监控用户
  15. password=123qqq…A            //密码
  16.      [server1]            //指定第1台数据库服务器
  17. hostname=192.168.4.51       //服务器ip地址
  18. port=3306                      //服务端口
  19. candidate_master=1              //竞选主服务器
  20.  
  21. [server2]          //指定第2台数据库服务器
  22. hostname=192.168.4.52
  23. port=3306
  24. candidate_master=1
  25.             
  26. [server3]         //指定第3台数据库服务器
  27. hostname=192.168.4.53
  28. port=3306
  29. candidate_master=1
  30. :wq

3)创建故障切换脚本

 之前我们配置了ssh无密访问,就是为这里做铺垫,因为我们做集群需要为集群配置一个vip地址,但是会了防止ip冲突我们要求一个时刻只有台主机使用这个vip地址,所以我们使用一个脚本帮助我们切换vip,在主服务器宕机时自动将vip地址配置到新主服务器上

  1. [root@mgm57 ~]# cp mha-soft-student/master_ip_failover /etc/mha/
  2.  
  3. [root@mgm57 ~]# vim +35 /etc/mha/master_ip_failover
  4. my $vip = '192.168.4.100/24';              # Virtual IP //定义VIP地址
  5. my $key = "1";                     //定义变量$key
  6. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";     //部署vip地址命令
  7. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";    //释放vip地址命令
  8. :wq
  9. [root@mgm57 ~]# chmod +x /etc/mha/master_ip_failover //给脚本加执行权限

4)在当前主服务器部署vip地址

  1. [root@host51 ~]# ifconfig eth0:1 //部署之前查看
  2. eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  3. ether 52:54:00:d8:10:d7 txqueuelen 1000 (Ethernet)
  4.  
  5. [root@host51 ~]# ifconfig eth0:1 192.168.4.100 //部署vip地址
  6.  
  7. [root@host51 ~]# ifconfig eth0:1 //部署后查看
  8. eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  9. inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255
  10. ether 52:54:00:d8:10:d7 txqueuelen 1000 (Ethernet)

5.配置数据节点服务器

1)在所有数据库服务器上,安装mha-node软件包

  1. ]# cd /root/mha-soft-student/
  2. ]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
  3. 准备中... ################################# [100%]
  4. 正在升级/安装...
  5. 1:mha4mysql-node-0.56-0.el6 ################################# [100%]

2)在所有数据服务器上添加监控用户

可以只在host51主机执行授权命令,host52和host53 会自动同步授权

  1. ]# mysql –uroot –p密码
  2. mysql> grant all on *.* to root@"%" identified by "123qqq...A";
  3. mysql> exit;

3)修改数据库服务运行参数

修改主服务器host51,host52,host53,启动半同步复制,每台都要修改这里只写了一台

为什么要开启半同步复制?因为当主服务器宕机后,从服务器会竞选主服务器,成为主服务器的条件是开启log-bin

修改主服务器host51,host52,host53,启动半同步复制,每台都要修改这里只写了一台

  1. [root@host51 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. log-bin=master1
  4. plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" //加载模块
  5. rpl_semi_sync_master_enabled=1 //启用master模块
  6. rpl_semi_sync_slave_enabled=1 //启用slave模块
  7. relay_log_purge=0 //禁止自动删除中继日志文件
  8. :wq
  9. [root@host51 ~]# systemctl restart mysqld         #重启服务 每台主机都需要执行

 

三、测试集群环境

1.在管理主机,测试ssh配置

提示All SSH connection tests passed successfully即成功

  1. [root@mgm57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf //执行测试命令
  2. ....................
  3. Thu Jun 20 15:33:51 2019 - [info] All SSH connection tests passed successfully.//测试成功提示

2.在管理主机,测试主从同步

出现MySQL Replication Health is OK.即代表主从同步配置成功,如果有某台服务器停止,查看 Dead Servers这一栏

  1. [root@mgm57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf //执行测试命令
  2. ...............................
  3. Thu Jun 20 15:37:47 2019 - [info] Dead Servers:               //没有停止的mysql服务器
  4. .............................
  5. MySQL Replication Health is OK.                             //测试成功提示信息

3.启动管理服务

mha启动命令比较特别,--conf指定配置文件 --remove_dead_master_conf移除宕机的主机 --ignore_last_failover 忽视故障机

注意:这个命令会占用当前终端.不要关闭这个终端否则mha监控会停止

  1. [root@mgm57 ~]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \
  2. --ignore_last_failover //执行启动命令

有一个方法可以将进程放到后台执行

masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null >  /etc/mha/manager.log 2>&1 &

4.查看服务状态

  1. [root@mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf//执行命令
  2. app1 (pid:15806) is running(0:PING_OK), master:192.168.4.51 //服务运行,监视主服务器192.168.4.51

5.测试主从同步

在主服务器51 添加访问数据的连接用户,使用客户端测试从服务器中是否存在数据,测试客户端是否能通过vip访问服务器。如果在数据库主服务器上插入数据,在从服务器上可以获得同步,说明主从同步成功。

1)在主服务器51 添加访问数据的连接用户

  1. ]# mysql -uroot -p123qqq...A
  2. mysql> create database db9;
  3. Query OK, 1 row affected (0.05 sec)
  4.  
  5. mysql> create table db9.a (id int);
  6. Query OK, 0 rows affected (0.63 sec)
  7.  
  8. mysql> grant select,insert on db9.* to yaya55@"%" identified by "123qqq...A";
  9. Query OK, 0 rows affected, 1 warning (0.08 sec)
  10. mysql>exit

2)客户端50 连接vip地址访问集群

  1. host50~]# mysql -h192.168.4.100 -uyaya55 -p123qqq...A
  2. mysql> select * from db9.a;
  3. mysql> insert into db9.a values(100);
  4. mysql> select * from db9.a;
  5. +------+
  6. | id |
  7. +------+
  8. | 100 |
  9. +------+
  10. 1 row in set (0.00 sec)
  11. mysql>exit

3)在从服务器host52 查看数据

  1. [root@host52 ~]# mysql -uroot -p123qqq...A -e "select * from db9.a"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------+
  4. | id |
  5. +------+
  6. | 100 |
  7. +------+

4)在从服务器host53 查看数据

  1. [root@host53 ~]# mysql -uroot -p123qqq...A -e "select * from db9.a"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +------+
  4. | id |
  5. +------+
  6. | 100 |
  7. +------+

 

四、测试高可用

 上述演示了测试集群环境是否部署成功以及是否实现主从同步,下面测试是否实现了高可用,确保主服务器宕机,从服务器能自动升级成主服务器

1.停止主服务器51的mysql服务

[root@host51 ~]# systemctl stop mysqld

2.查看管理服务 ,输出的监控信息

监控到主服务器宕机 管理服务自动停止

[root@mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf

app1 is stopped(2:NOT_RUNNING).   

执行命令,移除失效的服务器

[root@mgm57~]#masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \

> --ignore_last_failover

Thu Jun 20 17:05:58 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Thu Jun 20 17:05:58 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..

Thu Jun 20 17:05:58 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..

Creating /var/tmp if not exists.. ok.

Checking output directory is accessible or not..

ok.

Binlog found at /var/lib/mysql, up to master51.000002

Thu Jun 20 17:35:59 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Thu Jun 20 17:35:59 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..

Thu Jun 20 17:35:59 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..

3)客户端依然连接vip地址,可以访问到数据

 说明故障修改脚本起到了作用,自动在新主服务器上配置了vip

  1. client50]# ping -c 2 192.168.4.100 //能够ping通vip地址
  2. PING 192.168.4.100 (192.168.4.100) 56(84) bytes of data.
  3. 64 bytes from 192.168.4.100: icmp_seq=1 ttl=255 time=0.222 ms
  4. 64 bytes from 192.168.4.100: icmp_seq=2 ttl=255 time=0.121 ms
  5.  
  6. --- 192.168.4.71 ping statistics ---
  7. 2 packets transmitted, 2 received, 0% packet loss, time 999ms
  8. rtt min/avg/max/mdev = 0.121/0.171/0.222/0.052 ms
  9.  
  10. client50]# mysql -h192.168.4.100 -uyaya55 -p123qqq...A //连接vip地址
  11. mysql> insert into db9.a values(200); //插入记录
  12. mysql> select * from db9.a;//查询记录
  13. +------+
  14. | id |
  15. +------+
  16. | 100 |
  17. | 200 |
  18. +------+

4)查看vip地址

在host52主机查看到vip地址,说明host52 主机被选举为主服务器,在host53主机未查看到vip地址,说明host53主机是当前host52的从服务器。

[root@host52 ~]# ifconfig eth0:1

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.4.100 netmask 255.255.255.0 broadcast 192.168.4.255

ether 52:54:00:f5:c4:6a txqueuelen 1000 (Ethernet)

 

五、故障恢复

上述演示了MHA的高可用特性,将一台主服务器宕机后从服务器升级为主服务器,这里演示如何将宕机的服务器从新添加到集群中去

1)配置数据库服务器

启动host51主机的数据库服务

[root@host51 ~]#systemctl start mysqld

2) 恢复数据

在被竞选为新的主数据库服务器上做完全备份(根据实际情况,也可以选择差异或增量备份),将数据发送给宕机的数据库服务器器。

[root@host52 ~]# mysqldump -uroot -p123qqq...A --master-data db9 > db9.sql //在主服务器host52 做完全备份

mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@host52 ~]# scp db9.sql root@192.168.4.51:/root/ //拷贝备份文件给host51主机

db9.sql 100% 1918 3.1MB/s 00:00

重启mysql服务后,使用备份文件恢复数据

[root@host51 ~]# mysql -uroot -p123qqq...A db9 < /root/db9.sql//host51 

mysql: [Warning] Using a password on the command line interface can be insecure.

3)指定主服务器信息,启动slave进程

  1. [root@host51 ~]# grep master52 /root/db9.sql //查看日志名及偏移量
  2. CHANGE MASTER TO MASTER_LOG_FILE='master52.000001', MASTER_LOG_POS=895;
  3.  
  4. [root@host51 ~]# mysql -uroot -p123qqq...A
  5. mysql>change master to master_host="192.168.4.52",master_user="repluser",master_password="123qqq...A",master_log_file="master52.000001",master_log_pos=895;
  6. Query OK, 0 rows affected, 2 warnings (0.14 sec)
  7. mysql> start slave;
  8. Query OK, 0 rows affected (0.01 sec)
  9. Mysql> exit ;

查看状态信息

  1. [root@host51 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" |grep 192.168.4.52
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Master_Host: 192.168.4.52 //主服务器ip地址
  4.  
  5. [root@host51 ~]#
  6. [root@host51 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" |grep -i yes
  7. mysql: [Warning] Using a password on the command line interface can be insecure.
  8. Slave_IO_Running: Yes //IO线程状态正常
  9. Slave_SQL_Running: Yes //SQL线程状态正常
  10. [root@host51 ~]#

 

4)配置管理服务器

修改配置文件,添加数据库服务器host51,某台服务器宕机后配置文件中会自动删除掉那台服务器的配置信息

  1. ]# vim /etc/mha/app1.cnf
  2. [server1 ]
  3. hostname=192.168.4.51
  4. port=3306
  5. candidate_master=1
  6. :wq

测试集群环境

  1. [root@mgm57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf //测试SSH
  2. MySQL Replication Health is OK. //成功

重启管理服务

  1. ]# masterha_stop --conf=/etc/mha/app1.cnf //停止管理服务
  2. Stopped app1 successfully.
  3.  
  4. ]# masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf \
  5. --ignore_last_failover //启动管理服务
  6.  
  7. Thu Jun 20 17:05:58 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
  8. Thu Jun 20 17:05:58 2019 - [info] Reading application default configuration from /etc/mha/app1.cnf..
  9. Thu Jun 20 17:05:58 2019 - [info] Reading server configuration from /etc/mha/app1.cnf..

查看状态

  1. mgm57 ~]# masterha_check_status --conf=/etc/mha/app1.cnf
  2. app1 (pid:15806) is running(0:PING_OK), master:192.168.4.52 //服务运行,监视服务器52
  3. [root@mgm57 ~]#

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值