基于mysql-mmm实现对mysql replication进行监控和故障迁移


更多博文请关注:没有伞的孩子必须努力奔跑 (www.xuchanggang.cn)


一、mysql-mmm简述:

     mysq-mmm英译:MYSQL-MMM(Mysql Master-Master replication manager for Mysql)是一套基于perl编写的脚本程序(这也是我们配置时需要安装perl相关的依赖包),用来对 mysql replication 进行监控和故障迁移,并能管理 mysql Master-Master 复制的配置(同一时间只有一个节点是可写的,在DB server宕掉后,会自动帮你重新配置主从),附带的工具套件可以实现多个 slaves 的 read 负载均衡,因此你可以使用这个工具移除一组服务器中复制延迟较高的服务器的虚拟 IP,它还可以备份数据,两节点之间再同步等等。
二、mysql-mmm命令的组成部分及原理,相关用户简介[基于c/s架构,就是说代理端需要配置agent,监管端需要配置monitor]:
1.命令构成:
     mmm_mond :监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行。
     mmm_agentd :运行在每个 mysql 服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置 。此脚本需要在被监管机上运行。
     mmm_control :一个简单的脚本,提供管理 mmm_mond 进程的命令
2.mysql-mmm的原理:
     mysql-mmm 的监管端会提供多个虚拟 IP(VIP),包括一个可写 VIP,多个可读VIP,通过监管的管理,这些 IP 会绑定在可用 mysql 之上,当某一台 mysql 宕机时,监管机会将 VIP 迁移至其他 mysql。在整个监管过程中,需要在 mysql 中添加相关授权用户,以便让 mysql 可以支持监理机的维护。授权的用户包括一个 mmm_monitor 用户和一个 mmm_agent 用户。
3.相关用户简介:
   monitor user(这里使用:mmm_monitor): mmm 监控用于对 mysql 服务器进程健康检查,需要具有 REPLICATION CLIENT
   agent user(这里使用:mmm_agent): mmm 代理用来更改只读模式,复制的主服务器等等,需要具有 SUPER, REPLICATION CLIENT, PROCESS
   relication user(这里使用:slave): 用于复制,需要具有 REPLICATION SLAVE
4.官方配置文档:
   mysql-mmm官方文档: http://mysql-mmm.org/mmm2:guide
三、本次测试环境拓扑图:
203725478.png
四、mysql-mmm配置实例:
1.测试环境:
  系统:red hat linux 6(2.6.32)
  数据库:mysql 5.1.61(这里直接使用rpm包安装,也不使用最新版本数据库)
  MMM:mysql-mmm  2.2.21
  DB server地址分配:
         192.168.1.100(master)
         192.168.1.101(master)
         192.168.1.102(slave)
         192.168.1.103(slave)
  MMM地址:
         192.168.1.104(monitor)
  虚拟IP地址(vip):
         192.168.1.12(write)
         192.168.1.13(read)
         192.168.1.14(read)
         192.168.1.15(read)
2.在DB server机器上安装、配置mysql数据库
[root@client100 ~]# yum -y install mysql mysql-server
[root@client101 ~]# yum -y install mysql mysql-server
[root@client102 ~]# yum -y install mysql mysql-server
[root@client103 ~]# yum -y install mysql mysql-server
# 设置1台DB server(192.168.1.100)的登陆密码[其他机器使用备份恢复即可]
[root@client100 ~]# mysqladmin -uroot password 'kongzhong'
# 修改192.168.1.100数据库配置文件
[root@client100 ~]# vim /etc/my.cnf
# 添加或修改如下参数
max_connections=1400
binlog-format='row'
server_id= 100
log_bin= /var/lib/mysql/mysql-100-bin.log
log_bin_index= /var/lib/mysql/mysql-bin.log.index
relay_log= /var/lib/mysql/mysql-relay-bin
relay_log_index= /var/lib/mysql/mysql-relay-bin.index
expire_logs_days= 10
max_binlog_size= 100M
log_slave_updates= 1
sync-binlog=1
auto_increment_increment=2
auto_increment_offset=1
# skip-name-resolve 这个参数可以选配,如果不使用,则host配置文件必须有所有dbserver对应解析关系
skip-name-resolve
# 修改192.168.1.101数据库配置文件
[root@client101 ~]# vim /etc/my.cnf
max_connections=1400
binlog-format='row'
server_id= 101
log_bin= /var/lib/mysql/mysql-101-bin.log
log_bin_index= /var/lib/mysql/mysql-bin.log.index
relay_log= /var/lib/mysql/mysql-relay-bin
relay_log_index= /var/lib/mysql/mysql-relay-bin.index
expire_logs_days= 10
max_binlog_size= 100M
log_slave_updates= 1
sync-binlog=1
auto_increment_increment=2
# 这个参数值和上面的值不一样
auto_increment_offset=2
skip-name-resolve
# 修改192.168.1.102数据库配置文件
[root@client102 ~]# vim /etc/my.cnf
max_connections=1400
server_id= 102
log_bin= /var/lib/mysql/mysql-102-bin.log
log_bin_index= /var/lib/mysql/mysql-bin.log.index
relay_log= /var/lib/mysql/mysql-relay-bin
relay_log_index= /var/lib/mysql/mysql-relay-bin.index
log_slave_updates= 1
# 修改192.168.1.103数据库配置文件
[root@client103 ~]# vim /etc/my.cnf
max_connections=1400
server_id= 103
log_bin= /var/lib/mysql/mysql-103-bin.log
log_bin_index= /var/lib/mysql/mysql-bin.log.index
relay_log= /var/lib/mysql/mysql-relay-bin
relay_log_index= /var/lib/mysql/mysql-relay-bin.index
log_slave_updates= 1
# 启动所有DB server上的mysql数据库
[root@client100 ~]# /etc/init.d/mysqld start
[root@client101 ~]# /etc/init.d/mysqld start
[root@client102 ~]# /etc/init.d/mysqld start
[root@client103 ~]# /etc/init.d/mysqld start
# 登陆192.168.1.100上的数据库,做授权操作(授权用户包括复制的用户,mmm需要的监控,代理用户等)
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by 'kongzhong';
mysql> grant replication client on *.* to 'mmm_monitor'@'192.168.1.%' identified by 'kongzhong';
mysql> grant replication client,process,super on *.* to 'mmm_agent'@'192.168.1.%' identified by 'kongzhong';
mysql> flush privileges;
# 备份192.168.1.100的数据,并传送到其他几台DB server上
[root@client100 ~]# mysqldump -uroot -p --all-databases --lock-all-tables --flush-logs >/tmp/all.sql
Enter password:
[root@client100 ~]# scp /tmp/all.sql 192.168.1.101:/tmp/
[email protected]'s password:
all.sql                                                                                                                    100%  495KB 495.3KB/s   00:00  
[root@client100 ~]# scp /tmp/all.sql 192.168.1.102:/tmp/
[email protected]'s password:
all.sql                                                                                                                    100%  495KB 495.3KB/s   00:00  
[root@client100 ~]# scp /tmp/all.sql 192.168.1.103:/tmp/
[email protected]'s password:
all.sql                                                                                                                    100%  495KB 495.3KB/s   00:00
3.配置DB server 的主从关系
(1).在192.168.1.101,192.168.1.102,192.168.1.103利用备份还原数据库,并重启数据库[这里不演示,大家都会]
(2).在192.168.1.100上查看当前日志文件的位置
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-100-bin.000003 |      106 |              |                  |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
(3).在192.168.1.101,192.168.1.102,192.168.1.103配置与192.168.1.100的主从关系,语句如下:
mysql> CHANGE MASTER TO master_host='192.168.1.100',
    -> master_port=3306,
    -> master_user='slave',
    -> master_password='kongzhong',
    -> master_log_file='mysql-100-bin.000003',
    -> master_log_pos=106;
mysql> start slave;
mysql> show slave start\G;
# 配置完主从,查看下面两个参数的值是否为yes,根据提示排错
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
(4).配置192.168.1.100与192.168.1.01互为主从
# 在192.168.1.101上查看当前日志文件的位置
mysql> show master status;
+----------------------+----------+--------------+------------------+
| File                 | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-101-bin.000004 |   996936 |              |                  |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
# 在192.168.1.100上,执行如下语句
mysql>CHANGE MASTER TO master_host='192.168.1.101',
    -> master_port=3306,
    -> master_user='slave',
    -> master_password='kongzhong',
    -> master_log_file='mysql-101-bin.000004',
    -> master_log_pos=996936;
mysql> start slave;
mysql> show slave start\G;
# 配置完主从,查看下面两个参数的值是否为yes,根据提示排错
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
# 以上配置完之后,大家可以在主DB server创建库来验证主从的有效性(这里不演示了)
4.在所有机器上安装如下软件:
# 所有机器(包括dbserver和监控机器)都需安装[这里指演示在一台机器上安装]
[root@client104 ~]# yum -y install  libart_lgpl perl-Date-Manip perl-XML-DOM perl-XML-DOM-XPath perl-XML-Parser perl-XML-RegExp rrdtool perl-Class-Singleton  perl perl-DBD-MySQL perl-Params-Validate perl-MailTools perl-Time-HiRes
# 以下几个软件,本地光盘上是没有的,需要使用网络yum源[epel],我这里下载好了
# 下载地址如下:http://www.kuaipan.cn/file/id_119710994921422892.htm
[root@client104 ~]# yum -y install perl-Algorithm-Diff-1.1902-9.el6.noarch.rpm perl-IPC-Shareable-0.60-2.el6.rf.noarch.rpm perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm perl-Log-Log4perl-1.26-1.el6.rf.noarch.rpm perl-Net-ARP-1.0.6-2.1.el6.x86_64.rpm perl-Proc-Daemon-0.06-1.el6.noarch.rpm perl-Proc-ProcessTable-0.44-4.el6.x86_64.rpm rrdtool-perl-1.3.8-6.el6.x86_64.rpm
# 数据库端需要安装
[[email protected] ~]#yum -y install mysql-connector-odbc
5.在所有机器上安装mysql-mmm软件:
# 安装mysql-mmm,可以使用epel提供的rpm包安装
# 这里不使用rpm安装,软件都已共享,自行下载,在每台机器上安装mysql-mmm[这里仅演示在1台机器上mysql-mmm]
[root@client104 ~]# tar -xf mysql-mmm-2.2.1.tar.gz
[root@client104 ~]# cd mysql-mmm-2.2.1
[root@client104 mysql-mmm-2.2.1]# make install
# 配置mmm_common.conf,并传送到每台机器上对应目录[包括监控机]
[root@client104 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_common.conf
active_master_role      writer
<host default>
        cluster_interface               eth0
        pid_path                                /var/run/mmm_agentd.pid
        bin_path                                /usr/lib/mysql-mmm/
# 用于主从复制的账户和密码
        replication_user        slave
        replication_password    kongzhong
# 代理端的账户和密码[这些账户,上面都已做过授权,不清楚,可以回头看看]
        agent_user                              mmm_agent
        agent_password                          kongzhong
</host>
# 配置第一台DB server,这里的ip地址写真实数据库IP地址,模式为主dbserver
<host db1>
        ip                                      192.168.1.100
        mode                                    master
        peer                                    db1
</host>
# 配置第二台DB server,模式为主dbserver
<host db2>
        ip                                      192.168.1.101
        mode                                    master
        peer                                    db2
</host>
# 配置第三台DB server,模式为从dbserver
<host db3>
        ip                                      192.168.1.102
        mode                                    slave
</host>
# 配置第四台DB server,模式为从dbserver
<host db4>
        ip                                      192.168.1.103
        mode                                    slave
</host>
# 配置可写的DB server(即master dbserver),这里的ip为虚拟ip地址
<role writer>
        hosts                                   db1, db2
        ips                                    192.168.1.12
        mode                                    exclusive
</role>
# 配置可读的DB server(可以是所有,也可以是部分),这里的ip为虚拟ip地址,模式为轮询,
<role reader>
        hosts                                   db1, db2, db3, db4
        ips                                     192.168.1.13, 192.168.1.14, 192.168.1.15,192.168.1.16
        mode                                    balanced
</role>
# 上面的配置文件传送到各个主机
[root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf  192.168.1.101:/etc/mysql-mmm/mmm_common.conf
[root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf  192.168.1.102:/etc/mysql-mmm/mmm_common.conf
[root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf  192.168.1.103:/etc/mysql-mmm/mmm_common.conf
[root@client104 mysql-mmm-2.2.1]# scp /etc/mysql-mmm/mmm_common.conf  192.168.1.100:/etc/mysql-mmm/mmm_common.conf

6.分别修改192.168.1.101,192.168.1.102,192.168.1.103,192.168.1.100的mmm_agent.conf文件,注意this 部分
[root@client100 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1
[root@client101 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db2
[root@client102 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db3
[root@client103 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db4
7.修改监控端(192.168.1.104)的mmm_mon.conf文件[此文件仅监控端需要配置]
[root@client104 mysql-mmm-2.2.1]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
# 以下IP均为真实IP地址
<monitor>
        ip                                          192.168.1.104
        pid_path                                /var/run/mmm_mond.pid
        bin_path                                /usr/lib/mysql-mmm/
        status_path                             /var/lib/misc/mmm_mond.status
        ping_ips                                192.168.1.100, 192.168.1.101, 192.168.1.102, 192.168.1.103 
</monitor>
# 监管的账号密码
<host default>
        monitor_user                    mmm_monitor
        monitor_password                kongzhong
</host>
debug 0

8.dbserver启动agent,监管端启动monitor
[root@client100 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
[root@client101 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
[root@client102 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
[root@client103 ~]# /etc/init.d/mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok
# 监管端启动
[root@client104 mysql-mmm-2.2.1]# /etc/init.d/mysql-mmm-monitor start
Daemon bin: '/usr/sbin/mmm_mond'
Daemon pid: '/var/run/mmm_mond.pid'
Starting MMM Monitor daemon: Ok
# 查看状态[第一次需要激活dbserver]
[root@client104 ~]# mmm_control show
  db1(192.168.1.100) master/AWAITING_RECOVERY. Roles:
  db2(192.168.1.101) master/AWAITING_RECOVERY. Roles:
  db3(192.168.1.102) slave/AWAITING_RECOVERY. Roles:
  db4(192.168.1.103) slave/AWAITING_RECOVERY. Roles:
# 激活所有dbserver
[root@client104 ~]# mmm_control set_online db1
[root@client104 ~]# mmm_control set_online db2
[root@client104 ~]# mmm_control set_online db3
[root@client104 ~]# mmm_control set_online db4
# 再次查看
[root@client104 ~]# mmm_control show
  db1(192.168.1.100) master/ONLINE. Roles: reader(192.168.1.14)
  db2(192.168.1.101) master/ONLINE. Roles: reader(192.168.1.13), writer(192.168.1.12)
  db3(192.168.1.102) slave/ONLINE. Roles: reader(192.168.1.16)
  db4(192.168.1.103) slave/ONLINE. Roles: reader(192.168.1.15)
# 现在mmm就配置好,大家可以自行测试
# 我的测试方法为:停掉192.168.1.101的dbserver 查看虚拟ip变化,其他几台dbserver复制的变化
# 启动192.168.1.101的dbserver,停掉192.168.1.100,查看虚拟ip的变化,特别看两台slave上复制主服务器的指向

五、配置中遇到的问题
1.问题1报错如下:
  ERROR: Can't connect to monitor daemon!
  可以 把debug=1 再去看日志文件,由哪些问题引起的。
2.问题2如下:
  启动monitor ,ok。设置成debug模式。可以看到这样的错误:
   DEBUG mysql(db2) = 'ERROR: Connect error (host = 192.168.1.100:3306, user = mmm_monitor)! Can't connect to MySQL server on '192.168.1.100' (4)'
# 这个问题搞了半天才解决,希望大家注意:
 原因:因为你的agent配置文件里写的是this db1 .this db2.在host里没有对应的解析.所以无法连接。
在/etc/hosts 添加如下语句即可[记得传送到所有机器上]:
192.168.1.100 client100.kongzhong.com
192.168.1.101 client101.kongzhong.com
192.168.1.102 client102.kongzhong.com
192.168.1.103 client103.kongzhong.com
192.168.1.104 client104.kongzhong.com

或者 在my.cnf 加入 skip-name-resolve 这个方法也是可行的!

你可能感兴趣的:(amoeba,mysql-mmm,故障迁移)