mysql 5.7 binlog无法导入数据

将命令输出到文件中:

mysqlbinlog /app/data/mysql-bin.000001 > binlog.sql

测试删除数据库:

[root@ceshi ~]# mysql -uroot -p'123456'
mysql> select * from testdb.t1;
+------+
| id   |
+------+
|   10 |
|   11 |
|   12 |
+------+
[root@ceshi ~]#
mysql> drop database testdb;
Query OK, 1 row affected (0.03 sec)

查看数据库并导入binlog:

[root@ceshi ~]# mysql -uroot -p'123456' -e "select * from testdb.t1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'testdb.t1' doesn't exist
[root@ceshi ~]# 
[root@ceshi ~]# mysql -uroot -p'123456' < binlog.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@ceshi ~]# mysql -uroot -p'123456' -e "select * from testdb.t1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'testdb.t1' doesn't exist

查看文件内容:

create database testdb
/*!*/;
# at 324
#190113 11:33:30 server id 1  end_log_pos 389 CRC32 0x7744b794  GTID    last_committed=1        sequence_number=2
SET @@SESSION.GTID_NEXT= '86cba1e7-14c5-11e9-9319-fa163eaab8a4:2'/*!*/;

看到这里大概就知道原因了,因为数据库启用了gtid所以导出的时候默认也带了gtid。
解析后的binlog文件中每个事物开始前,都执行了SET @@SESSION.GTID_NEXT=操作来执行下一个要执行的GTID。但是这些GTID都已经存在数据库的Executed_Gtid_Set中(因为这些GTID都之前已经在实例上执行过),所以我们执行解析后的binlog文件时,所有的事物都被忽略(已经存在于Executed_Gtid_Set集合中的GTID会跳过)。
因此,在使用gtid的mysql上使用mysqlbinlog解析数据来恢复数据库的话,在执行mysqlbinlog命令时加上–skip-gtids=true参数,解析的sql中就不会包含gtid信息了。

再次执行命令

mysqlbinlog /app/data/mysql-bin.000001 --skip-gtids=true > binlog.sql-3

对比两次输出结果:

vimdiff binlog.sql binlog.sql-3
在这里插入图片描述

测试跳过gtid,导入:

[root@ceshi ~]# mysql -uroot -p'123456' < binlog.sql-3 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@ceshi ~]# 
[root@ceshi ~]# mysql -uroot -p'123456' -e "select * from testdb.t1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| id   |
+------+
|   10 |
|   11 |
|   12 |
+------+

至此,数据成功导入。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 5.7 的二进制日志(binlog)相关配置包括以下几个方面: 1. 启用 binlog:在 MySQL 配置文件 `my.cnf` 中,设置 `log-bin` 参数为二进制日志的文件名,例如 `log-bin=mysql-bin`,表示启用二进制日志,并将日志存储在名为 `mysql-bin` 的文件中。 2. 配置 binlog 格式:在 MySQL 配置文件 `my.cnf` 中,设置 `binlog_format` 参数表示二进制日志的格式,支持三种格式:`STATEMENT`、`ROW` 和 `MIXED`。其中 `STATEMENT` 表示记录 SQL 语句,`ROW` 表示记录行数据,`MIXED` 表示记录 SQL 语句和行数据。例如,设置 `binlog_format=ROW` 表示采用行数据格式记录二进制日志。 3. 配置 binlog 写入方式:在 MySQL 配置文件 `my.cnf` 中,设置 `sync_binlog` 参数表示二进制日志的写入方式,支持三种方式:`0`、`1` 和 `N`。其中 `0` 表示不做任何同步,`1` 表示每次写入 binlog 都要将数据同步到磁盘,`N` 表示每写入 N 个事务才将数据同步到磁盘。例如,设置 `sync_binlog=1` 表示每次写入 binlog 都要将数据同步到磁盘。 4. 配置 binlog 文件大小:在 MySQL 配置文件 `my.cnf` 中,设置 `max_binlog_size` 参数表示二进制日志的文件大小,单位为字节,默认为 1GB。例如,设置 `max_binlog_size=1073741824` 表示二进制日志文件大小为 1GB。 5. 配置 binlog 保留时间:在 MySQL 配置文件 `my.cnf` 中,设置 `expire_logs_days` 参数表示二进制日志的保留时间,单位为天,默认为 0,表示不限制。例如,设置 `expire_logs_days=7` 表示二进制日志保留 7 天。 以上就是 MySQL 5.7 中二进制日志(binlog)相关的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值