一、需求说明

最近一直在学习mysql-mmm,想以后这个架构也能用在我们公司的业务上,我们公司的业务是单机多实例部署,所以也想把mysql-mmm部署成这样,功夫不负有心人,我成功了,和大家分享一下:
二、环境说明

集群角色主机名IPMysql PortServer IDVIP WriterVIP READER
navy2Agentdb1172.28.26.101330711172.28.26.107
navy3
Agent
db2172.28.26.10233081
172.28.26.110
navy2
Agent
db1
172.28.26.101
330722
172.28.26.108
navy3
Agent
db2
172.28.26.102
33082
172.28.26.109
navy2/navy3Monitor
Monitor
172.28.26.103
PS:db1和db2上分别有两个库navy2和navy3,互为主主,172.28.26.107是navy2的写入虚拟IP,172.28.26.108是navy2的读虚拟IP,172.28.26.109是navy2的写入虚拟IP,172.28.26.110是navy3的读虚拟IP。
三、部署
1、mysql和mysql-mmm的安装及mysql的主从配置请查看前面的博文:http://navyaijm.blog.51cto.com/4647068/1230674,这里只介绍mmm多实例配置。
2、db1上:
vi /etc/mysql-mmm/mmm_common_navy2.conf(navy2的配置文件)
 
 vi /etc/mysql-mmm/mmm_common_navy2.conf
active_master_role writer
<host default>
cluster_interface eth1
agent_port 9912
mysql_port 3307
pid_path /var/run/mysql-mmm/mmm_agentd_navy2.pid
bin_path /usr/libexec/mysql-mmm/
replication_user slave
replication_password 123456
agent_user mmm_agent
agent_password 123456
</host>
<host db1>
ip 172.28.26.101
mysql_port 3307
mode master
peer db2
</host>
<host db2>
ip 172.28.26.102
mysql_port 3307
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 172.28.26.107
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 172.28.26.108
mode balanced
</role>

vi /etc/mysql-mmm/mmm_common_navy3.conf(navy的配置文件)

 active_master_role      writer
<host default>
cluster_interface eth1
agent_port 9913
mysql_port 3308
pid_path /var/run/mysql-mmm/mmm_agentd_navy3.pid
bin_path /usr/libexec/mysql-mmm/
replication_user slave
replication_password 123456
agent_user mmm_agent
agent_password 123456
</host>
<host db1>
ip 172.28.26.101
mysql_port 3308
mode master
peer db2
</host>
<host db2>
ip 172.28.26.102
mysql_port 3308
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 172.28.26.109
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 172.28.26.110
mode balanced
</role>

vi /etc/mysql-mmm/mmm_agent_navy2.conf(navy2的agent配置文件)

 include mmm_common_navy2.conf
this db1

vi /etc/mysql-mmm/mmm_agent_navy3.conf(navy3的agent配置文件)

 include mmm_common_navy3.conf
this db1

vi /etc/init.d/mysql-mmm-agent-navy2(navy2的agent启动脚本)

 #!/bin/sh
# chkconfig: - 64 36
# description: MMM Agent.
# processname: mmm_agentd
# config: /etc/mysql-mmm/mmm_agent.conf
# pidfile: /var/run/mysql-mmm/mmm_agentd.pid
# Source function library and defaults file.
. /etc/rc.d/init.d/functions
. /etc/default/mysql-mmm-agent
# Cluster name (it can be empty for default cases)
CLUSTER='navy2'
LOCKFILE='/var/lock/subsys/mysql-mmm-agent_navy2'
prog='MMM Agent Daemon'
#-----------------------------------------------------------------------
# Paths
if [ "$CLUSTER" != "" ]; then
MMMD_AGENT_BIN="/usr/sbin/mmm_agentd @$CLUSTER"
MMMD_AGENT_PIDFILE="/var/run/mysql-mmm/mmm_agentd_$CLUSTER.pid"
else
MMMD_AGENT_BIN="/usr/sbin/mmm_agentd"
MMMD_AGENT_PIDFILE="/var/run/mysql-mmm/mmm_agentd.pid"
fi
start() {
if [ "${ENABLED}" != "1" ]; then
echo "$prog is disabled!"
exit 1
fi
echo -n "Starting $prog: "
if [ -s $MMMD_AGENT_PIDFILE ] && kill -0 `cat $MMMD_AGENT_PIDFILE` 2> /dev/null; then
echo " already running."
exit 0
fi
daemon $MMMD_AGENT_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
# Stop daemon.
echo -n "Stopping $prog: "
killproc -p $MMMD_AGENT_PIDFILE $MMMD_AGENT_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $MMMD_AGENT_PIDFILE $MMMD_AGENT_BIN
RETVAL=$?
;;
restart|reload)
stop
start
;;
condrestart)
if [ -f $LOCKFILE ]; then
stop
start
fi
;;
*)
echo "Usage: $0 {start|stop|restart|condrestart|status}"
;;
esac
exit $RETVAL

赋予执行权限:

 chmod +x /etc/init.d/mysql-mmm-agent-navy2

vi /etc/init.d/mysql-mmm-agent-navy3(navy3的agent启动脚本)

 #!/bin/sh
# chkconfig: - 64 36
# description: MMM Agent.
# processname: mmm_agentd
# config: /etc/mysql-mmm/mmm_agent.conf
# pidfile: /var/run/mysql-mmm/mmm_agentd.pid
# Source function library and defaults file.
. /etc/rc.d/init.d/functions
. /etc/default/mysql-mmm-agent
## Paths
#MMMD_AGENT_BIN="/usr/sbin/mmm_agentd"
#MMMD_AGENT_PIDFILE="/var/run/mysql-mmm/mmm_agentd.pid"
#LOCKFILE='/var/lock/subsys/mysql-mmm-agent'
#prog='MMM Agent Daemon'
# Cluster name (it can be empty for default cases)
CLUSTER='navy3'
LOCKFILE='/var/lock/subsys/mysql-mmm-agent_navy3'
prog='MMM Agent Daemon'
#-----------------------------------------------------------------------
# Paths
if [ "$CLUSTER" != "" ]; then
MMMD_AGENT_BIN="/usr/sbin/mmm_agentd @$CLUSTER"
MMMD_AGENT_PIDFILE="/var/run/mysql-mmm/mmm_agentd_$CLUSTER.pid"
else
MMMD_AGENT_BIN="/usr/sbin/mmm_agentd"
MMMD_AGENT_PIDFILE="/var/run/mysql-mmm/mmm_agentd.pid"
fi
start() {
if [ "${ENABLED}" != "1" ]; then
echo "$prog is disabled!"
exit 1
fi
echo -n "Starting $prog: "
if [ -s $MMMD_AGENT_PIDFILE ] && kill -0 `cat $MMMD_AGENT_PIDFILE` 2> /dev/null; then
echo " already running."
exit 0
fi
daemon $MMMD_AGENT_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
# Stop daemon.
echo -n "Stopping $prog: "
killproc -p $MMMD_AGENT_PIDFILE $MMMD_AGENT_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $MMMD_AGENT_PIDFILE $MMMD_AGENT_BIN
RETVAL=$?
;;
restart|reload)
stop
start
;;
condrestart)
if [ -f $LOCKFILE ]; then
stop
start
fi
;;
*)
echo "Usage: $0 {start|stop|restart|condrestart|status}"
;;
esac
exit $RETVAL

赋予可执行权限:

 chmod +x /etc/init.d/mysql-mmm-agent-navy3

启动服务:

 /etc/init.d/mysql-mmm-agent-navy2 start
/etc/init.d/mysql-mmm-agent-navy3 start
3、db2上:
复制db1上的文件到相应的目录下:
 
 scp /etc/mysql-mmm/mmm_common_navy2.conf 172.28.26.102:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common_navy3.conf 172.28.26.102:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_agent_navy2.conf 172.28.26.102:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_agent_navy3.conf 172.28.26.102:/etc/mysql-mmm/
scp /etc/init.d/mysql-mmm-agent-navy2 172.28.26.102:/etc/init.d/
scp /etc/init.d/mysql-mmm-agent-navy3 172.28.26.102:/etc/init.d/

修改agent配置文件:

 sed -i 's/this db1/this db2/' /etc/mysql-mmm/mmm_agent_navy2.conf
sed -i 's/this db1/this db2/' /etc/mysql-mmm/mmm_agent_navy3.conf

赋予可执行权限

 chmod +x /etc/init.d/mysql-mmm-agent-navy2
chmod +x /etc/init.d/mysql-mmm-agent-navy3

启动服务:

 /etc/init.d/mysql-mmm-agent-navy2 start
/etc/init.d/mysql-mmm-agent-navy3 start
4、monitor上
复制db1上的配置文件:
 scp /etc/mysql-mmm/mmm_common_navy2.conf 172.28.26.103:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common_navy3.conf 172.28.26.103:/etc/mysql-mmm/

vi /etc/mysql-mmm/mmm_mon_navy2.conf

 include mmm_common_navy2.conf
<monitor>
ip 127.0.0.1
port 9992
pid_path /var/run/mysql-mmm/mmm_mond_navy2.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond_navy2.status
ping_ips 172.28.26.101,172.28.26.102
auto_set_online 10
# wait_for_other_master 2
# 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 123456
</host>
debug 0

vi /etc/mysql-mmm/mmm_mon_navy3.conf

 include mmm_common_navy3.conf
<monitor>
ip 127.0.0.1
port 9993
pid_path /var/run/mysql-mmm/mmm_mond_navy3.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond_navy3.status
ping_ips 172.28.26.101,172.28.26.102
auto_set_online 10
# wait_for_other_master 2
# 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 123456
</host>
debug 0

vi /etc/mysql-mmm/mmm_mon_log_navy2.conf

 #log4perl.logger = FATAL, MMMLog, MailFatal
log4perl.logger = FATAL, MMMLog
log4perl.appender.MMMLog = Log::Log4perl::Appender::File
log4perl.appender.MMMLog.Threshold = INFO
log4perl.appender.MMMLog.filename = /var/log/mysql-mmm/mmm_mond_navy2.log
log4perl.appender.MMMLog.recreate = 1
log4perl.appender.MMMLog.layout = PatternLayout
log4perl.appender.MMMLog.layout.ConversionPattern = %d %5p %m%n
#log4perl.appender.MailFatal = Log::Dispatch::Email::MailSender
#log4perl.appender.MailFatal.Threshold = FATAL
#log4perl.appender.MailFatal.from = [email protected]
#log4perl.appender.MailFatal.to = root
#log4perl.appender.MailFatal.buffered = 0
#log4perl.appender.MailFatal.subject = FATAL error in mysql-mmm-monitor
#log4perl.appender.MailFatal.layout = PatternLayout
#log4perl.appender.MailFatal.layout.ConversionPattern = %d %m%n

vi /etc/mysql-mmm/mmm_mon_log_navy3.conf

 #log4perl.logger = FATAL, MMMLog, MailFatal
log4perl.logger = FATAL, MMMLog
log4perl.appender.MMMLog = Log::Log4perl::Appender::File
log4perl.appender.MMMLog.Threshold = INFO
log4perl.appender.MMMLog.filename = /var/log/mysql-mmm/mmm_mond_navy3.log
log4perl.appender.MMMLog.recreate = 1
log4perl.appender.MMMLog.layout = PatternLayout
log4perl.appender.MMMLog.layout.ConversionPattern = %d %5p %m%n
#log4perl.appender.MailFatal = Log::Dispatch::Email::MailSender
#log4perl.appender.MailFatal.Threshold = FATAL
#log4perl.appender.MailFatal.from = [email protected]
#log4perl.appender.MailFatal.to = root
#log4perl.appender.MailFatal.buffered = 0
#log4perl.appender.MailFatal.subject = FATAL error in mysql-mmm-monitor
#log4perl.appender.MailFatal.layout = PatternLayout
#log4perl.appender.MailFatal.layout.ConversionPattern = %d %m%n

vi /etc/init.d/mysql-mmm-monitor-navy2

 #!/bin/sh
#
# mysql-mmm-monitor This shell script takes care of starting and stopping
# the mmm monitoring daemon.
#
# chkconfig: - 64 36
# description: MMM Monitor.
# processname: mmm_mond
# config: /etc/mysql-mmm/mmm_mon.conf
# pidfile: /var/run/mysql-mmm/mmm_mond.pid
# Source function library and defaults file.
. /etc/rc.d/init.d/functions
. /etc/default/mysql-mmm-monitor
# Cluster name (it can be empty for default cases)
CLUSTER='navy2'
LOCKFILE='/var/lock/subsys/mysql-mmm-monitor_navy2'
prog='MMM Monitor Daemon'
if [ "$CLUSTER" != "" ]; then
MMMD_MON_BIN="/usr/sbin/mmm_mond @$CLUSTER"
MMMD_MON_PIDFILE="/var/run/mysql-mmm/mmm_mond-$CLUSTER.pid"
else
MMMD_MON_BIN="/usr/sbin/mmm_mond"
MMMD_MON_PIDFILE="/var/run/mysql-mmm/mmm_mond.pid"
fi
start() {
if [ "${ENABLED}" != "1" ]; then
echo "$prog is disabled!"
exit 1
fi
echo -n "Starting $prog: "
if [ -s $MMMD_MON_PIDFILE ] && kill -0 `cat $MMMD_MON_PIDFILE` 2> /dev/null; then
echo " already running."
exit 0
fi
daemon $MMMD_MON_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
# Stop daemon.
echo -n "Stopping $prog: "
killproc -p $MMMD_MON_PIDFILE $MMMD_MON_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $MMMD_MON_PIDFILE $MMMD_MON_BIN
RETVAL=$?
;;
restart|reload)
stop
start
;;
condrestart)
if [ -f $LOCKFILE ]; then
stop
start
fi
;;
*)
echo "Usage: $0 {start|stop|restart|condrestart|status}"
;;
esac
exit $RETVAL

vi /etc/init.d/mysql-mmm-monitor-navy3

 #!/bin/sh
#
# mysql-mmm-monitor This shell script takes care of starting and stopping
# the mmm monitoring daemon.
#
# chkconfig: - 64 36
# description: MMM Monitor.
# processname: mmm_mond
# config: /etc/mysql-mmm/mmm_mon.conf
# pidfile: /var/run/mysql-mmm/mmm_mond.pid
# Source function library and defaults file.
. /etc/rc.d/init.d/functions
. /etc/default/mysql-mmm-monitor
# Cluster name (it can be empty for default cases)
CLUSTER='navy3'
LOCKFILE='/var/lock/subsys/mysql-mmm-monitor_navy3'
prog='MMM Monitor Daemon'
if [ "$CLUSTER" != "" ]; then
MMMD_MON_BIN="/usr/sbin/mmm_mond @$CLUSTER"
MMMD_MON_PIDFILE="/var/run/mysql-mmm/mmm_mond-$CLUSTER.pid"
else
MMMD_MON_BIN="/usr/sbin/mmm_mond"
MMMD_MON_PIDFILE="/var/run/mysql-mmm/mmm_mond.pid"
fi
start() {
if [ "${ENABLED}" != "1" ]; then
echo "$prog is disabled!"
exit 1
fi
echo -n "Starting $prog: "
if [ -s $MMMD_MON_PIDFILE ] && kill -0 `cat $MMMD_MON_PIDFILE` 2> /dev/null; then
echo " already running."
exit 0
fi
daemon $MMMD_MON_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
# Stop daemon.
echo -n "Stopping $prog: "
killproc -p $MMMD_MON_PIDFILE $MMMD_MON_BIN
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $MMMD_MON_PIDFILE $MMMD_MON_BIN
RETVAL=$?
;;
restart|reload)
stop
start
;;
condrestart)
if [ -f $LOCKFILE ]; then
stop
start
fi
;;
*)
echo "Usage: $0 {start|stop|restart|condrestart|status}"
;;
esac
exit $RETVAL

赋予可执行权限:

 chmod +x /etc/init.d/mysql-mmm-monitor-navy2
chmod +x /etc/init.d/mysql-mmm-monitor-navy3

启动监控服务:

/etc/init.d/mysql-mmm-monitor-navy2 start
/etc/init.d/mysql-mmm-monitor-navy3 start

结果

 [root@monitor ~]# mmm_control  show
db1(172.28.26.101) master/ONLINE. Roles: writer(172.28.26.104)
db2(172.28.26.102) master/ONLINE. Roles:
db3(172.28.26.188) slave/ONLINE. Roles: reader(172.28.26.105)
db4(172.28.26.189) slave/ONLINE. Roles: reader(172.28.26.106)
[root@monitor ~]# mmm_control @navy2 show
db1(172.28.26.101) master/ONLINE. Roles: writer(172.28.26.107)
db2(172.28.26.102) master/ONLINE. Roles: reader(172.28.26.108)
[root@monitor ~]# mmm_control @navy3 show
db1(172.28.26.101) master/ONLINE. Roles: writer(172.28.26.109)
db2(172.28.26.102) master/ONLINE. Roles: reader(172.28.26.110)
[root@monitor ~]#
05-11 20:54