|mysql-bin.000003 | 2328055 | | |
+------------------------+------------+-------------------+-------------------------+
1row in set (0.00 sec)
11、从库设置同步主库
此处binlog文件与位置状态,是主库在步骤4锁表时show master status查看的位置状态。
CHANGE MASTER TO
MASTER_HOST='10.0.0.2',
MASTER_PORT=8306,
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=26314;
12、开启从库同步并确认同步是否成功
使用start slave开启同步功能,使用show slave status\G查看同步是否成功
mysql>start slave;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status\G #\G不按表格输出
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 10.0.0.2
Master_User: replication
Master_Port: 8306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 136270
Relay_Log_File:mysqld-relay-bin.000002
Relay_Log_Pos: 72697
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:
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: 98758
Relay_Log_Space: 110366
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: 622 #查看主从同步延迟,延迟大则可能需要优化
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
1row in set (0.00 sec)
#sql线程与IO线程都是YES,slave配置成功。
13、主库设置同步从库
由于从库是全备导入,原先在主库上配置的复制帐户也同样导入,所以这里不用在从库上新授权复制用户。
从库上的binlog文件与位置状态,是从库在刚导入时show master status查看到的位置状态。
CHANGEMASTER TO
MASTER_HOST='172.16.0.2',
MASTER_PORT=3306,
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=2328055;
#修改相应信息,直接把这些配置在mysql中粘贴即可。
14、开启同步并确认同步是否成功
使用start slave开启同步功能,使用show slave status\G查看同步是否成功
mysql>start slave;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 172.16.0.2
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 107
Relay_Log_File:mysqld-relay-bin.000006
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:
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: 556
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: 2
1row in set (0.00 sec)
#IO线程与sql线程都是正常。
15、互为主从测试
在两台mysql各创建一个库,看两边是否都能进行同步。
分别在主库上执行 create database test01;
从库上执行create database test02;
看两台数据库上执行show databases;,看是否都有test01表和test02表。
我的经过测试,双主测试成功。
生产环境MySQL主主同步主键冲突处理