Mysql的Binlog數(shù)據(jù)恢復(fù):不小心刪除數(shù)據(jù)庫詳解
Mysql的Bin log數(shù)據(jù)恢復(fù):不小心刪除數(shù)據(jù)庫
:因為不小心刪除了測試機器上Mysql的一整個數(shù)據(jù)庫Schema,因為是測試機所以沒有做備份,現(xiàn)在通過MySQL的Bin log方式恢復(fù)到刪除以前的數(shù)據(jù)庫。
當然做Bin log的數(shù)據(jù)恢復(fù)前提是已經(jīng)打開Bin log的功能,如果又沒做數(shù)據(jù)備份,又沒打開Bin log日志,那你就可能需要考慮快照等其它方式從系統(tǒng)的角度去恢復(fù)。
Bin log 常用于數(shù)據(jù)增量備份和恢復(fù),以及數(shù)據(jù)庫主從復(fù)制。如果沒有開啟,可以通過如下方式打開:
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、用如下方式查看二進制日志狀態(tài):是否開啟
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`<pre name="code" class="sql">/*!*/; # 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恢復(fù)數(shù)據(jù). 因為我整個Schema都刪掉了,又沒備份,正好開啟了bin log日志,所以把歷史的bin-log都重新執(zhí)行了一遍,重新恢復(fù)到誤刪以前的版本,(我這里總共有91個文件,批量處理的):(9999999999999:是為了省掉去查找每一個bin-log日志文件的起始結(jié)束位置,設(shè)的一個無窮大的數(shù)字,簡化操作.)
#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 ... ...
所以總結(jié)結(jié)論是:
- 1、切記一定要定期備份;
- 2、有備份的話恢復(fù)也快一點,可以從備份的時間點做增量備份,不需要像我這里從頭開始91個文件全部批量跑一遍,當然我用編輯器批量處理的也還算快;
- 3、另外一定要打開Bin-log日志,如果沒做備份也可以通過Bin-log日志恢復(fù)。
- 4、操作要小心。
其它:
1、還有個sql_log
mysql> show variables like 'sql_log_%';
Mysql開啟關(guān)閉sql二進制日志:
mysql> set sql_log_bin=0; //關(guān)閉
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中設(shè)定binary logs回滾天數(shù):
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) ---------------------
以上所述是小編給大家介紹的Mysql的Binlog數(shù)據(jù)恢復(fù)詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對本站網(wǎng)站的支持!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。