mysql binlog日誌 誤刪數據 用於恢復數據

1.binlog 日誌打開方法

my.cnf這個文件中加一行(Windowsmy.ini)。
#vi /etc/my.cnf
[mysqld]
log-bin=mysqlbin-log #
添加這一行就ok=號後面的名字本身定義吧
而後咱們能夠對數據庫作簡單的操做後到mysql數據文件所在的目錄來看binlog文件。 html

2. 查看本身的BINLOG的名字是什麼 mysql

命令:show binary logs
sql

Mysqlbinlog使用

 

 之後每次對錶的相關操做時候,這個File_size都會增大。 數據庫

 

3. 作了幾回操做後,它就記錄了下來。 session

命令:show binlog events 工具


Mysqlbinlog使用

 

4. mysqlbinlog 工具來顯示記錄的二進制結果,而後導入到文本文件,爲了之後的恢復。 this

詳細過程以下:
C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=4 --sto
url

p-position=106 mysqlbin-log.000001 > c:\\test1.txt spa

 

或者所有導出: 日誌

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog mysqlbin-log.000001 > c:\\test1.txt

 

 

test1.txt的文件內容:

;

;

DELIMITER ;

# at 4

#110916  9:51:06 server id 1  end_log_pos 98 Start: binlog v 4, server v 5.0.45-community-nt-log created 110916  9:51:06 at startup

# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.

ROLLBACK;

# at 98

#110916 10:11:21 server id 1  end_log_pos 28   Intvar

SET INSERT_ID=2;

# at 126

#110916 10:11:21 server id 1  end_log_pos 143        Query     thread_id=2    exec_time=0   error_code=0

use test;

SET TIMESTAMP=1316139081;

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1;

SET @@session.sql_mode=1344274432;

;

SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8;

insert into User (name,password) values('ddd','222');

DELIMITER ;

# End of log file

ROLLBACK ;

;

 

5. 導入結果到MYSQL中進行數據恢復。

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=134 --stop-position=330 mysqlbin-log.000001 | mysql -uroot -p

 

或者

C:\Program Files\MySQL\MySQL Server 5.0\bin>mysqlbinlog --start-position=134 --stop-position=330 mysqlbin-log.000001 >test1.txt

進入MYSQL導入

mysql> source c:\\test1.txt

 

還有一種辦法是根據日期來恢復

C:\Program Files\MySQL\MySQL Server 5.0\bin >mysqlbinlog --start-datetime="2009-09-14 0:20:00" --stop-datetim="2009-09-15 01:25:00" /diskb/bin-logs/xxx_db-bin.000001 | mysql -u root

 

6、查看數據

Select * from User

相關文章
相關標籤/搜索