欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Mysql5.5部署MHA

程序员文章站 2024-01-25 16:39:52
...

MHA分管理节点和数据库节点,数据库节点由mysql主从或者主主从构成,当主库挂掉后,管理节点会自动将从节点提升为主节点;管理节点的角色类似于oracle数据库中的

MHA分管理节点和数据库节点,香港空间,数据库节点由mysql主从或者主主从构成,当主库挂掉后,管理节点会自动将从节点提升为主节点;管理节点的角色类似于oracle数据库中的fast start failover中的observer,但mha上层可以通过keepalive部署VIP,程序连接数据库使用VIP,从而实现后台数据库的故障切换透明化

MHA节点包含三个脚本,依赖perl模块;
save_binary_logs:保存和复制当掉的主服务器二进制日志;
apply_diff_relay_logs:识别差异的relay log事件,并应用于其他salve服务器;
purge_relay_logs:清除relay log文件;
需要在所有mysql服务器上安装MHA节点,网站空间,MHA管理服务器也需要安装。MHA管理节点模块内部依赖MHA节点模块;
MHA管理节点通过ssh连接管理mysql服务器和执行MHA节点脚本。MHA节点依赖perl的DBD::mysql模块;

本文环境介绍,香港服务器,操作系统均为rhel5.4
主库:192.168.123.13/dg53.yang.com
备库:192.168.123.14/dg54.yang.com
管理节点:192.168.123.15/dg55.yang.com

在开始之前,请先配置好服务器间的时间同步和名称解析

一:在数据库节点安装mha node

二:管理节点
1:按照步骤一安装mha node

2:安装mha manager

3:编辑配置文件

4:配置manager节点和node节点以及node节点间的ssh公钥信任

5:测试ssh连接

6:测试主从复制情况,默认使用root用户连接

7:启动管理节点进程

三:测试failover过程
1:当前主库为192.168.123.13,关闭主库

2:在管理节点上观察日志输出

[root@dg55 ~]# tail -f /masterha/app1/manager.log
Wed Jun 6 14:50:48 2012 - [info]
192.168.123.13 (current master)
+--192.168.123.14

Wed Jun 6 14:50:48 2012 - [warning] master_ip_failover_script is not defined.
Wed Jun 6 14:50:48 2012 - [warning] shutdown_script is not defined.
Wed Jun 6 14:50:48 2012 - [info] Set master ping interval 1 seconds.
Wed Jun 6 14:50:48 2012 - [warning] secondary_check_script is not defined. It is highly recommended setting it to check master reachability from two or more routes.
Wed Jun 6 14:50:48 2012 - [info] Starting ping health check on 192.168.123.13(192.168.123.13:3306)..
Wed Jun 6 14:50:48 2012 - [info] Ping succeeded, sleeping until it doesn't respond..

Wed Jun 6 14:51:32 2012 - [warning] Got error on MySQL ping: 2006 (MySQL server has gone away)
Wed Jun 6 14:51:32 2012 - [info] HealthCheck: SSH to 192.168.123.13 is reachable.
Wed Jun 6 14:51:33 2012 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Jun 6 14:51:33 2012 - [warning] Connection failed 1 time(s)..
Wed Jun 6 14:51:34 2012 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Jun 6 14:51:34 2012 - [warning] Connection failed 2 time(s)..
Wed Jun 6 14:51:35 2012 - [warning] Got error on MySQL connect: 2013 (Lost connection to MySQL server at 'reading initial communication packet', system error: 111)
Wed Jun 6 14:51:35 2012 - [warning] Connection failed 3 time(s)..
Wed Jun 6 14:51:35 2012 - [warning] Master is not reachable from health checker!
Wed Jun 6 14:51:35 2012 - [warning] Master 192.168.123.13(192.168.123.13:3306) is not reachable!
Wed Jun 6 14:51:35 2012 - [warning] SSH is reachable.
Wed Jun 6 14:51:35 2012 - [info] Connecting to a master server failed. Reading configuration file /etc/masterha_default.cnf and /etc/masterha/app1.cnf again, and trying to connect to all servers to check server status..
Wed Jun 6 14:51:35 2012 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Wed Jun 6 14:51:35 2012 - [info] Reading application default configurations from /etc/masterha/app1.cnf..
Wed Jun 6 14:51:35 2012 - [info] Reading server configurations from /etc/masterha/app1.cnf..
Wed Jun 6 14:51:35 2012 - [info] Dead Servers:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Alive Servers:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306)
Wed Jun 6 14:51:35 2012 - [info] Alive Slaves:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306) Version=5.5.25-log (oldest major version between slaves) log-bin:enabled
Wed Jun 6 14:51:35 2012 - [info] Replicating from 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jun 6 14:51:35 2012 - [info] Checking slave configurations..
Wed Jun 6 14:51:35 2012 - [warning] read_only=1 is not set on slave 192.168.123.14(192.168.123.14:3306).
Wed Jun 6 14:51:35 2012 - [warning] relay_log_purge=0 is not set on slave 192.168.123.14(192.168.123.14:3306).
Wed Jun 6 14:51:35 2012 - [info] Checking replication filtering settings..
Wed Jun 6 14:51:35 2012 - [info] Replication filtering check ok.
Wed Jun 6 14:51:35 2012 - [info] Master is down!
Wed Jun 6 14:51:35 2012 - [info] Terminating monitoring script.
Wed Jun 6 14:51:35 2012 - [info] Got exit code 20 (Master dead).
Wed Jun 6 14:51:35 2012 - [info] MHA::MasterFailover version 0.52.
Wed Jun 6 14:51:35 2012 - [info] Starting master failover.
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] * Phase 1: Configuration Check Phase..
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] Dead Servers:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Checking master reachability via mysql(double check)..
Wed Jun 6 14:51:35 2012 - [info] ok.
Wed Jun 6 14:51:35 2012 - [info] Alive Servers:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306)
Wed Jun 6 14:51:35 2012 - [info] Alive Slaves:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306) Version=5.5.25-log (oldest major version between slaves) log-bin:enabled
Wed Jun 6 14:51:35 2012 - [info] Replicating from 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jun 6 14:51:35 2012 - [info] ** Phase 1: Configuration Check Phase completed.
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] * Phase 2: Dead Master Shutdown Phase..
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] Forcing shutdown so that applications never connect to the current master..
Wed Jun 6 14:51:35 2012 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master ip address.
Wed Jun 6 14:51:35 2012 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Wed Jun 6 14:51:35 2012 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] * Phase 3: Master Recovery Phase..
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] The latest binary log file/position on all slaves is mysql-bin.000021:107
Wed Jun 6 14:51:35 2012 - [info] Latest slaves (Slaves that received relay log files to the latest):
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306) Version=5.5.25-log (oldest major version between slaves) log-bin:enabled
Wed Jun 6 14:51:35 2012 - [info] Replicating from 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jun 6 14:51:35 2012 - [info] The oldest binary log file/position on all slaves is mysql-bin.000021:107
Wed Jun 6 14:51:35 2012 - [info] Oldest slaves:
Wed Jun 6 14:51:35 2012 - [info] 192.168.123.14(192.168.123.14:3306) Version=5.5.25-log (oldest major version between slaves) log-bin:enabled
Wed Jun 6 14:51:35 2012 - [info] Replicating from 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:35 2012 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] * Phase 3.2: Saving Dead Master's Binlog Phase..
Wed Jun 6 14:51:35 2012 - [info]
Wed Jun 6 14:51:35 2012 - [info] Fetching dead master's binary logs..
Wed Jun 6 14:51:35 2012 - [info] Executing command on the dead master 192.168.123.13(192.168.123.13:3306): save_binary_logs --command=save --start_file=mysql-bin.000021 --start_pos=107 --binlog_dir=/mydata --output_file=/var/tmp/saved_master_binlog_from_192.168.123.13_3306_20120606145135.binlog --handle_raw_binlog=1 --disable_log_bin=0 --manager_version=0.52
Creating /var/tmp if not exists.. ok.
Concat binary/relay logs from mysql-bin.000021 pos 107 to mysql-bin.000021 EOF into /var/tmp/saved_master_binlog_from_192.168.123.13_3306_20120606145135.binlog ..
Dumping binlog format description event, from position 0 to 107.. ok.
Dumping effective binlog data from /mydata/mysql-bin.000021 position 107 to tail(126).. ok.
Concat succeeded.
Wed Jun 6 14:51:36 2012 - [info] scp from root@192.168.123.13:/var/tmp/saved_master_binlog_from_192.168.123.13_3306_20120606145135.binlog to local:/masterha/app1/saved_master_binlog_from_192.168.123.13_3306_20120606145135.binlog succeeded.
Wed Jun 6 14:51:36 2012 - [info] HealthCheck: SSH to 192.168.123.14 is reachable.
Wed Jun 6 14:51:37 2012 - [info]
Wed Jun 6 14:51:37 2012 - [info] * Phase 3.3: Determining New Master Phase..
Wed Jun 6 14:51:37 2012 - [info]
Wed Jun 6 14:51:37 2012 - [info] Finding the latest slave that has all relay logs for recovering other slaves..
Wed Jun 6 14:51:37 2012 - [info] All slaves received relay logs to the same position. No need to resync each other.
Wed Jun 6 14:51:37 2012 - [info] Searching new master from slaves..
Wed Jun 6 14:51:37 2012 - [info] Candidate masters from the configuration file:
Wed Jun 6 14:51:37 2012 - [info] 192.168.123.14(192.168.123.14:3306) Version=5.5.25-log (oldest major version between slaves) log-bin:enabled
Wed Jun 6 14:51:37 2012 - [info] Replicating from 192.168.123.13(192.168.123.13:3306)
Wed Jun 6 14:51:37 2012 - [info] Primary candidate for the new Master (candidate_master is set)
Wed Jun 6 14:51:37 2012 - [info] Non-candidate masters:
Wed Jun 6 14:51:37 2012 - [info] Searching from candidate_master slaves which have received the latest relay log events..
Wed Jun 6 14:51:37 2012 - [info] New master is 192.168.123.14(192.168.123.14:3306)
Wed Jun 6 14:51:37 2012 - [info] Starting master failover..
Wed Jun 6 14:51:37 2012 - [info]
From:
192.168.123.13 (current master)
+--192.168.123.14