標籤:blog http io os 使用 ar strong for 檔案
簡介
MMM(Master-Master replication manager for MySQL)是一套支援雙主故障切換和雙主日常管理的指令碼程式。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Master(雙主)複製,雖然叫做雙主複製,但是業務上同一時刻只允許對一個主進行寫入,另一台備選主上提供部分讀服務,以加速在主主切換時刻備選主的預熱,可以說MMM這套指令碼程式一方面實現了故障切換的功能,另一方面其內部附加的工具指令碼也可以實現多個slave的read負載平衡。
MMM提供了自動和手動兩種方式移除一組伺服器中複寫延遲較高的伺服器的虛擬ip,同時它還可以備份資料,實現兩節點之間的資料同步等。由於MMM無法完全的保證資料一致性,所以MMM適用於對資料的一致性要求不是很高,但是又想最大程度的保證業務可用性的情境。對於那些對資料的一致性要求很高的業務,非常不建議採用MMM這種高可用架構。
MMM項目來自 Google:http://code.google.com/p/mysql-master-master
官方網站為:http://mysql-mmm.org
下面我們通過一個實際案例來充分瞭解MMM的內部架構,如所示。
具體的配置資訊如下所示:
角色 ip地址 主機名稱字 server-idmonitoring 192.168.0.30 db2 -master1 192.168.0.60 db1 1master2 192.168.0.50 db2 2slave1 192.168.0.40 db3 3
業務中的服務ip資訊如下所示:
ip地址 角色 描述192.168.0.108 write 應用程式串連該ip對主庫進行寫請求192.168.0.88 read 應用程式串連該ip進行讀請求192.168.0.98 read 應用程式串連該ip進行讀請求
具體的配置步驟如下:
(1)主機配置
配置/etc/hosts,在所有主機中,添加所有的主機資訊:
[[email protected] ~]# cat /etc/hosts192.168.0.60 db1192.168.0.50 db2192.168.0.40 db3[[email protected] ~]#
(2)首先在3台主機上安裝mysql和搭建複製(192.168.0.60和192.168.0.50互為主從,192.168.0.40為192.168.0.60的從)具體的複製搭建這裡就省略,要是這都不會,那麼該文章對你就沒意思了。然後在每個mysql的設定檔中加入以下內容,注意server_id 不能重複。
db1(192.168.0.60)上:
server-id = 1log_slave_updates = 1auto-increment-increment = 2auto-increment-offset = 1
db2(192.168.0.50)上:
server-id = 2log_slave_updates = 1auto-increment-increment = 2auto-increment-offset = 2
db3(192.168.0.40)上:
server-id = 3log_slave_updates = 1
上面的id不一定要按順序來,只要沒有重複即可。
(3)安裝MMM所需要的Perl模組(所有伺服器)執行該指令碼,也可以安裝epel源,然後yum -y install mysql-mmm*來安裝MMM:
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmyum -y install mysql-mmm*
[[email protected] ~]# cat install.sh #!/bin/bashwget http://xrl.us/cpanm --no-check-certificatemv cpanm /usr/binchmod 755 /usr/bin/cpanmcat > /root/list << EOFinstall Algorithm::Diffinstall Class::Singletoninstall DBIinstall DBD::mysqlinstall File::Basenameinstall File::statinstall File::Tempinstall Log::Dispatchinstall Log::Log4perlinstall Mail::Sendinstall Net::ARPinstall Net::Pinginstall Proc::Daemoninstall Thread::Queueinstall Time::HiResEOFfor package in `cat /root/list`do cpanm $packagedone[[email protected] ~]#
(4)
下載mysql-mmm軟體,在所有伺服器上安裝:
[[email protected] ~]# wget http://mysql-mmm.org/_media/:mmm2:mysql-mmm-2.2.1.tar.gz
[[email protected] ~]# mv :mmm2:mysql-mmm-2.2.1.tar.gz mysql-mmm-2.2.1.tar.gz[[email protected] ~]# tar xf mysql-mmm-2.2.1.tar.gz [[email protected] ~]# cd mysql-mmm-2.2.1[[email protected] mysql-mmm-2.2.1]# make install
mysql-mmm安裝後的主要拓撲結構如下所示(注意:yum安裝的和源碼安裝的路徑有所區別):
目錄 介紹/usr/lib/perl5/vendor_perl/5.8.8/MMM MMM使用的主要perl模組/usr/lib/mysql-mmm MMM使用的主要指令碼/usr/sbin MMM使用的主要命令的路徑/etc/init.d/ MMM的agent和monitor啟動服務的目錄/etc/mysql-mmm MMM設定檔的路徑,預設所以的設定檔位於該目錄下/var/log/mysql-mmm 預設的MMM儲存日誌的位置
到這裡已經完成了MMM的基本需求,接下來需要配置具體的設定檔,其中mmm_common.conf,mmm_agent.conf為agent端的設定檔,mmm_mon.conf為monitor端的設定檔。
(5)配置agent端的設定檔,需要在db1,db2,db3上分別配置。
在db1主機上配置agent設定檔:
[[email protected] ~]# cd /etc/mysql-mmm/[[email protected] mysql-mmm]# cat mmm_common.conf active_master_role writer<host default> cluster_interface eth1 pid_path /var/run/mmm_agentd.pid bin_path /usr/lib/mysql-mmm/ replication_user repl replication_password 123456 agent_user mmm_agent agent_password mmm_agent</host><host db1> ip 192.168.0.60 mode master peer db2</host><host db2> ip 192.168.0.50 mode master peer db1</host><host db3> ip 192.168.0.40 mode slave</host><role writer> hosts db1, db2 ips 192.168.0.108 mode exclusive</role><role reader> hosts db2, db3 ips 192.168.0.88, 192.168.0.98 mode balanced</role>[[email protected] mysql-mmm]#
其中replication_user用於檢查複製的使用者,agent_user為agent的使用者,mode標明是否為主或者備選主,或者從庫。mode exclusive主為獨佔模式,同一時刻只能有一個主,<role write>中hosts表示目前的主庫和備選主的真實主機ip或者主機名稱,ips為對外提供的虛擬機器ip地址,<role readr>中hosts代表從庫真實的ip和主機名稱,ips代表從庫的虛擬ip地址。
由於db2和db3兩台主機也要配置agent設定檔,我們直接把mmm_common.conf從db1拷貝到db2和db3兩台主機的/etc/mysql-mmm下。
注意:monitor主機要需要:
scp /etc/mysql-mmm/mmm_common.conf db2:/etc/mysql-mmm/
scp /etc/mysql-mmm/mmm_common.conf db3:/etc/mysql-mmm/
分別在db1,db2,db3三台主機的/etc/mysql-mmm配置mmm_agent.conf檔案,分別用不同的字元標識,注意這三台機器的this db1這塊要想,比如本環境中,db1要配置this db1,db2要配置為this db2,而db3要配置為this db3。
在db1(192.168.0.60)上:
[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.confthis db1[[email protected] ~]#
在db2(192.168.0.50)上:
[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.confthis db2[[email protected] ~]#
在db3(192.168.0.40)上:
[[email protected] ~]# cat /etc/mysql-mmm/mmm_agent.conf include mmm_common.confthis db3[[email protected] ~]#
在db2(192.168.0.30)配置monitor的設定檔:
[[email protected] ~]# 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.40,192.168.0.50,192.168.0.60 auto_set_online 60</monitor><host default> monitor_user mmm_monitor monitor_password mmm_monitor</host>debug 0[[email protected] ~]#
這裡只在原有設定檔中的ping_ips添加了整個架構被監控主機的ip地址,而在<host default>中配置了用於監控的使用者。
(6)建立監控使用者,這裡需要建立3個監控使用者,具體描述如下:
使用者名稱 描述 許可權monitor user MMM的monitor端監控所有的mysql資料庫的狀態使用者 REPLICATION CLIENTagent user 主要是MMM用戶端用於改變的master的read_only狀態使用者 SUPER,REPLICATION CLIENT,PROCESSrepl 用於複製的使用者 REPLICATION SLAVE
在3台伺服器(db1,db2,db3)進行授權,因為我之前的主主複製,以及主從已經是ok的,所以我在其中一台伺服器執行就ok了。用於複製的帳號之前已經有了,所以這裡就授權兩個帳號。
mysql> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO ‘mmm_agent‘@‘192.168.0.%‘ IDENTIFIED BY ‘mmm_agent‘;Query OK, 0 rows affected (0.08 sec)mysql> GRANT REPLICATION CLIENT ON *.* TO ‘mmm_monitor‘@‘192.168.0.%‘ IDENTIFIED BY ‘mmm_monitor‘;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.03 sec)mysql>
如果是從頭到尾從新搭建,則加上另外一個複製賬戶(分別在3台伺服器都需要執行這3條SQL):
GRANT REPLICATION SLAVE ON *.* TO ‘repl‘@‘192.168.0.%‘ IDENTIFIED BY ‘123456‘;
(7)啟動agent服務。
最後分別在db1,db2,db3上啟動agent,並在db2(192.168.0.30)上啟動monitor程式:
[[email protected] ~]# /etc/init.d/mysql-mmm-agent startDaemon bin: ‘/usr/sbin/mmm_agentd‘Daemon pid: ‘/var/run/mmm_agentd.pid‘Starting MMM Agent daemon... Ok[[email protected] ~]#
[[email protected] ~]# /etc/init.d/mysql-mmm-agent startStarting MMM Agent Daemon: [ OK ][[email protected] ~]#
因為我有些使用yum安裝的,所以啟動資訊有些不一樣。^_^
[[email protected] ~]# /etc/init.d/mysql-mmm-agent startStarting MMM Agent Daemon: [ OK ][[email protected] ~]#
啟動monitor:
[[email protected] ~]# /etc/init.d/mysql-mmm-monitor startStarting MMM Monitor Daemon: [ OK ][[email protected] ~]#
其中agent的日誌存放在/var/log/mysql-mmm/mmm_agentd.log,monitor日誌放在/var/log/mysql-mmm/mmm_mond.log,啟動過程中有什麼問題,通常日誌都會有詳細的記錄。
(8)在monitor主機上檢查叢集主機的狀態:
[[email protected] ~]# mmm_control checks alldb2 ping [last change: 2014/04/18 00:29:01] OKdb2 mysql [last change: 2014/04/18 00:29:01] OKdb2 rep_threads [last change: 2014/04/18 00:29:01] OKdb2 rep_backlog [last change: 2014/04/18 00:29:01] OK: Backlog is nulldb3 ping [last change: 2014/04/18 00:29:01] OKdb3 mysql [last change: 2014/04/18 00:29:01] OKdb3 rep_threads [last change: 2014/04/18 00:29:01] OKdb3 rep_backlog [last change: 2014/04/18 00:29:01] OK: Backlog is nulldb1 ping [last change: 2014/04/18 00:29:01] OKdb1 mysql [last change: 2014/04/18 00:29:01] OKdb1 rep_threads [last change: 2014/04/18 00:29:01] OKdb1 rep_backlog [last change: 2014/04/18 00:29:01] OK: Backlog is null[[email protected] ~]#
(9)在monitor主機上檢查叢集環境線上狀況:
[[email protected] ~]# mmm_control show db1(192.168.0.60) master/ONLINE. Roles: writer(192.168.0.108) db2(192.168.0.50) master/ONLINE. Roles: reader(192.168.0.88) db3(192.168.0.40) slave/ONLINE. Roles: reader(192.168.0.98)[[email protected] ~]#
(10)online(上線)所有主機:
我這裡主機已經線上了,如果沒有線上,可以使用下面的命令將相關主機online
[[email protected] ~]# mmm_control set_online db1OK: This host is already ONLINE. Skipping command.[[email protected] ~]#
提示主機已經線上,已經跳過命令執行了。
到這裡整個叢集就配置完成了。從輸出中可以看到虛擬ip 192.168.0.108已經順利添加到主機192.168.0.60上作為主對外提供寫服務,虛擬ip 192.168.0.88添加到主機192.168.0.50上對外提供讀服務,而虛擬ip 192.168.0.98添加到192.168.0.40上對外提供讀服務。
MMM高可用測試
我們已經完成高可用環境的搭建了,下面我們就可以做MMM的HA測試咯。首先查看整個叢集的狀態,可以看到整個叢集狀態正常。
[[email protected] ~]# mmm_control show db1(192.168.0.60) master/ONLINE. Roles: writer(192.168.0.108) db2(192.168.0.50) master/ONLINE. Roles: reader(192.168.0.88) db3(192.168.0.40) slave/ONLINE. Roles: reader(192.168.0.98)[[email protected] ~]#
類比db2(192.168.0.50 )宕機,手動停止mysql服務,觀察monitor日誌:
[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2014/04/18 00:55:53 FATAL State of host ‘db2‘ changed from ONLINE to HARD_OFFLINE (ping: OK, mysql: not OK)
從日誌發現db2的狀態有ONLINE轉換為HARD_OFFLINE
重新查看叢集的最新狀態:
[[email protected] ~]# mmm_control show db1(192.168.0.60) master/ONLINE. Roles: writer(192.168.0.108) db2(192.168.0.50) master/HARD_OFFLINE. Roles: db3(192.168.0.40) slave/ONLINE. Roles: reader(192.168.0.88), reader(192.168.0.98)[[email protected] ~]#
重啟db2,可以看到db2由HARD_OFFLINE轉到AWAITING_RECOVERY。這裡db2再次接管讀請求。
[[email protected] ~]# mmm_control show db1(192.168.0.60) master/ONLINE. Roles: writer(192.168.0.108) db2(192.168.0.50) master/ONLINE. Roles: reader(192.168.0.88) db3(192.168.0.40) slave/ONLINE. Roles: reader(192.168.0.98)[[email protected] ~]#
類比db1主庫宕機:
查看叢集狀態:
[[email protected] ~]# mmm_control show db1(192.168.0.60) master/HARD_OFFLINE. Roles: db2(192.168.0.50) master/ONLINE. Roles: reader(192.168.0.88), writer(192.168.0.108) db3(192.168.0.40) slave/ONLINE. Roles: reader(192.168.0.98)[[email protected] ~]#
查看MMM日誌:
[[email protected] ~]# tail -f /var/log/mysql-mmm/mmm_mond.log 2014/04/18 01:09:20 FATAL State of host ‘db1‘ changed from ONLINE to HARD_OFFLINE (ping: OK, mysql: not OK)
從上面可以發現,db1由以前的ONLINE轉化為HARD_OFFLINE,移除了寫角色,因為db2是備選主,所以接管了寫角色,db3指向新的主庫db2,應該說db3實際上找到了db2的sql現在的位置,即db2 show master返回的值,然後直接在db3上change master to到db2。
db1,db2,db3之間為一主兩從的複製關係,一旦發生db2,db3延時於db1時,這個時刻db1 mysql宕機,db3將會等待資料追上db1後,再重新指向新的主db2,進行change master to db2操作,在db1宕機的過程中,一旦db2落後於db1,這時發生切換,db2變成了可寫狀態,資料的一致性將會無法保證。
總結:
MMM不適用於對資料一致性要求很高的環境。但是高可用完全做到了。
參考資料:
http://mysql-mmm.org/mmm2:guide
MySQL 高可用架構之MMM