MHA集群

MHA简介

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,该工具仅适用于MySQL Replication 环境,目的在于维持master主库的高可用性。MHA 是自动的master 故障转移和slave提升的软件包,基于标准的MySQL复制(异步/半同步)。
MHA由两部分组成:MHA Manager(管理节点)和 MHA Node(数据节点)。

  • MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。
  • MHA Node运行在每台MySQL服务器上。

MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

MHA工作原理总结为如下

(1)从宕机崩溃的master保存二进制日志事件(binlog events);
(2)识别含有最新更新的slave;
(3)apply差异的中继日志(relay log)到其他的slave;
(4)apply从master保存的二进制日志事件(binlog events);
(5)提升一个slave为新的master;
(6)使其他的slave连接新的master进行复制。

准备MHA集群环境

准备5台虚拟机,角色规划如图所示。
在这里插入图片描述
步骤一: 配置ssh免密登录
1)配置数据库服务器192.168.4.51(52,53主机也做)

[root@host51 ~]# ssh-keygen   -f /root/.ssh/id_rsa    -N ''
[root@host51 ~]# for i in 52 53 
 do
     ssh-copy-id  192.168.4.$i
 done 
 //传递公钥给host52,53,主机

步骤二: 安装依赖包
配置数据库服务器192.168.4.51(52,53,57也做)

[root@host51 ~]# yum -y install perl-* //安装系统自带的perl软件包
[root@host51 ~]# ls mha-soft
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
perl-Mail-Sendmail-0.79-21.el7.art.noarch.rpm
perl-MIME-Lite-3.030-1.el7.noarch.rpm
perl-MIME-Types-1.38-2.el7.noarch.rpm
perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
root@host51 ~]# cd mha-soft
[root@host51 mha-soft]# yum –y install perl-* //安装共享的perl软件包

步骤三: 配置MySQL一主多从结构
1)配置主服务器192.168.4.51

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

2)配置从服务器192.168.4.52

[root@host52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52 //指定server_id

[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

[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日志
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线程正常

部署MHA集群

步骤一:配置管理节点
1)安装软件

[root@mgm57 ~]# cd mha-soft-student/
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# rpm -ivh  mha4mysql-node-0.56-0.el6.noarch.rpm//安装mha-node软件包
准备中...                          ################################# [100%]
正在升级/安装...
 1:mha4mysql-node-0.56-0.el6        ################################# [100%]
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# rpm -qa | grep  mha //查看是否安装成功
mha4mysql-node-0.56-0.el6.noarch
[root@mgm57 mha-soft-student]#
[root@mgm57 mha-soft-student]# tar -zxvf mha4mysql-manager-0.56.tar.gz //解压mha-manager软件包
mha4mysql-manager-0.56/
mha4mysql-manager-0.56/debian/
mha4mysql-manager-0.56/debian/control
mha4mysql-manager-0.56/debian/copyright
……
……
[root@mgm57 mha-soft-student]# ls  
app1.cnf            mha4mysql-manager-0.56        
mha4mysql-node-0.56-0.el6.noarch.rpm
master_ip_failover  mha4mysql-manager-0.56.tar.gz
[root@mgm57 mha-soft-student]# cd mha4mysql-manager-0.56  //进入源码目录
[root@mgm57 mha4mysql-manager-0.56]# ls //查看文件列表
AUTHORS  COPYING  inc  Makefile.PL  META.yml  rpm      t
bin      debian   lib  MANIFEST     README    samples  tests
[root@mgm57 mha4mysql-manager-0.56]#
[root@mgm57 mha4mysql-manager-0.56]# perl Makefile.PL //配置
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI                   ...loaded. (1.627)
- DBD::mysql            ...loaded. (4.023)
- Time::HiRes           ...loaded. (1.9725)
- Config::Tiny          ...loaded. (2.14)
- Log::Dispatch         ...loaded. (2.41)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst        ...loaded. (0.56)
*** Module::AutoInstall configuration finished.
Checking if your kit is complete...
Looks good
Writing Makefile for mha4mysql::manager
Writing MYMETA.yml and MYMETA.json
[root@mgm57 mha4mysql-manager-0.56]#  make //编译
[root@mgm57 mha4mysql-manager-0.56]#  make install //安装
[root@mgm57 mha4mysql-manager-0.56]# ls /root/perl5/bin  //查看安装的命令
masterha_check_repl    masterha_conf_host       masterha_master_switch
masterha_check_ssh     masterha_manager         masterha_secondary_check
masterha_check_status  masterha_master_monitor  masterha_stop

2)编辑主配置文件

[root@mgm57 ~ ]#  mkdir  /etc/mha  //创建工作目录
[root@mgm57 ~ ]#  cp mha4mysql-manager-0.56/sample/conf/app1.cnf /etc/mha/ //拷贝模板文件
[root@mgm57 ~ ]#  vim /etc/mha/app1.cnf   //编辑主配置文件
     [server default]     //管理服务默认配置
                manager_workdir=/etc/mha    //工作目录
                manager_log=/etc/mha/manager.log   //日志文件
                master_ip_failover_script=/etc/mha/master_ip_failover //故障切换脚本
                 ssh_user=root  //访问ssh服务用户
                 ssh_port=22    //ssh服务端口
                 repl_user=repluser           //主服务器数据同步授权用户
                 repl_password=123qqq…A    //密码
                 user=root                   //监控用户
                 password=123qqq…A               //密码
        [server1]   //指定第1台数据库服务器
hostname=192.168.4.51    //服务器ip地址
port=3306                //服务端口
candidate_master=1               //竞选主服务器
[server2]  //指定第2台数据库服务器
hostname=192.168.4.52
port=3306
candidate_master=1  
            
[server3]  //指定第3台数据库服务器
hostname=192.168.4.53
port=3306
candidate_master=1

3)创建故障切换脚本

[root@mgm57 ~]# cp mha-soft-student/master_ip_failover /etc/mha/
[root@mgm57 ~]# vim +35 /etc/mha/master_ip_failover
my $vip = '192.168.4.100/24';  # Virtual IP  //定义VIP地址
my $key = "1"; //定义变量$key
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; //部署vip地址命令
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down"; //释放vip地址命令

[root@mgm57 ~]# chmod +x  /etc/mha/master_ip_failover //给脚本加执行权限

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

[root@host51 ~]# ifconfig  eth0:1  //部署之前查看 
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:d8:10:d7  txqueuelen 1000  (Ethernet)
[root@host51 ~]# ifconfig  eth0:1  192.168.4.100  //部署vip地址
[root@host51 ~]# 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:d8:10:d7  txqueuelen 1000  (Ethernet)

步骤二:配置数据节点
1)在所有数据库服务器上,安装mha-node软件包

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

2)在所有数据服务器上添加监控用户
可以只在host51主机执行授权命令,host52和host53 会自动同步授权

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

3)在2台从服务器上添加,数据同步连接用户
在从服务器host52添加用户

[root@host52]# mysql –uroot –p密码
mysql> grant replication slave on  *.*  to repluser@"%" identified by "123qqq...A";
mysql> exit;
在从服务器host53添加用户
[root@host53]# mysql –uroot –p密码
mysql> grant replication slave on  *.*  to repluser@"%" identified by "123qqq...A";
mysql> exit;

4)修改数据库服务运行参数
修改主服务器host51

[root@host51 ~]# vim /etc/my.cnf
[mysqld]
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" //加载模块
rpl_semi_sync_master_enabled=1  //启用master模块
rpl_semi_sync_slave_enabled=1     //启用slave模块
relay_log_purge=0   //禁止自动删除中继日志文件

[root@host51 ~]#  systemctl  restart  mysqld  //重启服务

修改从服务器host52

[root@host52 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master52
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" //加载模块
rpl_semi_sync_master_enabled=1  //启用master模块
rpl_semi_sync_slave_enabled=1     //启用slave模块
relay_log_purge=0   //禁止自动删除中继日志文件

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

修改从服务器host53

[root@host53 ~]# vim /etc/my.cnf
[mysqld]
log-bin=master53
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" //加载模块
rpl_semi_sync_master_enabled=1 //启用master模块
rpl_semi_sync_slave_enabled=1 //启用slave模块
relay_log_purge=0 //禁止自动删除中继日志文件

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

测试配置

步骤一:测试集群环境
1)在管理主机,测试ssh配置

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

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

 [root@host57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf //执行测试命令
 MySQL Replication Health is OK.//测试成功提示信息

3)启动管理服务

[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.. 

4)查看服务状态

[root@mgm57 ~]#  masterha_check_status  --conf=/etc/mha/app1.cnf//执行命令
app1 (pid:15806) is running(0:PING_OK), master:192.168.4.51 //服务运行,监视主服务器192.168.4.51
[root@mgm57 ~]# ls /etc/mha/   //查看工作目录文件列表
app1.cnf  app1.master_status.health  manager.log  master_ip_failover

步骤二:访问集群
1)在主服务器51 添加访问数据的连接用户

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

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

host50~]# mysql -h192.168.4.100  -uyaya55  -p123qqq...A
mysql> select  * from  db9.a;
mysql> insert into db9.a values(100);
mysql> select  * from db9.a;
+------+
| id   |
+------+
|  100 |
+------+
1 row in set (0.00 sec)
mysql>exit

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

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

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

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

步骤三:测试高可用
1)停止主服务器51的mysql服务

host51~]# systemctl  stop  mysqld

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

 [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..
[root@host57 ~]#
[root@mgm57 ~]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 is stopped(2:NOT_RUNNING).  //监控到主服务器宕机 管理服务自动停止
[root@mgm57 ~]#

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

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

4)查看vip地址
在host52主机查看到vip地址,说明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)

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

 [root@host53 ~]# ifconfig  eth0:1 //未查到vip地址
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:28:22:2e  txqueuelen 1000  (Ethernet)
[root@host53 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i 192
mysql: [Warning] Using a password on the command line interface can be insecure.
                  Master_Host: 192.168.4.52  //主服务器Ip地址
[root@host53 ~]#
[root@host53 ~]# mysql -uroot -p123qqq...A -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: Yes  //IO线程正常
            Slave_SQL_Running: Yes  //SQL线程正常
 [root@host53 ~]# mysql -uroot -p123qqq...A -e "select  * from db9.a" //自动同步数据
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
|  100 |
|  200 |
+------+

步骤四:修复故障服务器
1)配置数据库服务器
启动host51主机的数据库服务

host51~]# systemctl  start  mysqld

与主服务器数据一致

[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 ~]#
[root@host52 ~]# scp db9.sql  root@192.168.4.51:/root/  //拷贝备份文件给host51主机
db9.sql  100% 1918     3.1MB/s   00:00
[root@host52 ~]#
host51 ~]# mysql -uroot -p123qqq...A db9 < /root/db9.sql//host51 主机使用备份文件恢复数据
mysql: [Warning] Using a password on the command line interface can be insecure.

指定主服务器信息

 [root@host51 ~]# grep master52 /root/db9.sql  //查看日志名及偏移量
CHANGE MASTER TO MASTER_LOG_FILE='master52.000001', MASTER_LOG_POS=895;
[root@host51 ~]# mysql -uroot -p123qqq...A
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;
Query OK, 0 rows affected, 2 warnings (0.14 sec)

启动slave进程

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
Mysql> exit ;

查看状态信息

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

2)配置管理服务器
修改配置文件,添加数据库服务器host51

]# vim /etc/mha/app1.cnf
 [server1 ]
hostname=192.168.4.51
port=3306
candidate_master=1

测试集群环境

[root@mgm57 ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf //测试SSH
Thu Jun 20 15:33:51 2019 - [info] All SSH connection tests passed successfully.//成功
[root@mgm57 ~]# masterha_check_repl --conf=/etc/mha/app1.cnf//测试主从同步
MySQL Replication Health is OK. //成功

重启管理服务

]# masterha_stop  --conf=/etc/mha/app1.cnf //停止管理服务
Stopped app1 successfully.
]# 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..

查看状态

mgm57 ~]# masterha_check_status  --conf=/etc/mha/app1.cnf
app1 (pid:15806) is running(0:PING_OK), master:192.168.4.52 //服务运行,监视服务器52
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值