从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)