Mysql的Bin log数据恢复:不小心删除数据库
前言:因为不小心删除了测试机器上Mysql的一整个数据库Schema,因为是测试机所以没有做备份,现在通过MySQL的Bin log方式恢复到删除以前的数据库。
当然做Bin log的数据恢复前提是已经打开Bin log的功能,如果又没做数据备份,又没打开Bin log日志,那你就可能需要考虑快照等其它方式从系统的角度去恢复。
Bin log 常用于数据增量备份和恢复,以及数据库主从复制。如果没有开启,可以通过如下方式打开:
1、打开mysql的binlog功能
mysql是支持增量备份,但要打开mysql的bin log功能。
修改mysql的配置文件。linux是/etc/my.cnf,windows是mysql的安装目录/my.ini
在[mysqld]下面加上log-bin一行代码,如下面:
# Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin # binary logging format - mixed recommended binlog_format=mixed。
2、用如下方式查看二进制日志状态:是否开启
mysql> show variables like 'log_%';
3、查看所有二进制日志文件:
mysql> show libary logs;
mysql> show binary logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 201 | | mysql-bin.000002 | 351 | | mysql-bin.000003 | 276 | | mysql-bin.000004 | 201 | | mysql-bin.000005 | 16509 |
4、Mysql查看二进制日志文件的操作日志
#mysqlbinlog --start-position=0 /mydata/data/mysql-bin.000089
[root@test mysql]# mysqlbinlog --start-position=0 --stop-position=500 mysql-bin.000091 Warning: option 'start-position': unsigned value 0 adjusted to 4 /*!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 #151022 18:00:43 server id 1 end_log_pos 107 Start: binlog v 4, server v 5.5.38-log created 151022 18:00:43 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' y7MoVg8BAAAAZwAAAGsAAAABAAQANS41LjM4LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAADLsyhWEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA== '/*!*/; # at 107 #151022 23:27:50 server id 1 end_log_pos 198 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1445527670/*!*/; SET @@session.pseudo_thread_id=2/*!*/; SET @@session.foreign_key_checks=0, @@session.sql_auto_is_null=0, @@session.unique_checks=0, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1608515584/*!*/; 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=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; DROP SCHEMA IF EXISTS `pandora`/*!*/; # at 198 #151022 23:27:50 server id 1 end_log_pos 346 Query thread_id=2 exec_time=0 error_code=0
5、通过Bin log恢复数据.
因为我整个Schema都删掉了,又没备份,正好开启了bin
log日志,所以把历史的bin-log都重新执行了一遍,重新恢复到误删以前的版本,(我这里总共有91个文件,批量处理的):(9999999999999:是为了省掉去查找每一个bin-log日志文件的起始结束位置,设的一个无穷大的数字,简化操作.)
#mysqlbinlog /var/lib/mysql/mysql-bin.000001 --start-position=0 --stop-position=9999999999999 | mysql -uroot -p123456 #mysqlbinlog /var/lib/mysql/mysql-bin.000002 --start-position=0 --stop-position=9999999999999 | mysql -uroot -p123456 #mysqlbinlog /var/lib/mysql/mysql-bin.000003 --start-position=0 --stop-position=9999999999999 | mysql -uroot -p123456 ... ...
所以总结结论是:
其它:
1、还有个sql_log
mysql> show variables like 'sql_log_%';
Mysql开启关闭sql二进制日志:
mysql> set sql_log_bin=0; //关闭
set session sql_log_bin=0;
2、查找文件位置:
find / -name my.cnf
3、linux 查看当前所在目录的全路径
pwd命令:
/var/lib/mysql
4、查看当前binary log的情况:
mysql>show master status;
5、在my.cnf/my.ini中设定binary logs回滚天数:
expire_logs_days = 7
6、查看Master的bin log日志
mysql> show master logs; +-----------------+-----------+ | Log_name | File_size | +-----------------+-----------+ | log-bin.000001 | 98 | +-----------------+-----------+ 1 row in set (0.00 sec) ---------------------