2、在作为测试数据库的mysql上建立一个账户专门用于开发数据库来进行数据同步。
在开发数据库上测试账户develop是否可以访问测试数据库上的mysql。
mysql -u develop -p -h 192.168.2.3(输入密码***,可以访问说明设置正确)
3、验证数据库同步
完成双方数据库同步的配置后,我们需要在不同的主机进行配置验证。首先通过ssh登录开发数据库mysql> 输入show master status;,显示如下信息:
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 189 | developDB | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql>输入 show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.3
Master_User: develop
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 207
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 344
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: developDB
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: 207
Relay_Log_Space: 344
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)
通过sssh 登录测试数据库输入以下命令:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 207 | developDB | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.4
Master_User: develop
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 189
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: developDB
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: 189
Relay_Log_Space: 326
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)
如果两次的命令输入均显示如下内容,就证明我们的配置已经成功并生效。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
四、测试
通过前面的配置和验证,两台MySQL服务器已经建立了同步机制并可以正常运行了。下面将通过在双方数据库上添加新的表,来测试另外的服务器上是否可以及时同步过去。
1、首先登录开发数据库,在数据库developDB中建立一张新表,名字为test。
mysql>use developDB;
mysql>create table test(id int);
完成后,转到测试数据库服务器,通过如下命令查询,是否已经同步。
mysql>use developDB;
mysql>show tables;
+------------------+
| Tables_in_developDB |
+------------------+
| test |
+------------------+
1 row in set (0.00 sec)
由此可以知道,新建表格test已经被同步到测试数据库中。
2、登录测试数据库服务器,在数据库developDB中建立一张新表,名字为test2。
mysql>use developDB;mysql>create table test2(id int);
完成后,转到开发数据库服务器,通过如下命令查询,是否已经同步。
mysql>use developDB;
mysql>show tables;
+------------------+
| Tables_in_developDB |
+------------------+
| test2 |
+------------------+
1 row in set (0.00 sec)
通过测试可以确认,数据库可以进行双向的自动同步,确保数据的冗余和高可用。
五、总结
本文主要介绍了MySQL同步的配置及验证,通过配置使其应用到实际的生产环境中。除此之外,也可以利用my.cnf配置文件,实现单向及多主的同步,甚至有选择性的同步。本文中暂时不讨论其他的同步方式,建议参考MySQL的官方文档。