OS: CentOS-6.4-x86_64-minimal
MYSQL: mysql-5.5.30-linux2.6-x86_64
參考文檔:http://mysql-mmm.org/mmm2:guide
------------------------------------------------------
function IP servername server-id
-------------------------------------------------------
monitor 192.168.137.42 monitor
master 192.168.137.40 db1 1
master 192.168.137.41 db2 2
slave 192.168.137.43 db3 3
slave 192.168.137.44 db4 4
-------------------------------------------------------
虛擬IP設置如下:
writer(192.168.137.140)
reader(192.168.137.141)
reader(192.168.137.142)
reader(192.168.137.143)
一、配置MySQL Relication,
1.修改添加內容在/etc/my.cnf如下
[mysqld]
default-storage-engine = innodb
replicate-ignore-db = mysql
binlog-ignore-db = mysql
server-id = 1
log-bin = mysql-bin.log
log_bin_index = mysql-bin.log.index
relay_log = mysql-bin.relay
relay_log_index = mysql-bin.relay.index
expire_logs_days = 10
max_binlog_size = 100M
log_slave_updates = 1
注意:server-id在每臺服務器上的值都是不一樣
2.重啓mysql服務,重啓成功後登陸mysql,執行show master show;命令
記住File 和Position的值
3.使用mysql-mmm時一共需要三個用戶: replication、mmm_agent和mmm_monitor
(管理服務器上用來監控cluster狀態的用戶,所以可以限定只能從管理服務器登錄)。
使用下面三條命令新建這三個用戶並分配相應的權限:
mysql>GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.137.%' IDENTIFIED BY 'monitor';
mysql>GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.137.%' IDENTIFIED BY 'replication';
mysql>GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.137.%' IDENTIFIED BY 'agent';
4.啓動從數據庫SLAVE進程,Mysql版本從5.1.7以後開始就不支持“master-host”類似的參數,在從庫上執行如下命令;
mysql>CHANGE MASTER TO master_host='192.168.137.40', master_port=3306, master_user='replication',master_password='replication', master_log_file='mysql-bin.000004', master_log_pos=107;
mysql>start slave;
mysql>show slave status\G
主要看
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
是否爲Yes
將db2作爲master,db1作爲slave,重複1-4的內容 。
將db3 db4配置爲db1的slave,重複1-4的內容 。
二、安裝MMM
在管理服務器和數據庫服務器上分別要運行mysql-mmm monitor和agent程序。
下面分別安裝:
先安裝mysql-mmm依賴文件可從如下地址下載
http://search.cpan.org/
或者通過”perl -MCPAN -e shell“ 來安裝
yum -y install perl-CPAN
yum -y install perl-Time-HiRes*
tar -zxvf Algorithm-Diff-1.1902.tar.gz
cd Algorithm-Diff-1.1902
perl Makefile.PL
make
make test
make install
tar -zxvf DBI-1.625.tar.gz
cd DBI-1.625
perl Makefile.PL
make
make test
make install
tar -zxvf Log-Dispatch-2.37.tar.gz
cd Log-Dispatch-2.37
perl Makefile.PL
make
make test
make install
tar -zxvf Log-Log4perl-1.40.tar.gz
cd Log-Log4perl-1.40
perl Makefile.PL
make
make test
make install
tar -zxvf MailTools-2.12.tar.gz
cd MailTools-2.12
perl Makefile.PL
make
make test
make install
gzip -d Net-ARP-1.0.6.gz
tar xzvf Net-ARP-1.0.6
cd Net-ARP
perl Makefile.PL
make
make install
tar -zxvf Proc-Daemon-0.14.tar.gz
cd Proc-Daemon-0.14
perl Makefile.PL
make
make test
make install
tar -zxvf DBD-mysql-4.023.tar.gz
cd DBD-mysql-4.030
perl Makefile.PL --with-mysql=/usr/local/mysql/ --mysql_config=/usr/local/mysql/bin/mysql_config
make
make install
tar xzvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1
make install
在monitor安裝
tar xzvf Class-Singleton-1.4.tar.gz
cd Class-Singleton-1.4
make
make install
三、配置MMM
完成安裝後,所有的配置文件都放到了/etc/mysql-mmm/下面。管理服務器和數據庫服務器上都要包含一個共同的文件mmm_common.conf,內容如下:
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user replication
replication_password replication
agent_user mmm_agent
agent_password agent
</host>
<host db1>
ip 192.168.137.40
mode master
peer db2
</host>
<host db2>
ip 192.168.137.41
mode master
peer db1
</host>
<host db3>
ip 192.168.137.43
mode slave
</host>
<host db4>
ip 192.168.137.44
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.137.140
mode exclusive
</role>
<role reader>
hosts db2, db3, db4
ips 192.168.137.141, 192.168.137.142, 192.168.137.143
mode balanced
</role>
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user replication
replication_password replication
agent_user mmm_agent
agent_password agent
</host>
<host db1>
ip 192.168.137.40
mode master
peer db2
</host>
<host db2>
ip 192.168.137.41
mode master
peer db1
</host>
<host db3>
ip 192.168.137.43
mode slave
</host>
<host db4>
ip 192.168.137.44
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.137.140
mode exclusive
</role>
<role reader>
hosts db2, db3, db4
ips 192.168.137.141, 192.168.137.142, 192.168.137.143
mode balanced
</role>
可以在db1上編輯該文件後,通過scp命令分別複製到monitor、db2、db3、db4上。
編輯mmm_agent.conf。在數據庫服務器上,還有一個mmm_agent.conf需要修改,其內容是:
include mmm_common.conf
this db1
this db1
最後一行的db1,在不同的數據庫服務器上要分別改爲db2、db3和db4,否則代理就會無法啓動。
在數據庫服務器上啓動代理程序
/etc/init.d/mysql-mmm-agent start
編輯mmm_mon.confg。在管理服務器上,修改mmm_mon.conf文件,修改後內容爲:
include mmm_common.conf
<monitor>
ip 192.168.137.42
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.137.40, 192.168.137.41, 192.168.137.43, 192.168.137.44
#auto_set_online 60
</monitor>
<host default>
monitor_user mmm_monitor
monitor_password monitor
</host>
debug 0
<monitor>
ip 192.168.137.42
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.137.40, 192.168.137.41, 192.168.137.43, 192.168.137.44
#auto_set_online 60
</monitor>
<host default>
monitor_user mmm_monitor
monitor_password monitor
</host>
debug 0
在管理服務器上啓動監控程序
/etc/init.d/mysql-mmm-monitor start
啓動後,稍等幾秒,可以通過mmm_control程序查看狀態:
[root@monitor ~]# mmm_control show
db1(192.168.137.40) master/AWAITING_RECOVERY. Roles:
db2(192.168.137.41) master/AWAITING_RECOVERY. Roles:
db3(192.168.137.43) slave/AWAITING_RECOVERY. Roles:
db4(192.168.137.44) slave/AWAITING_RECOVERY. Roles:
將所有DB服務器設爲online狀態:
shell>mmm_control set_online db1
shell>mmm_control set_online db2
shell>mmm_control set_online db3
shell>mmm_control set_online db4
再次查看狀態:
[root@monitor ~]#mmm_control show
db1(192.168.137.40) master/ONLINE. Roles: writer(192.168.137.140)
db2(192.168.137.41) master/ONLINE. Roles: reader(192.168.137.142)
db3(192.168.137.43) slave/ONLINE. Roles: reader(192.168.137.141)
db4(192.168.137.44) slave/ONLINE. Roles: reader(192.168.137.143)
設置成功,下面查看節點狀態:
[root@monitor ~]# mmm_control checks all
db4 ping [last change: 2013/04/16 13:22:29] OK
db4 mysql [last change: 2013/04/16 13:22:29] OK
db4 rep_threads [last change: 2013/04/16 13:22:29] OK
db4 rep_backlog [last change: 2013/04/16 13:22:29] OK: Backlog is null
db2 ping [last change: 2013/04/16 09:49:54] OK
db2 mysql [last change: 2013/04/16 09:49:54] OK
db2 rep_threads [last change: 2013/04/16 09:49:54] OK
db2 rep_backlog [last change: 2013/04/16 09:49:54] OK: Backlog is null
db3 ping [last change: 2013/04/16 09:49:54] OK
db3 mysql [last change: 2013/04/16 09:49:54] OK
db3 rep_threads [last change: 2013/04/16 09:49:54] OK
db3 rep_backlog [last change: 2013/04/16 09:49:54] OK: Backlog is null
db1 ping [last change: 2013/04/16 09:49:54] OK
db1 mysql [last change: 2013/04/16 09:49:54] OK
db1 rep_threads [last change: 2013/04/16 09:49:54] OK
db1 rep_backlog [last change: 2013/04/16 09:49:54] OK: Backlog is null
最後將除write機器的DB的read_only狀態設爲on:
mysql>set global read_only=on;
或者在slave機器上添加只讀賬號
在db1,db2上添加一個帳號給外部登陸:
mysql>grant all privileges on * . * to 'test'@'%' identified by '123456';
然後就可以通過 mysql -utest -p123456 -h 192.168.137.140 連接writer 服務器
同樣的道理可以虛擬IP連接reader服務器。
特別注意:現在解釋一下monitor機上/etc/mysql-mmm/mmm_mon.conf裏我註釋掉的那句auto_set_online參數:
不設置該參數:MySql服務器掛掉的時間超過60秒的話,服務器恢復後會一直處於AWAITING_RECOVERY的狀態,需要我們手動set_online把他設置爲ONLINE狀態,如果低於60秒,且非Flapping狀態,會自動設置爲ONLINE狀態。
設置了該參數:服務器恢復到AWAITING_RECOVERY狀態後,會等待我們設置的秒數,然後自動設置爲ONLINE狀態(還要考慮Flapping狀態)。
因此,設置與不設置auto_set_online參數根據你的需求了,我偏向於不設置。
特別注意,在安裝過程中發現writer IP ping不通,通過ip add list命令發現也沒發現虛擬IP,而reader IP 則沒有這個問題。
在db1上的/etc/mysql-mmm/mmm_agent.conf文件加入一行
debug 1
重啓/etc/init.d/mysql-mmm-agent restart 報錯,如下圖所示
原來是DBD-mysql 沒有安裝好,
cp -rf /usr/local/mysql/lib/* /usr/lib64/
cp -rf /usr/local/mysql/include/mysql/* /usr/include/
重新安裝DBD-mysql 後問題解決.