星空网 > 软件开发 > 数据库

MySQL MMM高可用方案

介绍

本篇文章主要介绍搭建MMM方案以及MMM架构的原理。这里不介绍主从、主主的搭建方法,MMM方案不适用对数据一致性要求很高的业务。

 

 

 架构

MySQL MMM高可用方案

MySQL MMM高可用方案

部署

1.修改hosts

在所有的服务器中执行相同的操作。

vim /etc/hosts

192.168.137.10 master192.168.137.20 backup192.168.137.30 slave192.168.137.40 monitor

2.添加mysql用户

只需要在所有的数据库端执行即可,监控端不需要。

GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.137.%' IDENTIFIED BY 'mmm_monitor'; GRANT SUPER,REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.137.%' IDENTIFIED BY 'mmm_agent';flush privileges;

MySQL MMM高可用方案

 注意:repl用户在搭建主从服务的时候已经创建好了。

 3.安装监控软件

注意:监控端全部安装,但是监控端只需要用到mysql-mmm-monitor,mysql-mmm-agent不需要启动。数据库端只需要安装mysql-mmm-agent

1.在监控服务器执行

wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm –ivh epel-release-6-8.noarch.rpmyum –y install mysql-mmm*

2.在数据库服务器上执行,每个数据库服务器上都要执行

wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpmrpm –ivh epel-release-6-8.noarch.rpmyum -y install mysql-mmm-agent

路径说明:

MySQL MMM高可用方案

4.配置文件

db服务器配置文件:mmm_agent.conf,mmm_common.conf

监控服务器的配置文件:mmm_mon.conf,mmm_common.conf(该文件所有服务器一样)

配置db服务器

1.修改master服务器

vim /etc/mysql-mmm/mmm_common.conf

active_master_role   writer   ###积极的master角色的标示,所有的db服务器都需要开启read_only参数,对于writer服务器监控代理会自动将read_only属性关闭。<host default>  cluster_interface    eth0   #####群集的网络接口  pid_path        /var/run/mysql-mmm/mmm_agentd.pid  ####pid路径  bin_path        /usr/libexec/mysql-mmm/       #####可执行文件路径   replication_user    repl      #######复制用户  replication_password  repl      #######复制用户密码  agent_user       mmm_agent   #######代理用户,用于更改只读操作  agent_password     mmm_agent    #######代理用户密码</host><host master>      ##########master1的host名  ip   192.168.137.10  #####master1的ip  mode  master    ########角色属性,master代表是主  peer  backup    ########与master1对等的服务器的host名,也就是master2的服务器host名</host><host backup>   ####和master的概念一样  ip   192.168.137.20  mode  master  peer  master</host><host slave>   #####从库的host名,如果存在多个从库可以重复一样的配置  ip   192.168.137.30  ####从的ip  mode  slave  #####slave的角色属性代表当前host是从</host><role writer>  ####writer角色配置  hosts  master,backup  ####能进行写操作的服务器的host名,如果不想切换写操作这里可以只配置master,这样也可以避免因为网络延时而进行write的切换,但是一旦master出现故障那么当前的MMM就没有writer了只有对外的read操作。  ips   192.168.137.100 #####对外提供的写操作的虚拟IP  mode  exclusive  #####exclusive代表只允许存在一个主,也就是只能提供一个写的IP</role><role reader>  #####read角色配置  hosts  backup,slave ######对外提供读操作的服务器的host名,当然这里也可以把master加进来  ips   192.168.137.120,192.168.137.130,192.168.137.140 ###对外提供读操作的虚拟ip,这两个ip和host不是一一对应的,并且ips也hosts的数目也可以不相同,如果这样配置的话其中一个hosts会分配两个ip  mode  balanced  ###balanced代表负载均衡</role>

同时将这个文件拷贝到其它的服务器包括监控服务器,配置不变

scp /etc/mysql-mmm/mmm_common.conf slave:/etc/mysql-mmm/scp /etc/mysql-mmm/mmm_common.conf backup:/etc/mysql-mmm/scp /etc/mysql-mmm/mmm_common.conf monitor:/etc/mysql-mmm/

 2.代理文件配置

vim /etc/mysql-mmm/mmm_agent.conf

 MySQL MMM高可用方案

注意:这个配置只配置db服务器,监控服务器不需要配置,this后面的host名改成当前服务器的host命令,master2和slave也改成对应的服务器的host名。

3.启动代理进程

 

chkconfig mysql-mmm-agent on

service mysql-mmm-agent start

每台db服务器上都需要启动

配置监控服务器

vim /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 #####群集的状态文件,也就是执行mmm_control show操作的显示来源。  ping_ips      192.168.137.10,192.168.137.20,192.168.137.30 ######被监控的db服务器的ip地址  auto_set_online   0  ####设置自动online的时间,默认是超过60s就将它设置为online,默认是60s,这里将其设为0就是立即online  # 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  ####监控db服务器的用户  monitor_password  mmm_monitor  ####监控db服务器的密码</host>debug 0    #######debug 0正常模式,1为debug模式

注意:配置文件中的注释是为了便于理解,在部署的时候最好把注释去掉以免因为注释造成的潜在影响。

启动监控进程

chkconfig mysql-mmm- monitor onservice mysql-mmm-monitor start

注意:无论是在db端还是在监控端如果有对配置文件进行修改操作都需要重启代理进程和监控进程。

 

操作分析

日志文件

日志文件往往是分析错误的关键,所以要善于利用日志文件进行问题分析。

db端:/var/log/mysql-mmm/mmm_agentd.log

监控端:/var/log/mysql-mmm/mmm_mond.log

命令文件

mmm_agentd :db代理进程的启动文件mmm_mond :监控进程的启动文件  mmm_backup :备份文件 mmm_restore :还原文件mmm_clone  mmm_control:监控操作命令文件  

 

db服务器端只有mmm_agentd程序,其它的都是在monitor服务器端。

mmm_control用法

mmm_control程序可以用于监控群集状态、切换writer、设置online\offline操作等。

Valid commands are:  help               - show this message  ###帮助信息  ping               - ping monitor ###ping当前的群集是否正常  show               - show status ####群集在线状态检查  checks [<host>|all [<check>|all]] - show checks status #####执行监控检查操作  set_online <host>         - set host <host> online ####将host设置为online  set_offline <host>        - set host <host> offline ###将host设置为offline  mode               - print current mode. ####打印输出当前的mode  set_active            - switch into active mode.  set_manual            - switch into manual mode.  set_passive            - switch into passive mode.  move_role [--force] <role> <host> - move exclusive role <role> to host <host> ####移除writer服务器为指定的host服务器                    (Only use --force if you know what you are doing!)  set_ip <ip> <host>        - set role with ip <ip> to host <host>

1.检查所有的db服务器群集状态

[root@monitor mysql-mmm]# mmm_control checks allmaster ping     [last change: 2016/06/07 16:31:24] OKmaster mysql    [last change: 2016/06/07 16:31:24] OKmaster rep_threads [last change: 2016/06/07 16:31:24] OKmaster rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is nullslave  ping     [last change: 2016/06/07 16:31:24] OKslave  mysql    [last change: 2016/06/07 16:31:24] OKslave  rep_threads [last change: 2016/06/07 16:31:24] OKslave  rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is nullbackup ping     [last change: 2016/06/07 16:31:24] OKbackup mysql    [last change: 2016/06/07 16:31:24] OKbackup rep_threads [last change: 2016/06/07 16:31:24] OKbackup rep_backlog [last change: 2016/06/07 16:31:24] OK: Backlog is null

检查项包括:ping、mysql是否正常运行、复制线程是否正常等

2.检查群集环境在线状况

[root@monitor mysql-mmm]# mmm_control show backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120) master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

3.对指定的host执行offline操作

[root@monitor mysql-mmm]# mmm_control set_offline backupOK: State of 'backup' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles![root@monitor mysql-mmm]# mmm_control show backup(192.168.137.20) master/ADMIN_OFFLINE. Roles:  master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.120), reader(192.168.137.130)

4.对指定的host执行onine操作

[root@monitor mysql-mmm]# mmm_control set_online backupOK: State of 'backup' changed to ONLINE. Now you can wait some time and check its new roles![root@monitor mysql-mmm]# mmm_control show backup(192.168.137.20) master/REPLICATION_FAIL. Roles:  master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.120), reader(192.168.137.130)[root@monitor mysql-mmm]# mmm_control show backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120) master(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

5.执行write切换

1.查看当前的slave对应的master

[root@slave ~]# mysql -uroot -proot -e 'show slave status \G;'Warning: Using a password on the command line interface can be insecure.*************************** 1. row ***************************        Slave_IO_State: Waiting for master to send event         Master_Host: 192.168.137.10         Master_User: repl         Master_Port: 3306        Connect_Retry: 60       Master_Log_File: mysql-bin.000073     Read_Master_Log_Pos: 1461        Relay_Log_File: mysql-relay-bin.000006        Relay_Log_Pos: 283    Relay_Master_Log_File: mysql-bin.000073       Slave_IO_Running: Yes      Slave_SQL_Running: Yes

2.writer切换,要确保mmm_common.conf文件中的writer属性有配置对应的host,否则无法切换

[root@monitor mysql-mmm]# mmm_control move_role writer backupOK: Role 'writer' has been moved from 'master' to 'backup'. Now you can wait some time and check new roles info![root@monitor mysql-mmm]# mmm_control show backup(192.168.137.20) master/ONLINE. Roles: reader(192.168.137.120), writer(192.168.137.100) master(192.168.137.10) master/ONLINE. Roles:  slave(192.168.137.30) slave/ONLINE. Roles: reader(192.168.137.130)

3.save从库自动切换到了新的master

[root@slave ~]# mysql -uroot -proot -e 'show slave status \G;'Warning: Using a password on the command line interface can be insecure.*************************** 1. row ***************************        Slave_IO_State: Waiting for master to send event         Master_Host: 192.168.137.20         Master_User: repl         Master_Port: 3306        Connect_Retry: 60       Master_Log_File: mysql-bin.000039     Read_Master_Log_Pos: 120        Relay_Log_File: mysql-relay-bin.000002        Relay_Log_Pos: 283    Relay_Master_Log_File: mysql-bin.000039       Slave_IO_Running: Yes      Slave_SQL_Running: Yes

 

其它处理问题

 如果不想让writer从master切换到backup(包括主从的延时也会导致写VIP的切换),那么可以在配置/etc/mysql-mmm/mmm_common.conf时,去掉<role write>中的backup

<role writer>  ####writer角色配置  hosts  master  ###这里只配置一个Hosts  ips   192.168.137.100 #####对外提供的写操作的虚拟IP  mode  exclusive  #####exclusive代表只允许存在一个主,也就是只能提供一个写的IP</role>

 这样的话当master出现故障了writer写操作不会切换到backup服务器,并且slave也不会指向新的master,此时当前的MMM之前对外提供写服务。

 

之前写的复制相关文章:

主从复制:http://www.cnblogs.com/chenmh/p/5089919.html

主主复制:http://www.cnblogs.com/chenmh/p/5153184.html

总结

主从的延时会导致写VIP的切换,下一篇文章会写关于半同步复制结合MMM方案,可以更有效的解决切换数据不一致性的问题,欢迎关注。

1.对外提供读写的虚拟IP是由monitor程序控制。如果monitor没有启动那么db服务器不会被分配虚拟ip,但是如果已经分配好了虚拟ip当monitor程序关闭了原先分配的虚拟ip不会立即关闭外部程序还可以连接访问(只要不重启网络),这样的好处就是对于monitor的可靠性要求就会低一些,目前还不知道能维持多长的时间,但是如果这个时候其中的某一个db服务器故障了就无法处理切换,也就是原先的虚拟ip还是维持不变,挂掉的那台DB的虚拟ip会变的不可访问。

2.agent程序受monitor程序的控制处理write切换,从库切换等操作。如果monitor进程关闭了那么agent进程就起不到什么作用,它本身不能处理故障。

3.monitor程序负责监控db服务器的状态,包括Mysql数据库、服务器是否运行、复制线程是否正常、主从延时等;它还用于控制agent程序处理故障。

4.monitor会每隔几秒钟监控db服务器的状态,如果db服务器已经从故障变成了正常,那么monitor会自动在60s之后将其设置为online状态(默认是60s可以设为其它的值),有监控端的配置文件参数“auto_set_online”决定,群集服务器的状态有三种分别是:HARD_OFFLINE→AWAITING_RECOVERY→online

5.默认monitor会控制mmm_agent会将writer db服务器read_only修改为OFF,其它的db服务器read_only修改为ON,所以为了严谨可以在所有的服务器的my.cnf文件中加入read_only=1由monitor控制来控制writer和read,root用户和复制用户不受read_only参数的影响

 

备注:

    作者:pursuer.chen

    博客:http://www.cnblogs.com/chenmh

本站点所有随笔都是原创,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接。

《欢迎交流讨论》






原标题:MySQL MMM高可用方案

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流