由於mysql沒有提供高可用性方案,如果一臺資料庫down就會影響到業務,為了實現高可用性可以使用hearbeat,這裡我使用MMM來提高mysql的高可用性,以提高業務的不中斷.
 
環境: 
 
    伺服器型別                   IP                        VIP
     node1                  10.10.1.65                  10.10.1.77
     node2                  10.10.1.58                  10.10.1.78
     mon                    10.10.1.161                 10.10.1.79
 
 
配置過程:
 
 1.首先要實現node1和node2的雙向同步,這個本人部落格已經有相關的文章,在這裡就不進行說明了
 
 2.安裝和配置MMM
 
    3臺伺服器都要安裝MMM安裝包,由於要使用到perl模組,所以要進行如下模組的安裝
 
    Algorithm::Diff
    Proc::Daemon
    Time::HiRes
    DBI
    DBD::mysql
 
    以上模組你可以通過cpan進行安裝,也可以去cpan.org網站上去下載相應的安裝包,我已經安裝好了
    MMM下載地址如下:http://code.google.com/p/mysql-master-master/downloads/list,現在
    最新的安裝是mysql-master-master-1.2.6.tar.gz.
 
    #tar zxvf mysql-master-master-1.2.6.tar.gz
    #cd mysql-master-master-1.2.6
    #./install.pl
   
    在nod1上的配置:
    #cp usr/local/mmm/etc/examples/mmm_agent.conf.example mmm_agent.conf
    #vi usr/local/mmm/etc/examples/mmm_agent.conf  //這個配置檔案的內容很少,直接清空
                                                     新增如下的內容

#

     
# Master-Master Manager config (agent)

     
#

     
# Debug mode

     debug no //是否開啟debug模式
     
# Paths

     pid_path /usr/local/mmm/var/mmmd_agent.pid //程式路徑
     bin_path /usr/local/mmm/bin //執行命令路徑
     
# Logging setup

     log mydebug
     file /usr/local/mmm/var/mmm-debug.log //debug日誌檔案路徑
     level debug
     log mytraps
     file /usr/local/mmm/var/mmm-traps.log
     level trap
     
# MMMD command socket tcp-port and ip

     bind_port 9989
     
# Cluster interface

     cluster_interface eth0 //真實IP的介面
     
# Define current server id

     this db1
     mode master
     
# For masters

     peer db2
     
# Cluster hosts addresses and access params

     host db1
     ip 10.10.1.65 //node1的真實IP
     port 3306 //資料庫埠
     user rep_agent //如下建立的mysql使用者
     password RepAgent //建立mysql使用者的密碼
     host db2
     ip 10.10.1.58 //node2的真實IP
     port 3306
     user rep_agent
     password RepAgent
    
#mysql -u root -p123456
GRANT ALL PRIVILEGES on *.* to `rep_agent`@`%` identified  by `RepAgent`;
GRANT ALL PRIVILEGES on *.* to `rep_monitor`@`%` identified by
`RepMonitor`;
在node2上的配置:
#cp usr/local/mmm/etc/examples/mmm_agent.conf.example mmm_agent.conf
#vi usr/local/mmm/etc/examples/mmm_agent.conf  //這個配置檔案的內容很少,直接清空
                                                新增如下的內容
#

    
# Master-Master Manager config (agent)

    
#

    
# Debug mode

    debug no
    
# Paths

    pid_path /usr/local/mmm/var/mmmd_agent.pid
    bin_path /usr/local/mmm/bin
    
# Logging setup

    log mydebug
    file /usr/local/mmm/var/mmm-debug.log
    level debug
    log mytraps
    file /usr/local/mmm/var/mmm-traps.log
    level trap
    
# MMMD command socket tcp-port and ip

    bind_port 9989
    
# Cluster interface

    cluster_interface eth0
    
# Define current server id

    this db2
    mode master
    
# For masters

    peer db1
    
# Cluster hosts addresses and access params

    host db1
    ip 10.10.1.65
    port 3306
    user rep_agent
    password RepAgent
    host db2
    ip 10.10.1.58
    port 3306
    user rep_agent
    password RepAgent
#mysql -u root -p123456
GRANT ALL PRIVILEGES on *.* to `rep_agent`@`%` identified  by `RepAgent`;
GRANT ALL PRIVILEGES on *.* to `rep_monitor`@`%` identified by
`RepMonitor`;
 在mon上的配置:
# Master-Master Manager config (monitor)

#

# Debug mode

debug no
# Paths

pid_path /usr/local/mmm/var/mmmd.pid
status_path /usr/local/mmm/var/mmmd.status
bin_path /usr/local/mmm/bin
# Logging setup

log mydebug
    file /usr/local/mmm/var/mmm-debug.log
    level debug
log mytraps
    file /usr/local/mmm/var/mmm-traps.log
    level trap
    email root@localhost

# MMMD command socket tcp-port

bind_port 9988
agent_port 9989
monitor_ip 127.0.0.1
# Cluster interface

cluster_interface eth0
# Cluster hosts addresses and access params

host db1
    ip 10.10.1.65
    port 3306
    user rep_monitor
    password RepMonitor
    mode master
    peer db2
host db2
    ip 10.10.1.58
    port 3306
    user rep_monitor
    password RepMonitor
    mode master
    peer db1

#

# Define roles

#

active_master_role writer
# Mysql Reader role

role reader
    mode balanced
    servers db1, db2
    ip 10.10.1.77, 10.10.1.78   
//這裡是虛擬IP
# Mysql Writer role

role writer
    mode exclusive
    servers db1, db2
    ip 10.10.1.79              
//這裡是虛擬IP
#

# Checks parameters

#

# Ping checker

check ping
    check_period 1
    trap_period 5
    timeout 2
# Mysql checker

# (restarts after 10000 checks to prevent memory leaks)

check mysql
    check_period 1
    trap_period 2
    timeout 2
    restart_after 10000
# Mysql replication backlog checker

# (restarts after 10000 checks to prevent memory leaks)

check rep_backlog
    check_period 5
    trap_period 10
    max_backlog 60
    timeout 2
    restart_after 10000
# Mysql replication threads checker

# (restarts after 10000 checks to prevent memory leaks)

check rep_threads
    check_period 1
    trap_period 5
    timeout 2
    restart_after 10000
進行啟動以及測試:
在node1和node2上啟動mmmd_agent
#mmmd_agent
#netstat -tlnp
 tcp        0      0 0.0.0.0:9989                  0.0.0.0:*                   LISTEN      14301/perl
出現9989埠就表示啟動正常
在mon上啟動mmm_mon
#mmmd_mon
MySQL Multi-Master Replication Manager
Version: 1.2.6
Reading config file: `mmm_mon.conf`
$VAR1 = {};
在管理節點上(mon)啟動db節點:
#mmm_control set_online db1
 MySQL Multi-Master Replication Manager
 Version: 1.2.6
 Config file: mmm_mon.conf
 Daemon is running!
 Command sent to monitoring host. Result: OK: State of `db1` changed to ONLINE. Now you can wait some time and check its new roles!

#mmm_control set_online db2

 MySQL Multi-Master Replication Manager
 Version: 1.2.6
 Config file: mmm_mon.conf
 Daemon is running!
 Command sent to monitoring host. Result: OK: State of `db2` changed to ONLINE. Now you can wait some time and check its new roles!
檢視下節點狀態:
#mmm_control show
MySQL Multi-Master Replication Manager
Version: 1.2.6
Config file: mmm_mon.conf
Daemon is running!
===============================
Cluster failover method: AUTO
===============================
Servers status:
  db1(10.10.1.65): master/ONLINE. Roles: reader(10.10.1.78;), writer(10.10.1.79;)
  db2(10.10.1.58): master/ONLINE. Roles: reader(10.10.1.77;)
看到這個就說明配置沒有問題,現在我們來進行切換測試,看切換是否正常,我現在把node1的mysql停止掉然後在看下節點的狀態
#mmm_control show
[root@localhost etc]# mmm_control show
MySQL Multi-Master Replication Manager
Version: 1.2.6
Config file: mmm_mon.conf
Daemon is running!
===============================
Cluster failover method: AUTO
===============================
Servers status:
  db1(10.10.1.65): master/HARD_OFFLINE. Roles: None
  db2(10.10.1.58): master/ONLINE. Roles: reader(10.10.1.77;), reader(10.10.1.78;), writer(10.10.1.79;)
可以看到讀寫操作都轉到了node2機器上了,看來切換沒有問題,測試了下,切換需要大概3秒的時間,還算是可以接受的了.