mysql-mmm 架構詳細教程
Mysql mmm 特點
優點:高可用性,擴展性好,出現故障自動切換,對於主主同步,在同一時間只提供一臺數據庫寫操作,保證的數據的一致性。
缺點:Monitor節點是單點,可以結合Keepalived實現高可用,對主機的數量有要求,需要實現讀寫分離,對程序來說是個挑戰。
8.12.3.3 mysql-mmm 安裝
需要在所有server上都安裝mysql-mmm
wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm
8.12.3.4 mysql-mmm 配置
- 配置mmm代理和監控賬號的權限
現在環境已經配置好,我沒有忽略 mysql 庫和 user 表,所以只要在任意一臺主庫上執行下面的操作,其他庫就都有這倆賬號了。
這裏我在master1上進行的操作
mysql> grant replication client on . to ‘mmm_monitor‘@‘192.168.0.%‘ identified by ‘oldboy123‘;
mysql> grant super, replication client, process on .
-
所有服務器均需配置/etc/mysql-mmm/mmm_common.conf
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 rep #同步用戶
replication_password oldboy123 #同步密碼
agent_password oldboy123 # mmm_agent用戶的密碼
</host>
<host db1>
ip 192.168.0.60 # db1 master-1的物理ip
mode master
peer db2
</host>
<host db2>
ip 192.168.0.61 # db2 master-2的物理ip
mode master
peer db1
</host>
<host db3>
ip 192.168.0.62 # db3 slave的物理ip
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.0.20 #寫入的虛擬IP地址 VIP
mode exclusive
</role>
<role reader>
hosts db1, db2, db3
ips 192.168.0.21, 192.168.0.22, 192.168.0.23 #虛擬讀取IP地址
mode balanced
</role> -
所有服務器均需配置/etc/mysql-mmm/mmm_agent.conf
vi /etc/mysql-mmm/mmm_agent.conf
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 #分別修改為本機的主機名,即db1、db2、db3和db4 - monitor主機配置/etc/mysql-mmm/mmm_mon.conf
[root@mysql-mmm-monitor tools]# cat /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
ping_ips 192.168.0.60,192.168.0.61,192.168.0.62 #監控的IP
auto_set_online 30 #切換浮動IP的時間(默認為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 #mmm_monitor 用戶
monitor_password oldboy123 #mmm_monitor 密碼
</host>
debug 0
8.12.3.5 mysql-mmm 啟動
1) master-1,master-2,slave啟動代理:
所有數據庫主機啟動mmm-agent:
/etc/init.d/mysql-mmm-agent start
10.204.3.4:9989 0.0.0.0:* LISTEN 2407/mmm_agentd
2) monitor主機啟動mmm-monitor
/etc/init.d/mysql-mmm-monitor start
8.12.3.6 mysql-mmm 測試
mmm_control命令監控mysql服務器狀態
[root@mysql-mmm-monitor ~]# mmm_control show
db1(192.168.0.60) master/ONLINE. Roles: reader(192.168.0.23), writer(192.168.0.20)
db2(192.168.0.61) master/ONLINE. Roles: reader(192.168.0.22)
db3(192.168.0.62) slave/ONLINE. Roles: reader(192.168.0.21)
測試兩個mysql服務器能否實現故障自動切換
將db1的mysql服務停止
[root@mysql-mmm-master1 tools]# /etc/init.d/mysqld stop
Shutting down MySQL... SUCCESS!
等待30秒在mysql-mmm-monitor服務器上進行監控查看
[root@mysql-mmm-monitor ~]# mmm_control show
db1(192.168.0.60) master/HARD_OFFLINE. Roles:
db2(192.168.0.61) master/ONLINE. Roles: reader(192.168.0.22), writer(192.168.0.20)
db3(192.168.0.62) slave/ONLINE. Roles: reader(192.168.0.21), reader(192.168.0.23)
slave檢查master_host是否切換到了另一個主庫地址:
[root@mysql-mmm-slave ~]# mysql -e "show slave status\G"|egrep "Master_Host:"
Master_Host: 192.168.0.61
恢復master-1(db1)
[root@mysql-mmm-master1 tools]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS!
monitor端檢查恢復情況
[root@mysql-mmm-monitor ~]# mmm_control show
db1(192.168.0.60) master/ONLINE. Roles: reader(192.168.0.21)
db2(192.168.0.61) master/ONLINE. Roles: reader(192.168.0.22), writer(192.168.0.20)
db3(192.168.0.62) slave/ONLINE. Roles: reader(192.168.0.23)
我們可以看到當db1恢復後就充當slave的角色了!只有當db2掛了以後db1又會擔當起主服務器的寫入功能。
8.12.3.7 mmm_control命令介紹
Valid commands are:
help - show this message
#查看幫助信息
ping - ping monitor
#ping監控
show - show status
#查看狀態信息
checks [<host>|all [<check>|all]] - show checks status
#顯示檢查狀態,包括(ping、mysql、rep_threads、rep_backlog)
set_online <host> - set host <host> online
#設置某host為online狀態
set_offline <host> - set host <host> offline
#設置某host為offline狀態
mode - print current mode.
#打印當前的模式,是ACTIVE、MANUAL、PASSIVE?
#默認ACTIVE模式
set_active - switch into active mode.
#更改為active模式
set_manual - switch into manual mode.
#更改為manual模式
set_passive - switch into passive mode.
#更改為passive模式
move_role [--force] <role> <host> - move exclusive role <role> to host <host>
#更改host的模式,比如更改處於slave的mysql數據庫角色為write
(Only use --force if you know what you are doing!)
set_ip <ip> <host> - set role with ip <ip> to host <host>
#為host設置ip,只有passive模式的時候才允許更改!
mysql-mmm 架構詳細教程