一、架构图
二、配置步骤
所需软件
Mysql-5.6.20
mysql-mmm-agent-2.2.1
mysql-mmm-2.2.1
mysql-mmm-monitor-2.2.1
keepalived-1.2.13
安装步骤
1.四台机器安装mysql数据库(可直接yum安装或者编译安装)
2.设置主从关系
function | IP | Server Name | server id |
monitor | 192.168.1.234 | - | - |
monitor | 192.168.1.241 | ||
master | 192.168.1.242 | db1 | 1 |
master | 192.168.1.240 | db2 | 2 |
slave | 192.168.1.237 | db3 | 3 |
slave | 192.168.1.238 | db4 | 4 |
使用mysql-mmm时一共需要三个用户:
replication、mmm_agent和mmm_monitor(管理服务器上用来监控cluster状态的用户,所以可以限定只能从管理服务器登录)。使用下面三条命令新建这三个用户并分配相应的权限:
GRANT REPLICATION CLIENT ON *.* TO ‘mmm_monitor‘@‘192.168.1.%‘ IDENTIFIED BY ‘RepMonitor‘; GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO ‘mmm_agent‘@‘192.168.1.%‘ IDENTIFIED BY ‘RepAgent‘; grant replication slave on *.* to replication@‘192.168.1.%‘ identified by ‘replication‘;
分别查看两个mysql master的状态:
show master status\G
然后使用如下语句
change master to master_host=‘192.168.1.242‘, master_user=‘replication‘, master_password=‘replication‘, master_log_file=‘mysql-bin.000005‘, master_log_pos=1857;
来切换从库到主库的同步状态,当两个主库相互设置好了之后,设置两个从库,可以在两个主库中任意选择一个当做自己的同步主库。都设置好了之后,使用命令start slave;命令来开启从库状态,在同步之前,注意防火墙的设置。然后在四台mysql服务器上使用命令show slave status\G来查看是否同步成功,Slave_IO_Running: Yes、Slave_SQL_Running: Yes,如果这两个参数显示yes,表示从库同步设置成功。
3.安装mysql-mmm
在管理服务器(192.168.1.234和192.168.1.241)上分别安装
yum -y install mysql-mmm-monitor* 和 yum -y install perl-Time-HiRes*
在四台mysql服务器上分别安装
yum -y install mysql-mmm-agent*
4.设置mysql-mmm
a).
vi /etc/mysql-mmm/mmm_common.conf
配置文件如下,6台服务器的此文件到要一样,可以先做好一份,直接复制就ok了
################################################## 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 replication agent_user mmm_agent agent_password RepAgent </host> <host db1> ip 192.168.1.242 mode master peer db2 </host> <host db2> ip 192.168.1.240 mode master peer db1 </host> <host db3> ip 192.168.1.237 mode slave </host> <host db4> ip 192.168.1.238 mode slave </host> <role writer> hosts db1, db2 ips 192.168.1.243 mode exclusive </role> <role reader> hosts db1, db2, db3, db4 ips 192.168.1.244, 192.168.1.245, 192.168.1.246, 192.168.1.247 mode balanced </role> ###################################################################
b). 设置mmm_agent配置文件(在4台mysql服务器上面设置),
vi /etc/mysql-mmm/mmm_agent.conf
配置文件如下,主要修改this dbx,根据mmm_common.conf的内容指定到底是db几。
###################################################### include mmm_common.conf # The ‘this‘ variable refers to this server. Proper operation requires # that ‘this‘ server (db1 by default), as well as all other servers, have the # proper IP addresses set in mmm_common.conf. this db1 #########################################################################
c). 设置两台monitor的配置文件,
vi /etc/mysql-mmm/mmm_mon.conf
配置文件如下,
################################################################ include mmm_common.conf <monitor> ip 192.168.1.234 #这个ip设置monitor自己服务器的ip 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.242, 192.168.1.240, 192.168.1.237, 192.168.1.238 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 monitor_password RepMonitor </host> debug 0 #######################################################################
d). 分别启动mmm_agent和mmm_monitor,
(启动客户端)
/etc/init.d/mysql-mmm-agent start
(启动服务器监控端)
/etc/init.d/mysql-mmm-monitor start
e). 查看mysql-mmm是否起作用,在monitor端使用命令,
mmm_control show db1(192.168.1.242) master/ONLINE. Roles: reader(192.168.1.244) db2(192.168.1.240) master/ONLINE. Roles: reader(192.168.1.246), writer(192.168.1.243) db3(192.168.1.237) slave/ONLINE. Roles: reader(192.168.1.245) db4(192.168.1.238) slave/ONLINE. Roles: reader(192.168.1.247)
如果显示如此表示正常。
5.安装设置keepalived
分别在192.168.1.234和192.168.1.241上面安装keepalived,可用yum直接安装
分别设置配置文件,配置如下
vi /etc/keepalived/keepalived.conf
192.168.1.234的配置文件为:
############################################################### ! Configuration File for keepalived global_defs { # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_down { #script "netstat -apn | grep mmm_mond;if [[ $? -eq 1 ]];then exit 1;else exit 0;fi " script "netstat -apn | grep mmm_mond" interval 2 weight -30 fall 3 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 # nopreempt priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.249/24 dev eth0 label eth0:1 } track_interface { eth0 } track_script { chk_down } debug notify_master "/etc/keepalived/mmm_restart.sh" notify_backup "/etc/keepalived/mmm_stop.sh" # notify_fault "/etc/keepalived/mmm_stop.sh" } ################################################################
192.168.1.241的配置文件为:
################################################################## ! Configuration File for keepalived global_defs { # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_down { #script "netstat -apn | grep mmm_mond;if [[ $? -eq 1 ]];then exit 1;else exit 0;fi " script "netstat -apn | grep mmm_mond" interval 2 weight -10 fall 3 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 # nopreempt priority 85 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.249/24 dev eth0 label eth0:1 } track_interface { eth0 } track_script { chk_down } debug notify_master "/etc/keepalived/mmm_restart.sh" notify_backup "/etc/keepalived/mmm_stop.sh" # notify_fault "/etc/keepalived/mmm_stop.sh" } ############################################################
/etc/keepalived/mmm_restart.sh内容为:
#!/bin/bash service mysql-mmm-monitor start
############################################################
/etc/keepalived/mmm_stop.sh内容为:
#!/bin/bash service mysql-mmm-monitor stop
分别启动keepalived,命令为
/etc/init.d/keepalived start
到此所有配置结束。
三、重点说明
Mysql-mmm使用了虚拟ip的概念,故安装mmm_common.conf的配置,mysql提供对外写的虚拟ip为192.168.1.243,提供对外读的虚拟ip为192.168.1.244, 192.168.1.245, 192.168.1.246, 192.168.1.247,请程序段合理利用。
此架构的好处是,主库有两个,任意一个主库down掉,另外一台主库都能够及时对接,这样mysql数据写入形成冗余高可用性,而且当一台主库挂掉后,mmm架构也会及时的修改从库的同步主库ip,会及时的自动化修改从库的同步ip到正常工作的主库对应的ip中去。这样主从同步就不会出问题,保持主从数据的正确性,而且利用了虚拟ip的概念,提供读写分离功能。
此架构的一个缺点就是monitor端只有一个,此为单点,后期考虑到冗余性,所以加上了keepalived,这样当一台monitor端挂掉后,利用keepalived的高可用性和failover特性,可以成功的启动另外一台备用机器上的monitor,达到冗余效果。
本文出自 “郑小明的技术博客” 博客,请务必保留此出处http://zhengmingjing.blog.51cto.com/1587142/1860643
Mysql-mmm兼Keepalived高可用性mysql底层架构
原文:http://zhengmingjing.blog.51cto.com/1587142/1860643