要求:
2台计算机数据同步,使用MySQL数据库。
操作环境:
Windows XP,
MySQL版本:mysql-5.5.21-win32.msi
主服务器(master):
IP:10.0.1.174
MySQL 安装路径:E:\MySQL
从服务器(slave):
IP:10.0.1.74
MySQL 安装路径:C:\MySQL
一、搭建步骤
1) 主服务器的my.ini(在E:\MySQL目录下),在mysqld配置项中加入以下设置:
[mysqld]
server-id=1 #数据库ID号, 为1时表示为Master
log-bin=mysql-bin #启用二进制日志
binlog-do-db=test #需要同步的数据库名
2) 从服务器的my.ini(在C:\MySQL目录下),在mysqld配置项中加入以下设置:
[mysqld]
server-id=2 #如果需要增加Slave库,则此id往后顺延
replicate-do-db=test #需要备份的数据库名
据网上消息介绍,Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数;
3) 重启主从服务器的MySQL服务。
4) 登录10.0.1.174主服务器,执行如下命令:
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 107
Binlog_Do_DB: test
Binlog_Ignore_DB:
1 row in set (0.03 sec)
5) 登录10.0.1.74从服务器,执行如下命令;
(master_log_file对应主服务器显示的File,master_log_pos对应主服务器显示的Position)
mysql> change master to
-> master_host='10.0.1.174',
-> master_user='test',
-> master_password='test',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=107;
mysql> slave start;
mysql> show slave status\G; 执行完这个命令会显示:
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 10.0.1.174
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: pc-zx-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 107
Relay_Log_Space: 107
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)
从上面的状态可以看到,此时还无法从主服务器同步。
6) 在10.0.1.174主服务器,执行如下命令,授权给从服务器,同步相关数据:
mysql> grant replication slave on *.* to 'test'@'10.0.1.74' identified by 'test';
7) 在10.0.1.74从服务器,查看状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.1.174
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 107
Relay_Log_File: pc-zx-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 107
Relay_Log_Space: 412
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.01 sec)
其中:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
说明主从服务器的同步配置OK。
现在可以在主服务器插入数据进行测试,是否从服务器同步......