7. 集羣下的複製
7.1. 簡述
從MySQL 5.1 開始,就支持集羣+複製了,這對於想要構建一個高可用方案的用戶來說,無疑是個驚喜。在這種模式下,既有主從的實時備份,又有基於集羣的負載均衡,不足指出在於,從我的測試結果來看,這種方案下的性能還不是太高,仍有待改進。
集羣+複製的配置其實很簡單,就是配置好2個獨立的集羣后,把其中一個的SQL節點作爲另一個集羣SQL節點的slave即可。甚至可以使用下面幾種架構:
3個集羣,6個SQL節點,形成一個3個點環形的複製。
3個集羣,6個SQL節點,形成一個6個點環形的複製,把另一個SQL節點也利用起來。
7.2. 開始配置
7.2.1. master上的配置
由於集羣下的複製是基於row-based複製的,因此需要設置logbin-format的格式爲:ROW 或者 MIXED。
相對於普通的mysqld服務器配置,只需要增加類似如下2行:
- server-id = 1
- binlog_format = "ROW" #or MIXED
7.2.2. slave上的配置
新版本的MySQL已經不再通過 my.cnf 來指定master相關的信息了,而是通過 CHANGE MASTER 指令來管理。因此,slave上只需簡單增加類似如下幾行:
- server-id = 2
- relay-log-purge=1
- skip-slave-start
- replicate-ignore-db=mysql
7.3. 啓動slave
先按照正常的方式啓動master和slave上的mysqld後,執行SHOW PROCESSLIST 可以看到2個mysqld都只有2個進程:
- mysql> SHOW PROCESSLIST;
- +----+-------------+-----------+------+---------+------+-----------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+-------------+-----------+------+---------+------+-----------------------------------+------------------+
- | 1 | system user | | | Daemon | 0 | Waiting for event from ndbcluster | NULL |
- | 2 | root | localhost | NULL | Query | 0 | NULL | show processlist |
- +----+-------------+-----------+------+---------+------+-----------------------------------+------------------+
- 2 rows in set (0.00 sec)
在slave上執行 SHOW SLAVE STATUS 再看看:
- mysql> show slave status\G
- Empty set (0.00 sec)
可以看到,現在還沒有任何複製相關的配置。因此,我們需要先在master上添加一個賬戶用於複製:
- mysql> GRANT REPLICATION SLAVE, GRANT REPLICATION CLIENT ON *.* TO rep@’192.168.1.2’ IDENTIFIED BY ‘rep’;
- Query OK, 0 rows affected (0.00 sec)
然後,我們用 CHANGE MASTER 指令來指定一下 master 相關的參數:
- mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.2', MASTER_USER='rep', MASTER_PASSWORD='rep';
- Query OK, 0 rows affected (0.01 sec)
- mysql> SHOW SLAVE STATUS\G
- *************************** 1. row ***************************
- Slave_IO_State:
- Master_Host: 192.168.0.2
- Master_User: rep
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File:
- Read_Master_Log_Pos: 4
- Relay_Log_File: slave-relay-bin.000001
- Relay_Log_Pos: 4
- Relay_Master_Log_File:
- Slave_IO_Running: No
- Slave_SQL_Running: No
- 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: 0
- Relay_Log_Space: 106
- 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: NULL
- Master_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)
執行 START SLAVE 來啓動它。
- 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.0.2
- Master_User: rep
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: binlog.000001
- Read_Master_Log_Pos: 256
- Relay_Log_File: slave-relay-bin.000002
- Relay_Log_Pos: 398
- Relay_Master_Log_File: binlog.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: 256
- Relay_Log_Space: 557
- 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
- Master_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)
7.4. 簡單測試
這個留給讀者自己按照常規的測試去完成吧。
【編輯推薦】