实现双向复制的方法
第一步: 在从机创建主机用户
打开从机的mysql命令行 (192.168.1.29)
mysql> GRANT REPLICATION SLAVE ON . TO 'master_replicator'@'%' IDENTIFIED BY 'master';
现在我们用这个命令‘show master status’来确定当前二进制日志文件的文件名和位置。记住这个细节!在我们的事例中得到以下输出:
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000153 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
继续:mysql> FLUSH PRIVILEGES;
选择数据库 :mysql> USE newdatabase;
锁数据库防止任何新的更改:FLUSH TABLES WITH READ LOCK;
Step 2: 用主机用户连接从机(192.168.1.30):
在主机上打开mysql命令行
停止从机 : Stop slave;
执行命令
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.29',
-> MASTER_USER='master_replicator',
-> MASTER_PASSWORD='master',
-> MASTER_LOG_FILE='mysql-bin.000153',
-> MASTER_LOG_POS=106;
- 重启从机开始复制 : Start Slave;
下面命令检查系统状态 :
SHOW SLAVE STATUS\G;
你可以看到 192.168.1.30:
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.29
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000013
Read_Master_Log_Pos: 98
Relay_Log_File: PC-relay-bin.000074
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000013
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: demo
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: 98
Relay_Log_Space: 235
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.01 sec)
ERROR:
No query specified