1. 程式人生 > >mysql根據binlog恢復資料

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