如下操作均在vmware 虚拟机中winows xp 测试成功
中间走了很多弯路,网上的很多资料都是针对5.1以前的版本,在新版中根本无法使用,所以根据自己的实践整理了这篇文章
主服务:192.168.131.21
从服务器:192.168.131.22
1、主机的配置
my.ini中配置:
server-id = 1
binlog_format = "ROW"
log-bin=mysql-bin
启动服务后进入mysql命令行执行如下操作
GRANT ALL ON *.* TO ‘slave1‘@‘192.168.131.22‘ IDENTIFIED BY ‘123456‘;
如果需要进行某个数据库的同步的话,最好只创建对应数据库的权限,如:
GRANT ALL ON test1.* TO ‘slave1‘@‘192.168.131.22‘ IDENTIFIED BY ‘123456‘;
这样只会同步该数据库的内容。
2、从服务器设置
my.ini:
server-id = 2
relay-log-purge=1
skip-slave-start
replicate-ignore-db=mysql #此处为不同步mysql数据库的内容此处如果为多数据库的话中间以逗号分开
然后启动mysql 进入命令行 执行如下命令
CHANGE MASTER TO MASTER_HOST=‘192.168.131.21‘, MASTER_USER=‘slave1‘, MASTER_PASSWORD=‘123456‘;
3、启动主从复制
首先启动主服务器的服务
其次启动从服务器的服务
然后在从服务器命令行执行
start slave;
然后执行如下命令 查看从服务器状态
SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.131.21
Master_User: slave1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 120
Relay_Log_File: rainpetlab2-relay-bin.000010
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000007
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: 120
Relay_Log_Space: 625
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
Master_UUID: 2ab70a0e-a1c4-11e2-9c60-60eb69d711ca
Master_Info_File: D:\wamp\data5\master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the sla
ve I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
注:Slave_IO_Running: Yes
Slave_SQL_Running: Yes必须为yes才行
还要注意的是:
我在做从服务器时,是停止服务,然后整体复制的data目录,如果不进行任何修改的话,会出现如下提示:
[ERROR] Slave I/O: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
121122 17:40:58 [Note] Slave I/O thread exiting, read up to log ‘FIRST‘, position 4
这个时候,应该删除data目录下的auto.cnf,然后重启服务
执行 start slave;即可
之后在主服务器上创建数据库
再从服务器上就可看到同步过来的数据了
还有如果更改从服务器的机器名的话 可这样这样执行命令:
stop slave;
reset slave;
start slave;