一、需求說明

最近一直在學習mysql-mmm,想以後這個架構也能用在我們公司的業務上,我們公司的業務是單機多例項部署,所以也想把mysql-mmm部署成這樣,功夫不負有心人,我成功了,和大家分享一下:
二、環境說明
叢集 角色 主機名 IP Mysql Port Server ID VIP Writer VIP READER
navy2 Agent db1 172.28.26.101 3307 11 172.28.26.107
navy3
Agent
db2 172.28.26.102 3308 1
172.28.26.110
navy2
Agent
db1
172.28.26.101
3307 22
172.28.26.108
navy3
Agent
db2
172.28.26.102
3308 2
172.28.26.109
navy2/navy3 Monitor
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(navy3的配置檔案)
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 = mmm@example.com
#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 = mmm@example.com
#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 ~]#