之后操作数据库一点要谨慎!!!
今天上午11点左右,不小心删除了数据库,操作步骤如下:
在idea的数据库插件中,点击数据库,右键,点击 Drop , 点击 OK。
数据库误删之后,如何还原
方法:
1、养成定期备份的习惯。经过这件事之后,我决定每周五下班前,拿半个小时,备份数据库,提交代码等等。
2、开启数据库二进制日志功能。
开启二进制日志功能
找到 [mysqld] 在mysqld下面添加
添加以下代码:
#mysql 二进制日志
log-bin = D:\devenv\MySQL\log\logbin.log
expire_logs_days=10
max_binlog_size=100M
重启mysl
输入命令验证:
show variables like '%log_bin%';
配置完成,一比一还原删除步骤。
创建数据库:home
创建user表,插入两条数据
删除数据库:
确定被删除
尝试恢复
查看刚才的日志文件
使用mysqlbinlog
Microsoft Windows [版本 10.0.19045.4046]
(c) Microsoft Corporation。保留所有权利。
D:\devenv\MySQL\bin>mysqlbinlog --no-defaults D:\devenv\MySQL\log\logbin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#240318 21:37:15 server id 1 end_log_pos 107 Start: binlog v 4, server v 5.5.62-log created 240318 21:37:15 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
i0P4ZQ8BAAAAZwAAAGsAAAABAAQANS41LjYyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAACLQ/hlEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA==
'/*!*/;
# at 107
#240318 21:37:48 server id 1 end_log_pos 192 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1710769068/*!*/;
SET @@session.pseudo_thread_id=2/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
CREATE DATABASE `home`
/*!*/;
# at 192
#240318 21:38:10 server id 1 end_log_pos 340 Query thread_id=3 exec_time=0 error_code=0
use `home`/*!*/;
SET TIMESTAMP=1710769090/*!*/;
CREATE TABLE `user` (
`user` varchar(255) NULL ,
`password` varchar(255) NULL
)
/*!*/;
# at 340
#240318 21:38:25 server id 1 end_log_pos 408 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1710769105/*!*/;
BEGIN
/*!*/;
# at 408
#240318 21:38:25 server id 1 end_log_pos 538 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1710769105/*!*/;
INSERT INTO `user` (`user`, `password`) VALUES ('ipkiss', '123456')
/*!*/;
# at 538
#240318 21:38:25 server id 1 end_log_pos 565 Xid = 30
COMMIT/*!*/;
# at 565
#240318 21:38:31 server id 1 end_log_pos 633 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1710769111/*!*/;
BEGIN
/*!*/;
# at 633
#240318 21:38:31 server id 1 end_log_pos 767 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1710769111/*!*/;
INSERT INTO `user` (`user`, `password`) VALUES ('admin', 'admin123456')
/*!*/;
# at 767
#240318 21:38:31 server id 1 end_log_pos 794 Xid = 32
COMMIT/*!*/;
# at 794
#240318 21:41:14 server id 1 end_log_pos 918 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1710769274/*!*/;
SET @@session.sql_mode=2097152/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=33/*!*/;
/* ApplicationName=IntelliJ IDEA 2023.3.2 */ drop schema home
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
D:\devenv\MySQL\bin>
能看到见表语句、插值语句、删除语句
恢复到最原始的地方 从4开始
918时间点作了删除操作,那么就恢复到794时间点
mysqlbinlog --no-defaults --start-position="4" --stop-position="794" D:\devenv\MySQL\log\logbin.000001 | mysql -u root -p
数据回来了:
参考文档: