[转载]Centos下利用mysql-mmm实现mysql高可用
[[Multi-Master Replication Manager for MySQL]]
一、MMM简介:
MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。
MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。
MMM项目来自 Google:http://code.google.com/p/mysql-master-master
官方网站为:http://mysql-mmm.org
Mmm主要功能由下面三个脚本提供
l mmm_mond 负责所有的监控工作的监控守护进程,决定节点的移除等等
l mmm_agentd 运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给监控节点
l mmm_control 通过命令行管理mmm_mond进程
二、mysql-mmm架构的搭建
1、 先介绍下本文的环境:
系统环境:CentOS release 5.4(32bit)
server1 ip: 192.168.1.161 virtual read ip:192.168.1.111
server2 ip: 192.168.1.162 virtual read ip:192.168.1.112
server3 ip: 192.168.1.163 virtual write ip: 192.168.1.113
2、 mysql-mmm架构配置简介:
u 在server1、server2上安装mysql,并配置为master-master架构(就是互为主从)———-配置很简单,就不对着部分进行详细解释,有问题的话请查看:http://blog.chinaunix.net/u3/93755/showart.php?id=2213538
u 在server1、server2,server3上安装mmm,并配置:mmm_common.conf、mmm_agent.conf以及mmm_mon.conf文件
3、 Mysql-mmm实战
前提:server1和server2上已经配置好mysql主主同步
u 安装mysql-mmm
CentOS软件仓库默认是不含这些软件的,必须要有epel这个包的支持。故我们必须先安装epel:
wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -Uvh epel-release-5-4.noarch.rpm
yum -y install mysql-mmm*
u 配置mmm代理和监控账号的权限
在server1和server2上分别执行:
GRANT REPLICATION CLIENT ON *.* TO ‘mmm_monitor’@’192.168.1.%’ IDENTIFIED BY ‘monitor_password’;
GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO ‘mmm_agent’@’192.168.1.%’ IDENTIFIED BY ‘agent_password’;
flush privileges;
u 配置mysql-mmm
所有的配置选项都集合在了一个叫/etc/mysql-mmm/mmm_common.conf的单独文件中,系统中所有主机的该文件内容都是一样的, 配置完后不要忘记了拷贝这个文件到所有的主机(包括监控主机)!,内容如下:
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mysql-mmm/mmm_agentd.pid
bin_path /usr/libexec/mysql-mmm/
#同步的帐号(这些要和前面设置的保持一致!)
replication_user replication
replication_password 123456 #同步的密码
agent_user mmm_agent #mmm-agent用户名
agent_password agent_password #mmm-agent用户密码
</host>
<host db1>
ip 192.168.1.161 #db1的ip
mode master
peer db2
</host>
<host db2>
ip 192.168.1.162 #db2的ip
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 192.168.1.113 #设置写如的虚拟IP
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 192.168.1.111, 192.168.1.112 #设置读取的虚拟IP
mode balanced
</role>
在数据库主机上我们需要编辑/etc/mysql-mmm/mmm_agent.conf文件,根据其他主机的不同更改db1的值(db2就将db1更改成db2):
include mmm_common.conf
this db1
在监控主机上我们需要编辑/etc/mysql-mmm/mmm_mon.conf文件:
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mysql-mmm/mmm_mond.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 192.168.1.161,192.168.1.162 #监控服务器ip
auto_set_online 60
# The kill_host_bin does not exist by default, though the monitor will
# throw a warning about it missing. See the section 5.10 “Kill Host
# Functionality” in the PDF documentation.
#
# kill_host_bin /usr/libexec/mysql-mmm/monitor/kill_host
#
</monitor>
<host default>
monitor_user mmm_monitor #mmm_monitor用户名
monitor_password monitor_password #mmm_monitor密码
</host>
debug 0
u 启动MMM
启动代理:
(在数据库服务器上server1、2)编辑/etc/default/mysql-mmm-agent来开启:
ENABLED=1
然后启动它:
/etc/init.d/mysql-mmm-agent start
启动监控(在监控机上):
/etc/init.d/mysql-mmm-monitor start
u 利用mmm_control监控mysql服务器状态:
[root@server3 mysql-mmm]# mmm_control show
db1(192.168.1.161) master/ONLINE. Roles: reader(192.168.1.112), writer(192.168.1.113)
db2(192.168.1.162) master/ONLINE. Roles: reader(192.168.1.111)
u 测试看两个mysql服务器能否实现故障自动切换
停掉作为写的db1上的mysql,查看写的服务器会不会自动转移到db2上去
停掉几秒钟后用mmm_control show查看:
[root@server3 mysql-mmm]# mmm_control show
db1(192.168.1.161) master/HARD_OFFLINE. Roles:
db2(192.168.1.162) master/ONLINE. Roles: reader(192.168.1.111), reader(192.168.1.112), writer(192.168.1.113)
我们可以看到已经把db2当作主写服务器
再来看看db1恢复后会是什么情况:
[root@server3 mysql-mmm]# mmm_control show
db1(192.168.1.161) master/ONLINE. Roles: reader(192.168.1.111)
db2(192.168.1.162) master/ONLINE. Roles: reader(192.168.1.112), writer(192.168.1.113)
我们可以看到当db1恢复后就充当slave的角色了!只有当db2挂了以后db1又会担当起主服务器的写入功能
u mmm_control命令简介
[root@server3 mysql-mmm]# mmm_control help
Valid commands are:
help – show this message
#查看帮助信息
ping – ping monitor
#ping监控
show – show status
#查看状态信息
checks [<host>|all [<check>|all]] – show checks status
#显示检查状态,包括(ping、mysql、rep_threads、rep_backlog)
set_online <host> – set host <host> online
#设置某host为online状态
set_offline <host> – set host <host> offline
#设置某host为offline状态
mode – print current mode.
#打印当前的模式,是ACTIVE、MANUAL、PASSIVE?
#默认ACTIVE模式
set_active – switch into active mode.
#更改为active模式
set_manual – switch into manual mode.
#更改为manual模式
set_passive – switch into passive mode.
#更改为passive模式
move_role [–force] <role> <host> – move exclusive role <role> to host <host>
#更改host的模式,比如更改处于slave的mysql数据库角色为write
(Only use –force if you know what you are doing!)
set_ip <ip> <host> – set role with ip <ip> to host <host>
#为host设置ip,只有passive模式的时候才允许更改!
原创作品:转载请注明出处!飞鸿无痕