环境:
两台win10电脑,主数据库所在服务器ip:10.50.12.50
mysql版本均为5.7.17。(此文章适用于Mysql5.7版本)
配置主数据库:
修改my.ini文件。我的在C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
# Binary Logging.
#主从配置,二进制日志文件,
log-bin = "C:/ProgramData/MySQL/MySQL Server 5.7/Data/mysql-bin/"
# 使binlog在每N次binlog写入后与硬盘 同步
sync-binlog=1
#需要同步的数据库,可以写多行,也可以一行以逗号隔开。我的数据库为test
binlog-do-db = test
#不需要同步的数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
# Server Id.
server-id=1
配置从数据库:
server-id=2
#主从配置
log-bin=mysql-bin
#同步的表,支持通配符%
replicate_wild_do_table=test.%
#不同步的表
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%
注意:每个数据库的server-id不可以相同。
主库:
使用黑窗口,登录mysql。
1)创建用户,指定主数据库所在服务器的ip,一个新的数据库用户名及密码。如图:slave_test/slave_123
mysql> grant replication slave on *.* to 'slave_tset'@'10.50.12.50' identified by 'slave_123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
2) 执行命令flush logs,以防在第三步信息错误
mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)
3)查看主数据库状态信息
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 154
Binlog_Do_DB: test
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
此时再次检查与自己在my.ini中配置的信息是否相同,避免错误。并记住File,Position字段对应的值。
从库:
1)重启mysql
2)停止同步
mysql> stop slave;
Query OK, 0 rows affected (0.07 sec)
3)设置登录主数据库的账号和密码等信息
mysql> change master to master_host='10.50.12.50',master_user='slave_test',master_password='slave_123',master_log_file='mysql-bin.000002', master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.22 sec)
4)开始同步
mysql> start slave;
Query OK, 0 rows affected (0.07 sec)
5)查看同步状态
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.50.12.50
Master_User: slave_test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: haha-relay-bin.000002
Relay_Log_Pos: 320
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: test.%
Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 526
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: a5f833ff-768c-11e7-93ef-507b9de7f130
Master_Info_File: C:\ProgramData\MySQL\MySQL Server 5.7\Data\master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
以上信息中,注意查看某些字段的值是否与我们设置的相同。如: Master_Host,Master_User,Master_Port等。Seconds_Behind_Master: 0,Slave_IO_Running: Yes和Slave_SQL_Running:Yes表示成功
我在实现过程中,遇见Slave_IO_Running:No,值为no,百度后发现是因为设置主数据库登录等信息时部分值不正确。然后把从主库第二条命令开始执行了一遍即可。