MHA集群介绍与安装
MHA ( Master High Availability )
– 由日本 DeNA 公司 youshimaton (现就职于 Facebook 公司)开发
– 是一套优秀的作为 MySQL 高可用性环境下故障切换和主从提升的高可用软件。
– 目前在 MySQL 高可用方面是一个相对成熟的解决方案。
– 在 MySQL 故障切换过程中, MHA 能做到在 0~30 秒之内自动完成数据库的故障切换操作
– 并且在进行故障切换的过程中, MHA 能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA 组成
? MHA Manager (管理节点)
– 可以单独部署在一台独立的机器上管理多个 master-slave 集群,也可以部署在一台 slave 节点上。
? MHA Node (数据节点)
– 运行在每台 MySQL 服务器上。
MHA 工作过程
? MHA Manager 会定时探测集群中的 master 节点,当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master ,然后将所有其他的 slave重新指向新的 master 。整个故障转移过程对应用程序完全透明。
– ( 1 )从宕机崩溃的 master 保存二进制日志事件( binlog events)
– ( 2 )识别含有最新更新的 slave
– ( 3 )应用差异的中继日志( relay log )到其他的 slave
– ( 4 )应用从 master 保存的二进制日志事件( binlog events )
– ( 5 )提升一个 slave 为新的 master ;
– ( 6 )使其他的 slave 连接新的 master 进行复制;
master51
|
|
| | | | |
slave52 slave53 slave54 slave55 mgm56
备用主 备用主 Manager
一、配置所有数据节点主机之间可以互相以ssh密钥对方式认证登陆
1.1 在每一台数据库服务器创建密钥对,然后把公钥拷贝给其他4台数据库服务器
[[email protected] mysql]# ssh-keygen 创建密钥对
[[email protected] ~]# for i in 192.168.4.{52..56}; ssh-copy-id $i //同样拷贝公钥给其他4台数据库服务器
1.2 配置manager56主机 无密码ssh登录 所有数据节点主机
[[email protected] ~]#ssh-keygen //创建密钥对
[[email protected] ~]#for i in 192.168.4.{51..55}; do ssh-copy-id $i; done
二、安装软件包
2.1 在所有主机(51-56)上安装perl软件包(在此我以51为例)
[[email protected] ]# yum -y install perl-*.rpm
2.2 在所有数据节点主机上[51-56]安装mha_node软件包
[[email protected] mha-soft-student]# yum -y install perl-DBD-mysql perl-DBI
[[email protected] mha-soft-student]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
2.3 只在管理 "主机56" 上安装mha_manager软件包
[[email protected] mha-soft-student]# yum -y install perl-ExtUtils- perl-CPAN
2.4 [[email protected] mha-soft-student]# tar -zxvf mha4mysql-manager-0.56.tar.gz
[[email protected] mha-soft-student]# cd mha4mysql-manager-0.56/
[[email protected] mha4mysql-manager-0.56]# perl Makefile.PL
[[email protected] mha4mysql-manager-0.56]# make && make install
三、配置主从同步,要求如下:
51 主库 开半同步复制
52 从库(备用主库) 开半同步复制
53 从库(备用主库) 开半同步复制
54 从库 不做备用主库所以不用开半同步复制
55 从库 不做备用主库所以不用开半同步复制
56 管理主机
3.1 配置主库51
[[email protected] ~]# 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
rpl-semi-sync-slave-enabled = 1
server_id=51
log-bin=master51
binlog-format="mixed"
:wq
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# ls /var/lib/mysql/master51.*
/var/lib/mysql/master51.000001 /var/lib/mysql/master51.index
[[email protected] ~]# mysql -uroot -p123456
mysql> grant replication slave on . to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.00 sec)
mysql> set global relay_log_purge=off; // 不自动删除本机的中继日志文件
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 438 | | | |
+-----------------+----------+--------------+------------------+-------------------+
mysql> quit
3.2、备用master52的配置
[[email protected] ~]# 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
rpl-semi-sync-slave-enabled = 1
server_id=52
log-bin=master52
binlog-format="mixed"
[[email protected] ~]# ls /var/lib/mysql/master52.*
/var/lib/mysql/master52.000001 /var/lib/mysql/master52.index
[[email protected] ~]# mysql -uroot -p123456
mysql> grant replication slave on . to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.00 sec)
mysql> set global relay_log_purge=off; // 不自动删除本机的中继日志文件
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host="192.168.4.51", \
-> master_user="harry",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=438;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: harry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 438
Relay_Log_File: 52-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.3、备用master53的配置
[[email protected] ~]# vim /etc/my.cnf
plugin-load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1
server_id=53
log-bin=master53
binlog-format="mixed"
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# ls /var/lib/mysql/master53.*
/var/lib/mysql/master53.000001 /var/lib/mysql/master53.index
[[email protected] ~]# mysql -uroot -p123456
mysql> grant replication slave on . to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (10.00 sec)
mysql> set global relay_log_purge=off; // 不自动删除本机的中继日志文件
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host="192.168.4.51", \
-> master_user="harry",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=438;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: harry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 438
Relay_Log_File: 53-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
mysql> quit;
3.4、配置从服务器54
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=54
:wq
[[email protected] ~]# systemctl restart mysqld
mysql> change master to master_host="192.168.4.51", \
-> master_user="harry",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=438;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: harry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 438
Relay_Log_File: 54-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.5、配置从服务器55
[[email protected] ~]# vim /etc/my.cnf
server_id=55
:wq
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# mysql -uroot -p123456
mysql> change master to master_host="192.168.4.51", \
-> master_user="harry",
-> master_password="123456",
-> master_log_file="master51.000001",
-> master_log_pos=438;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.51
Master_User: harry
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 438
Relay_Log_File: 55-relay-bin.000002
Relay_Log_Pos: 319
Relay_Master_Log_File: master51.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.6.2在客户端测试主从同步配置
3.6.1 在主库51上添加访问数据的授权用户
[[email protected] ~]# mysql -uroot -p123456
mysql> grant all on gamedb.* to [email protected]"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database gamedb;
Query OK, 1 row affected (0.06 sec)
mysql> create table gamedb.t1 (id int);
mysql> insert into gamedb.t1 values(222);
Query OK, 1 row affected (0.03 sec)
mysql> insert into gamedb.t1 values(222);
Query OK, 1 row affected (0.04 sec)
然后在从库上查看
3.6.3 在客户端使用授权用户连接从库52-55,也能看到同样的库表及记录
[[email protected] ~]# mysql -uroot -p123456
mysql> select * from gamedb.t1;
+------+
| id |
+------+
| 222 |
| 222 |
+------+
四、编辑管理主机(56)主配置文件
[[email protected] mha-soft-student]# cp mha4mysql-manager-0.56/bin/* /usr/local/bin/
[[email protected] mha-soft-student]# mkdir /etc/mha_manager/
[[email protected] mha-soft-student]# cp mha4mysql-manager-0.56/samples/conf/app1.cnf /etc/mha_manager/
[[email protected] ]# vim /etc/mha_manager/app1.cnf
[server default]
manager_workdir=/etc/mha_manager
manager_log=/etc/mha_manager.log
master_ip_failover_script=/usr/local/bin/master_ip_failover
ssh_user=root
ssh_port=22
repl_user=harry //主从同步用户名
repl_password=123456 //主从同步密码
user=root //连接数据库用户名
password=123456 //连接数据库密码
[server1]
hostname=192.168.4.51
candidate_master=1 // 设置为候选 master
port=3306
[server2]
hostname=192.168.4.52
candidate_master=1
port=3306
[server3]
hostname=192.168.4.53
candidate_master=1
port=3306
[server4]
hostname=192.168.4.54
no_master=1 // 不竞选 master
port=3306
[server5]
hostname=192.168.4.55
no_master=1
port=3306
在管理节点上 通过 master_check_ssh 做 ssh 检查
[[email protected] mha_manager]# masterha_check_ssh --conf /etc/mha_manager/app1.cnf
测试主从同步状态
**检查主从同步时把app1.cnf文件中的此配置项#master_ip_failover_script=/usr/local/bin/master_ip_failover注释掉,不然检查失败。
[[email protected] mha_manager]# vim /etc/mha_manager/app1.cnf
#master_ip_failover_script=/usr/local/bin/master_ip_failover
[[email protected] ~]# masterha_check_repl --conf=/etc/mha_manager/app1.cnf
四、测试高可用集群配置
4.1 在主库上手动部署vip 地址 192.168.4.100
[[email protected] ~]# ifconfig eth0:1 192.168.4.100/24
[[email protected] ~]# ifconfig eth0:1
4.2 修改故障切换脚本 指定vip地址的部署信息
[[email protected] ~]# vim /etc/mha_manager/master_ip_failover
my $vip = ‘192.168.4.100/24‘; # Virtual IP
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
:wq
4.3 启动管理服务,并查看服务状态
开启 MHA Manager 监控
– masterha_manager // 启动命令
– --remove_dead_master_conf // 不在 app1.cnf 文件
里删除宕机的主库的信息
– --ignore_last_failover // 忽略 .health 文件
[[email protected] ~]# masterha_manager --conf=/etc/mha_manager/app1.cnf --remove_dead_master_conf --ignore_last_failover
查看状态 : masterha_check_status
[[email protected] ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
app1 (pid:9399) is running(0:PING_OK), master:192.168.4.51
停止服务 : masterha_stop
[[email protected] bin]# masterha_stop --conf=/etc/mha_manager/app1.cnf
Stopped app1 successfully.
4.4 测试故障转移
在主库51 上执行 ]# shutdown -h now
4.5 在管理主机上查看服务状态(如果服务停止了,手动启动一下服务,再查看状态)
[[email protected] ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
app1 (pid:17507) is running(0:PING_OK), master:192.168.4.52
4.6 在52 本机查看是否获取vip地址
[[email protected] ~]# ip addr show | grep 192.168.4
inet 192.168.4.52/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.100/24 brd 192.168.4.255 scope global secondary eth0:1
4.6 客户端连接vip地址 ,访问数据服务
[[email protected]]#mysql -h192.168.4.100 -uwebadmin -p123456
查看 VIP 地址
当主库服务器宕机后,在备用 1 主库数据库服务器上查看 VIP 地址
[[email protected] ~]# ip addr show | grep vip 地址
手动配置 vip 地址
[[email protected] ~]# ifconfig ethX:1 x.x.x.x/32
原文地址:http://blog.51cto.com/13124606/2143360