雲平台是個好東西,MySQL-mmm的典型配置是需要五台機器,一台作為mmm admin,兩台master,兩台slave。一下子找五台機器真不容易,何況還要安裝同樣的作業系統。而有了cloud,簡單幾步就有了完備的實驗環境:四台資料庫伺服器和一台管理伺服器(Memory:8G,CPU:2G,Disk:128G,64bit RHEL6)。在此,向為付出辛勞搭建雲平台的同事們表示由衷的感謝:-)下面言歸正傳,開始全新的MySQL mmm之旅。
下面要配置的MySQL Cluster環境包含四台資料庫伺服器和一台管理伺服器,如下:
function
|
IP |
Server Name |
server id |
| monitor |
192.168.84.174 |
- |
- |
| master |
192.168.85.167 |
db1 |
1 |
| master |
192.168.85.169 |
db2 |
2 |
| slave |
192.168.85.171 |
db3 |
3 |
| slave |
192.168.85.168 |
db4 |
4 |
配置完成後,使用下面的虛擬IP訪問MySQL Cluster
| IP |
role |
| 192.168.85.200 |
writer |
| 192.168.85.201 |
reader |
| 192.168.85.202 |
reader |
| 192.168.85.203 |
reader |
一、配置MySQL Relication
1. 安裝MySQL
通過yum命令直接安裝了mysql5.1.52。
2. 修改設定檔/etc/my.cnf
要將添加的內容放在設定檔的[mysqld]部分,如下:
[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql#下面為新添加的內容default-storage-engine = innodbreplicate-ignore-db = mysqlbinlog-ignore-db = mysqlserver-id = 1log-bin = /var/log/mysql/mysql-bin.loglog_bin_index = /var/log/mysql/mysql-bin.log.indexrelay_log = /var/log/mysql/mysql-bin.relayrelay_log_index = /var/log/mysql/mysql-bin.relay.indexexpire_logs_days = 10max_binlog_size = 100Mlog_slave_updates = 1
注意:
1)server-id在每台伺服器上的值都是不一樣,在這裡依次為1、2、3、4。
2)因為在這裡把log檔案配置到了/var/log/mysql下,而mysql預設的目錄是在/var/lib/mysql,所以首先要建立mysql檔案夾,然後用chown -R mysql.mysql mysql命令將mysql的所有者修改為使用者mysql。其次要保證,mysql檔案夾的許可權755(即-rwxr-xr-x)。
如果沒有修改許可權和所有者,重啟服務時就會在錯誤記錄檔中出現找不到mysql-bin.log或者mysql-bin.log.index的錯誤(/usr/libexec/mysqld: File '/var/log/mysql/mysql-bin.log.index' not found (Errcode: 13))。
3. 重新啟動mysql服務
在完成了對my.cnf的修改後,通過service mysqld restart重新啟動mysql服務。在正確啟動後,可以通過如下方式檢查配置是否正確:
1)登入mysql,執行show master status,看是否有如下輸出:
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | mysql |
+------------------+----------+--------------+------------------+
2)到/var/log/mysql目錄下,看是否產生了類似mysql-bin.000001和mysql-bin.log.index的檔案。
二、建立同步資料庫需要的使用者
使用mysql-mmm時一共需要三個使用者: replication、mmm_agent和mmm_monitor(管理伺服器上用來監控cluster狀態的使用者,所以可以限定只能從管理伺服器登入)。使用下面三條命令建立這三個使用者並分配相應的許可權:
GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.84.%' IDENTIFIED BY 'monitor';GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.85.%' IDENTIFIED BY 'agent';GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.85.%' IDENTIFIED BY 'replication';
三、同步主從資料庫
1. 從主要資料庫伺服器匯出當前資料庫內容
mysql> FLUSH TABLES WITH READ LOCK;mysql> SHOW MASTER STATUS;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 106 | | mysql |+------------------+----------+--------------+------------------+
注意保留上述資訊,後面還會用到。另外,不要結束當前mysql控制台,重新開啟一個視窗,匯出資料庫。
# mysqldump -uroot -proot --all-databases > db01_20111005.sql
釋放鎖
mysql> UNLOCK TABLES;
2. 將匯出的sql檔案匯入到其他幾台資料庫伺服器上。首先通過scp複製過去:
# scp db01_20111005.sql root@192.168.85.167:/root/
在其他幾台服務其上匯入改SQL檔案:
# mysql -uroot -proot < db01_20111005.sql
3. 啟動從資料庫SLAVE進程。
mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> CHANGE MASTER TO master_host='192.168.85.167', master_port=3306, master_user='replication',master_password='replication', master_log_file='mysql-bin.000001', master_log_pos=106;Query OK, 0 rows affected (0.07 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.85.180 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 106 Relay_Log_File: mysql-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 106 Relay_Log_Space: 400 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error:1 row in set (0.00 sec)
4. 將db02作為master,db01作為slave,重複1-3。
四、安裝MMM
在管理伺服器和資料庫伺服器上分別要運行mysql-mmm monitor和agent程式。下面分別安裝:
1. 安裝監控程式
在管理伺服器(192.168.84.174)上,執行下面命令:
# yum -y install mysql-mmm-monitor*
與monitor依賴的所有檔案也會隨之安裝,但是有一個例外perl-Time-HiRes,所以還需要執行下面的命令:
# yum -y install perl-Time-HiRes*
2. 安裝代理程式
在資料庫伺服器上執行下面的命令:
# yum -y install mysql-mmm-agent*
五、配置MMM
1. 編輯mmm_common.conf
完成安裝後,所有的設定檔都放到了/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 replication replication_password replication agent_user mmm_agent agent_password agent</host><host db1> ip 192.168.85.167 mode master peer db2</host><host db2> ip 192.168.85.169 mode master peer db1</host><host db3> ip 192.168.85.171 mode slave</host><host db4> ip 192.168.85.168 mode slave</host><role writer> hosts db1, db2 ips 192.168.85.200 mode exclusive</role><role reader> hosts db2, db3, db4 ips 192.168.85.201, 192.168.85.202, 192.168.85.203 mode balanced</role>
可以在db1上編輯該檔案後,通過scp命令分別複製到monitor、db2、db3和db4上。
2. 編輯mmm_agent.conf。在資料庫伺服器上,還有一個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,否則代理就會無法啟動。
3. 編輯mmm_mon.confg。在管理伺服器上,修改mmm_mon.conf檔案,修改後內容為:
include mmm_common.conf<monitor> ip 192.168.84.174 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.85.167, 192.168.85.169, 192.168.85.171, 192.168.85.168 auto_set_online 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 monitor_password monitor</host>debug 0
六、啟動MMM
1. 在資料庫伺服器上啟動代理程式
# cd /etc/init.d/# chkconfig mysql-mmm-agent on# service mysql-mmm-agent start
2. 在管理伺服器上啟動監控程式
# cd /etc/init.d/# chkconfig mysql-mmm-monitor on# service mysql-mmm-monitor start
啟動後,稍等幾秒,可以通過mmm_control程式查看狀態:
# mmm_control show db1(192.168.85.167) master/ONLINE. Roles: writer(192.168.85.200) db2(192.168.85.169) master/ONLINE. Roles: reader(192.168.85.202) db3(192.168.85.171) slave/ONLINE. Roles: reader(192.168.85.203) db4(192.168.85.168) slave/ONLINE. Roles: reader(192.168.85.201)
七、遇到兩個問題
1. 監控程式伺服器無法啟動
在管理伺服器上,一切都完成後,通過mmm_control查看狀態,得到下面的錯誤資訊:ERROR: Can't connect to monitor daemon! 通過編輯/etc/mysql-mmm/mmm_mon.conf檔案將debug 0改為debug 1,開啟監控程式的debug狀態。重新啟動監控程式(service mysql-mmm-monitor restart),就會看到詳細的錯誤資訊,找不到Perl Time HiRes庫。執行yum -y install perl-Time-HiRes*就可以解決。
2. 防火牆問題導致Warning: agent on host db1 is not reachable.
控制台程式正確啟動後,再次執行mmm_control show,卻看到下面的輸出:
# Warning: agent on host db1 is not reachable# Warning: agent on host db2 is not reachable# Warning: agent on host db3 is not reachable# Warning: agent on host db4 is not reachable db1(192.168.85.167) master/ONLINE. Roles: db2(192.168.85.169) master/ONLINE. Roles: db3(192.168.85.171) slave/ONLINE. Roles: db4(192.168.85.168) slave/ONLINE. Roles:
再次開啟debug,發現了下面的錯誤資訊:
2011/10/07 13:38:45 DEBUG Sending command 'GET_AGENT_STATUS()' to db4 (192.168.85.167:9989)
2011/10/07 13:38:45 ERROR The status of the agent on host 'db4' could not be determined (answer was: 0).
通過telnet 192.168.85.167 9989下面檢查網路連接,得到了No route to host的錯誤資訊。登入db1,通過setup程式裡的Firewall configuration關閉Firewall(這不是一個好主意)。同樣,關閉db2、db3和db4上的防火牆,再次重啟監控程式,一切回到正常狀態!
參考文章:
MySQL MMM 官方安裝文檔 http://mysql-mmm.org/mmm2:guide
MMM Manual http://mysql-mmm.org/mysql-mmm.html