您的位置:首页 > 数据库 > MySQL

MySQL 5.6基于binlog的主从复制

2017-01-22 00:00 411 查看
MySQL从5.6版本开始主从复制,支持基于日志(binlog)和基于GTID(全局事务标示符)两种方式。
本文介绍了基于日志方式的配置方法。
####环境介绍:

master数据库IP:192.168.1.100

slave数据库IP:192.168.1.101

mysql版本:5.6.13

####具体配置步骤

修改master配置文件并重启服务:

[mysqld]
server_id=1
log-bin=mysql-bin
binlog_cache_size = 1M
binlog_format=mixed
expire_logs_days=15
binlog-ignore-db=test #不记录binlog
replicate-ignore-db=test #不复制test库的binlog


修改slave配置文件并重启服务:

[mysqld]
#注意id不能和master重复
server_id=2
log-bin=mysql-bin
binlog_cache_size = 1M
binlog_format=mixed
expire_logs_days=151.
binlog-do-db = wxcop  #需要复制的数据库
binlog-do-db = hppd  #需要复制的数据库
binlog-ignore-db=test #不记录binlog的数据库
replicate-ignore-db=test #不复制的数据库


在master上建立用于复制的账号

grant replication slave, replication client on *.* to 'userName'@'192.168.1.101' identified by 'pwd';


备份master的数据

#master数据库先进行锁表,以保证数据一致性
FLUSH TABLES WITH READ LOCK;
#查询当前master数据库状态
SHOW MASTER STATUS;

查询结果如下,
+—————–+————+—————-+——————–+
|File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+—————–+————+—————-+——————–+
|mysql-bin.000001 | 173 | | |
+—————–+————+—————-+——————–+
记住文件名和pos号

#备份数据库
mysqldump -u root -p mydb > /tmp/mydb.sql
#备份完毕,现在可以解锁数据库表
UNLOCK TABLES;


拷贝备份文件到slave,并导入

mysql -u root -p -B mydb </tmp/mydb.sql


在slave上配置需要同步的master信息

#设置master信息,注意账号,文件名和pos要和上面设置和查询的保持一致
CHANGE MASTER TO MASTER_HOST='192.168.1.100',MASTER_USER='databak',MASTER_PASSWORD='pwd',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=173;


开启复制

START slave;


查询slave状态

show slave status\G

查询结果示例:

Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.100
Master_User: databak
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 549828719
Relay_Log_File: mysqld-relay-bin.000023
Relay_Log_Pos: 431317387
Relay_Master_Log_File:mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: wxcop,hppd
Replicate_Ignore_DB: test
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: 549828719
Relay_Log_Space: 549829219
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: ce9d2f45-e728-11e4-ab8b-c442f91fabfe
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 8640
3ff0
0
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

特别需要注意的是Last_Error,如果出现异常,mysql就会停止主从复制。根据error信息定位问题,解决后需要重新启动slave。

STOP slave
START slave
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: