mysql根据binlog恢复数据

mysql根据binlog恢复数据

创建表

mysql> CREATE TABLE `test` (
                              `id` int(11) NOT NULL AUTO_INCREMENT,
                              `price` int(10) DEFAULT NULL,
                              PRIMARY KEY (`id`)
                          );
Query OK, 0 rows affected

mysql> desc test;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| price | int(10) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
2 rows in set

插入数据

mysql> insert into test values(1,2),(2,3),(3,4);
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test;
+----+-------+
| id | price |
+----+-------+
|  1 |     2 |
|  2 |     3 |
|  3 |     4 |
+----+-------+
3 rows in set

查看binlog记录

D:\mysql-5.6.36-winx64\data> mysqlbinlog mysql-bin.000001
// .....
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
)
/*!*/;
SET TIMESTAMP=1545790350/*!*/;
BEGIN
/*!*/;
SET TIMESTAMP=1545790350/*!*/;
insert into test values(1,2),(2,3),(3,4)
/*!*/;
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

删除表

# 暂停mysql的二进制日志功能
mysql> set SQL_LOG_BIN=0;
Query OK, 0 rows affected

mysql> drop table test;
Query OK, 0 rows affected

mysql> select * from test;
1146 - Table 0000.test does not exist

恢复数据

mysql> set SQL_LOG_BIN=1;
Query OK, 0 rows affected

D:\mysql-5.6.36-winx64\data>mysqlbinlog mysql-bin.000001|mysql -uroot -p
Enter password: ****


mysql> select * from test;
+----+-------+
| id | price |
+----+-------+
|  1 |     2 |
|  2 |     3 |
|  3 |     4 |
+----+-------+
3 rows in set

猜你喜欢

转载自blog.csdn.net/u013887008/article/details/85257894