如何利用MySQL的binlog恢復(fù)誤刪數(shù)據(jù)庫(kù)詳解
1 查看當(dāng)前數(shù)據(jù)庫(kù)內(nèi)容并備份數(shù)據(jù)庫(kù)
查看數(shù)據(jù)庫(kù)信息:

備份數(shù)據(jù)庫(kù):
[root@localhost ~]# mysqldump -u root -p t > /mnt/t.sql Enter password: [root@localhost ~]# ll /mnt/t.sql -rw-r--r-- 1 root root 1771 Aug 25 11:56 /mnt/t.sql
2 開啟bin_log功能
首先查看數(shù)據(jù)庫(kù)是否開啟bin_log功能
mysql> show variables like "%log_bin%";

需要修改mysql的配置文件,/etc/的my.cnf,添加一句log_bin = mysql_bin即可


3 模擬誤操作(插入3條數(shù)據(jù),刪除數(shù)據(jù)庫(kù))
mysql> insert into t1 values (3); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (4); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (5); Query OK, 1 row affected (0.00 sec) mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | | 5 | | 4 | | 3 | +------+ 5 rows in set (0.00 sec) mysql> flush logs; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql_bin.000003 | 106 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
刪除數(shù)據(jù):
mysql> truncate t1; Query OK, 0 rows affected (0.00 sec) mysql> select * from t1; Empty set (0.00 sec)
此時(shí)突然數(shù)據(jù)庫(kù)損壞或者人為刪除
mysql> drop table t1; Query OK, 0 rows affected (0.00 sec) mysql> show tables; Empty set (0.00 sec)
4 數(shù)據(jù)恢復(fù)
1 用已經(jīng)備份的/mnt/t.sql來(lái)恢復(fù)數(shù)據(jù)
mysql> source /mnt/t.sql; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> show tables; +-------------+ | Tables_in_t | +-------------+ | t1 | +-------------+ 1 row in set (0.00 sec) mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)
2 還有三條數(shù)據(jù)沒有恢復(fù),怎么辦。只能用bin-log來(lái)恢復(fù)
[root@localhost ~]# mysqlbinlog --no-defaults /var/lib/mysql/mysql_bin.000002 | mysql -u root -p123.com t
mysql> use t; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t1; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | +------+ 5 rows in set (0.00 sec) mysql>
5 總結(jié)
備份數(shù)據(jù)
mysqldump -uroot -p123456 test -l -F '/tmp/test.sql' -l:讀鎖(只能讀取,不能更新) -F:即flush logs,可以重新生成新的日志文件,當(dāng)然包括log-bin日志
查看binlog日志
mysql>show master status;
導(dǎo)入之前備份數(shù)據(jù)
mysql -uroot -p t -v -f </mnt/t.sql -v查看導(dǎo)入的詳細(xì)信息 -f是當(dāng)中間遇到錯(cuò)誤時(shí),可以skip過(guò)去,繼續(xù)執(zhí)行下面的語(yǔ)句
恢復(fù)binlog-file二進(jìn)制日志文件
mysqlbinlog --no-defaults binlog-file | mysql -uroot -p t
從某一(367)點(diǎn)開始恢復(fù)
mysqlbinlog --no-defaults --stop-position="367" mysql-bin.000001| mysql -uroot -p t
先查好那一點(diǎn),用more來(lái)查看
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | more
然后恢復(fù)
[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -p t
重置binlog日志
mysql> reset master; Query OK, 0 rows affected (0.01 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 106 | | | +------------------+----------+--------------+------------------+
mysql> flush logs;#關(guān)閉當(dāng)前的二進(jìn)制日志文件并創(chuàng)建一個(gè)新文件,新的二進(jìn)制日志文件的名字在當(dāng)前的二進(jìn)制文件的編號(hào)上加1。
到此這篇關(guān)于如何利用MySQL的binlog恢復(fù)誤刪數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)MySQL binlog恢復(fù)誤刪數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何解決MYSQL8.4.1 MySQL84--ERROR 1524(HY000):Plugin&n
這篇文章主要介紹了如何解決MYSQL8.4.1 MySQL84--ERROR 1524(HY000):Plugin ‘msql_native_password‘ is not loaded問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SQL Server 數(shù)據(jù)庫(kù)的備份詳細(xì)介紹及注意事項(xiàng)
這篇文章主要介紹了SQL Server 備份詳細(xì)介紹及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2016-12-12
一文總結(jié)MySQL中數(shù)學(xué)函數(shù)有哪些
MySQL函數(shù)包括數(shù)學(xué)函數(shù)、字符串函數(shù)、日期和時(shí)間函數(shù)、條件判斷函數(shù)、系統(tǒng)信息函數(shù)、加密函數(shù)等,下面這篇文章主要給大家介紹了關(guān)于MySQL中數(shù)學(xué)函數(shù)有哪些的相關(guān)資料,需要的朋友可以參考下2023-02-02
MySQL 8.0.23中復(fù)制架構(gòu)從節(jié)點(diǎn)自動(dòng)故障轉(zhuǎn)移的問(wèn)題
這篇文章主要介紹了MySQL 8.0.23中復(fù)制架構(gòu)從節(jié)點(diǎn)自動(dòng)故障轉(zhuǎn)移的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
MYSQL ZIP免安裝版配置步驟及圖形化管理工具mysql-workbench
在 windows7 64位操作系統(tǒng)下配置mysql-5.5.25-winx64 (免安裝版),記錄步驟如下2014-03-03

