關於MySQL-HA,目前有多種解決方案,比如heartbeat、drbd、mmm、共用儲存,但是它們各有優缺點。heartbeat、drbd配置較為複雜,需要自己寫指令碼才能實現MySQL自動切換,對於不會指令碼語言的人來說,這無疑是一種腦裂問題;對於mmm,生產環境中很少有人用,且mmm 管理端需要單獨運行一台伺服器上,要是想實現高可用,就得對mmm管理端做HA,這樣無疑又增加了硬體開支;對於共用儲存,個人覺得MySQL資料還是放在本地較為安全,存放裝置畢竟存在單點隱患。使用MySQL雙master+keepalived是一種非常好的解決方案,在MySQL-HA環境中,MySQL互為主從關係,這樣就保證了兩台MySQL資料的一致性,然後用keepalived實現虛擬IP,通過keepalived內建的服務監控功能來實現MySQL故障時自動切換。
下面,我把即將上線的一個生產環境中的架構與大家分享一下,看一下這個架構中,MySQL-HA是如何?的,環境拓撲如下
- MySQL-VIP:192.168.1.200
- MySQL-master1:192.168.1.201
- MySQL-master2:192.168.1.202
-
- OS版本:CentOS 5.4
- MySQL版本:5.0.89
- Keepalived版本:1.1.20
一、MySQL master-master配置
1、修改MySQL設定檔
兩台MySQL均如要開啟binlog日誌功能,開啟方法:在MySQL設定檔[MySQLd]段中加上log-bin=MySQL-bin選項
兩台MySQL的server-ID不能一樣,預設情況下兩台MySQL的serverID都是1,需將其中一台修改為2即可
2、將192.168.1.201設為192.168.1.202的主伺服器
在192.168.1.201上建立授權使用者
- MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
- Query OK, 0 rows affected (0.00 sec)
-
- MySQL> show master status;
- +------------------+----------+--------------+------------------+
- File Position Binlog_Do_DB Binlog_Ignore_DB
- +------------------+----------+--------------+------------------+
- MySQL-bin.000003 374
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
在192.168.1.202上將192.168.1.201設為自己的主伺服器
- MySQL> change master to master_host='192.168.1.201',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
- Query OK, 0 rows affected (0.05 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.1.201
- Master_User: replication
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: MySQL-bin.000003
- Read_Master_Log_Pos: 374
- Relay_Log_File: MySQL-master2-relay-bin.000002
- Relay_Log_Pos: 235
- Relay_Master_Log_File: MySQL-bin.000003
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- 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: 374
- Relay_Log_Space: 235
- 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: 0
- 1 row in set (0.00 sec)
3、將192.168.1.202設為192.168.1.201的主伺服器
在192.168.1.202上建立授權使用者
- MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
- Query OK, 0 rows affected (0.00 sec)
-
- MySQL> show master status;
- +------------------+----------+--------------+------------------+
- File Position Binlog_Do_DB Binlog_Ignore_DB
- +------------------+----------+--------------+------------------+
- MySQL-bin.000003 374
- +------------------+----------+--------------+------------------+
- 1 row in set (0.00 sec)
在192.168.1.201上,將192.168.1.202設為自己的主伺服器
- MySQL> change master to master_host='192.168.1.202',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
- Query OK, 0 rows affected (0.05 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.1.202
- Master_User: replication
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: MySQL-bin.000003
- Read_Master_Log_Pos: 374
- Relay_Log_File: MySQL-master1-relay-bin.000002
- Relay_Log_Pos: 235
- Relay_Master_Log_File: MySQL-bin.000003
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Replicate_Do_DB:
- Replicate_Ignore_DB:
- 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: 374
- Relay_Log_Space: 235
- 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: 0
- 1 row in set (0.00 sec)
4、MySQL同步測試
如上述均正確配置,現在任何一台MySQL上更新資料都會同步到另一台MySQL,MySQL同步在此不再示範