该文档介绍了基于 MHA (Master High Availability) 的 MySQL 高可用方案,详细讲解了 MHA 的特性、切换过程、构建步骤及所需条件。MHA 通过自动化主服务器故障转移,能够快速将从服务器誉为主服务器,确保数据的一致性。文档还包含了常用操作命令和注意事项,旨在帮助用户有效部署和管理高可用的 MySQL 环境。
4.4 Typical timeline
• Usually no more than 10-30 seconds
• 0-10s: Master failover detected in around 10 seconds
• (optional) 10-20s: 10 seconds to power off master
• 10-20s: apply differential relay logs to new master
• Practice: 4s @ DeNA, usually less than 10s
5. MHA 构建步骤
以下以一主三从为例,如:
10.0.0.11 slave 节点 mha node
10.0.0.74 slave 节点 mha Manager 节点
10.0.0.75 slave 节点 mha node
10.0.0.13 master 节点 mha node
5.1 第一步:master slave
搭建 mysql master slave,为 1 一个 master,3 个 slave 架构
注意 slave 节点要设置:
read_only=1
relay_log_purge=off
log_bin= log_bin
另外,需要授予其他 node 对应的权限:
grant all privileges on *.* to 'root'@'db-11' identified by '111111';
grant all privileges on *.* to 'root'@'db-13' identified by '111111';
grant all privileges on *.* to 'root'@'db-75' identified by '111111';
grant all privileges on *.* to 'root'@'db-74' identified by '111111';
5.2 第二步:mha rpm 安装
所有节点安装 mha node
yum install perl-DBD-MySQL
rpm -ivh mha4mysql-node-X.Y-0.noarch.rpm
10.0.0.74 节 点 mha Manager 节 点 安 装 mha manager, 以 下 rpm 可 以 在
http://pkgs.repoforge.org/上去下载
yum install perl-DBD-MySQL
10
6. 常用操作命令
6.1 检查MHA 的配置
masterha_check_repl --conf=/etc/app1.cnf
检查 mha 的配置,如有错误根据错误提示进行修改,会有如下的信息:
........
Tue Mar 19 10:14:35 2013 - [info]
10.0.0.13 (current master)
+--10.0.0.74
+--10.0.0.11
+--10.0.0.75
..........
MySQL Replication Health is OK.
6.2 检查 ssh 的配置
masterha_check_ssh --conf=/etc/app1.cnf
MHA Manager internally connects to MySQL servers via SSH. MHA Node on the
latest slave also internally sends relay log files to other slaves via
SSH(scp). To make these procedures automated, it is generally recommended
to enable SSH public key authentication without pass-phrase. You can use
masterha_check_ssh command included in MHA Manager to check whether SSH
connections work properly.
6.3 检查 MHA manager 的状态
masterha_check_status --conf=/etc/app1.cnf
检查 mha 的运行状态,如下表明 mha 没有启动
app1 is stopped(2:NOT_RUNNING).
14
7. 注意事项
7.1 修复crash master
每次 failover 切换之后,原来的 master 可能就废弃掉了,如果还想用,服务器
文件完整的情况下,可以继续尝试 change master 运行,日志恢复的点可以在
mha 的 manager 日志中找到:
less manager.log
Tue Mar 19 17:31:18 2013 - [info] Starting recovery on
10.0.0.74(10.0.0.74:3306)..
Tue Mar 19 17:31:18 2013 - [info] This server has all relay logs. Waiting
all logs to be applied..
Tue Mar 19 17:31:18 2013 - [info] done.
Tue Mar 19 17:31:18 2013 - [info] All relay logs were successfully
applied.
Tue Mar 19 17:31:18 2013 - [info] Getting new master's binlog name and
position..
Tue Mar 19 17:31:18 2013 - [info] mysql-bin.000008:3921
Tue Mar 19 17:31:18 2013 - [info] All other slaves should start
replication from here. Statement should be: CHANGE MASTER TO
MASTER_HOST='10.0.0.74', MASTER_PORT=3306, MASTER_LOG_FILE='mysql
-bin.000008', MASTER_LOG_POS=3921, MASTER_USER='repl',
MASTER_PASSWORD='xxx';
7.2 DBA 专有备用 slave
对于一个 mysql 集群,如果有 DBA 专有的备用 slave,自动监控的节点里可以不
包括这个节点,如下面的集群里,可以只自动监控 10.0.0.74,10.0.0.13 和
10.0.0.11,这样在 10.0.0.75 上的备份、分析等操作不会影响整个集群,但是
维护操作是要把整个集群考虑在内,这个可以通过两个配置文件实现,
/etc/app1.cnf 和 /etc/app1_monitor.cnf(这个里没有 10.0.0.75)
/etc/app1.cnf 里的结构:
10.0.0.74 (current master)
+--10.0.0.13
+--10.0.0.11
+--10.0.0.75
/etc/app1_monitor.cnf 里的结构:
10.0.0.74 (current master)
+--10.0.0.13
17
18.
+--10.0.0.11
7.3 mysqlbinlog 工具的问题
MHAuses mysqlbinlog for applying binlog events to target slaves. If the
master uses row based format, row based events are written in the binary
log. Such binary logs can be parsed by mysqlbinlog in MySQL 5.1 or higher
only because mysqlbinlog in MySQL 5.0 does not support row based format.
mysqlbinlog (and mysql) version can be checked as below.
mysqlbinlog 需要位于 ssh 用户可以访问的地方,可通过类似软连接来实现
[root@oel58 ~]# chmod +x mysqlbinlog
[root@oel58 ~]#
[root@oel58 ~]# ln -s /root/mysqlbinlog /usr/bin/mysqlbinlog
[root@oel58 ~]#
[root@oel58 ~]# which mysqlbinlog
/usr/bin/mysqlbinlog
[root@oel58 ~]# mysqlbinlog -V
mysqlbinlog Ver 3.3 for Linux at i686
mysqlbinlog version should be 3.3 or higher if it's included in MySQL 5.1.
MHA internally checks both MySQL and mysqlbinlog version on all slaves.
If mysqlbinlog version is 3.2 or earlier and if MySQL version is 5.1 or
higher, MHA stops with errors before starting monitoring.
7.4 VIP 问题
如 果 调 整 了 虚 拟 IP , 请 修 改 master_ip_online_change.pl 和
master_ip_failover.pl 里 的 下 列 代 码 : my $vip = '10.0.0.119/24'; #
Virtual IP
同时注意对应的 ssh 用户要有执行权限。
7.5 发邮件与发短信
需要安装 Oracle dbd,所以需要 oracle 的 lib;
配置对应的 Tns;
增加或者删除下面的代码组:
$sql1 = "insert into
edm_user.tb_queue(ID,PHONE,MSG,STATUS,SENDLEVEL,svrtype,INSERTTIME)
values(edm_user.SEQ_QUE.NEXTVAL,'13800000000','$subject','',1,'11',sy
18