MySQL的Master/Slave机制使数据的同步变得如此简单,其原理是在Master开启二进制日志功能(log-bin="log-file-name"),该功能会记录数据的任何操作(create, insert, update等),然后由Slave取回Master的二进制日志再自己执行一遍从而实现同步的功能。
因此它可实现数据或表结构的同步,在Master中新建/修改/删除表一样被同步到Slave中(这个正是我需要的,产品功能所限:Master在Windows平台,Slave在Linux平台)。
关于配置方面google一下,一大堆,就不废话了,主要记录下遇到的问题:
配置OK,同步失败并提示:
Slave_IO_State:
Master_Host: 192.168.0.211
Master_User: admin
Master_Port: 123
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 11296
Relay_Log_File: guangmean-desktop-relay-bin.000003
Relay_Log_Pos: 10154
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
解决方法:
1. 先停止Slave的同步: slave stop
2. 查看Master状态: mysql>SHOW master STATUS;
+-------------------------------+-------------+----------------------+--------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------------------+-------------+----------------------+--------------------------------------+
| mysql-bin.000001 | 24519 | guarddb | |
+-------------------------------+-------------+----------------------+--------------------------------------+
3. 手动同步:
mysql>change master to
>master_host='192.168.0.211',
>master_user='admin',
>master_password='123',
>master_port=65005,
>master_log_file='mysql-bin.000001',
>master_log_pos=24519;
4. 启动同步并查看状态:
mysql>slave start;
mysql>show slave status;
OK,程序在Windows下按小时/天建表被自动同步到Linux环境下,不用再用PHP写守护进程建表啦!!!
另goole后有方法: SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; 未测试
5. 开机自动同步:
注释掉配置文件配置项#skip-slave-start