Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7470553
  • 博文数量: 368
  • 博客积分: 9600
  • 博客等级: 上校
  • 技术积分: 18875
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-01 00:00
文章分类

全部博文(368)

文章存档

2017年(9)

2016年(19)

2015年(3)

2014年(6)

2013年(8)

2012年(78)

2011年(66)

2010年(135)

2009年(44)

分类: LINUX

2010-11-09 16:45:29

Centos下利用mysql-mmm实现mysql高可用

 

原创作品:转载请注明出处!飞鸿无痕

http://blog.chinaunix.net/u3/93755/showart.php?id=2390364

一、MMM简介:

MMMMaster-Master Replication Manager for MySQLmysql主主复制管理器)关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。

MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。

MMM项目来自 Google

官方网站为:

Mmm主要功能由下面三个脚本提供

l         mmm_mond  负责所有的监控工作的监控守护进程,决定节点的移除等等

l         mmm_agentd  运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给监控节点

l         mmm_control  通过命令行管理mmm_mond进程

 

二、mysql-mmm架构的搭建

1、  先介绍下本文的环境:

系统环境:CentOS release 5.432bit

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       server1server2上安装mysql,并配置为master-master架构(就是互为主从)----------配置很简单,就不对着部分进行详细解释,有问题的话请查看:http://blog.chinaunix.net/u3/93755/showart.php?id=2213538

u       server1server2server3上安装mmm,并配置:mmm_common.confmmm_agent.conf以及mmm_mon.conf文件

3、  Mysql-mmm实战

前提:server1server2上已经配置好mysql主主同步

u       安装mysql-mmm

CentOS软件仓库默认是不含这些软件的,必须要有epel这个包的支持。故我们必须先安装epel

wget  

rpm -Uvh epel-release-5-4.noarch.rpm

yum -y install mysql-mmm*

u       配置mmm代理和监控账号的权限

server1server2上分别执行:

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

 

    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用户密码

 

    ip      192.168.1.161      #db1ip

    mode    master

    peer    db2

 

    ip      192.168.1.162      #db2ip

    mode    master

    peer    db1

 

    hosts   db1, db2

    ips     192.168.1.113      #设置写如的虚拟IP

    mode    exclusive

 

    hosts   db1, db2

    ips     192.168.1.111, 192.168.1.112     #设置读取的虚拟IP

    mode    balanced

 

在数据库主机上我们需要编辑/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

 

    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_user        mmm_monitor    #mmm_monitor用户名

    monitor_password    monitor_password #mmm_monitor密码

 

debug 0

 

u       启动MMM

启动代理:

(在数据库服务器上server12)编辑/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 [|all [|all]] - show checks status

#显示检查状态,包括(pingmysqlrep_threadsrep_backlog

set_online                  - set host online

#设置某hostonline状态

set_offline                 - set host offline

#设置某hostoffline状态

mode                              - print current mode.

#打印当前的模式,是ACTIVEMANUALPASSIVE

#默认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] - move exclusive role to host

       #更改host的模式,比如更改处于slavemysql数据库角色为write  

   (Only use --force if you know what you are doing!)

set_ip                 - set role with ip to host

#host设置ip,只有passive模式的时候才允许更改!

阅读(17821) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~