MySQL 自動(dòng)備份與數(shù)據(jù)庫(kù)被破壞后的恢復(fù)方法
[2] 當(dāng)數(shù)據(jù)庫(kù)被修改后的恢復(fù)方法
數(shù)據(jù)庫(kù)被修改,可能存在著多方面的原因,被入侵、以及相應(yīng)程序存在Bug等等,這里不作詳細(xì)介紹。這里將只介紹在數(shù)據(jù)庫(kù)被修改后,如果恢復(fù)到被修改前狀態(tài)的方法。
具體和上面所述的“數(shù)據(jù)庫(kù)被刪除后的恢復(fù)方法”相類(lèi)似。這里,測(cè)試用數(shù)據(jù)庫(kù)接著使用剛剛在前面用過(guò)的test。這里為了使剛剛接觸數(shù)據(jù)庫(kù)的朋友不至于理解混亂,我們?cè)俅蔚卿浀組ySQL服務(wù)器上確認(rèn)一下剛剛建立的測(cè)試用的數(shù)據(jù)庫(kù)test的相關(guān)信息。
[root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務(wù)器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 to server version: 4.1.20 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; ← 查看當(dāng)前存在的數(shù)據(jù)庫(kù) +-------------+ | Database | +-------------+ | mysql | | test | +------------+ 2 rows in set (0.00 sec) mysql> use test ← 連接到test數(shù)據(jù)庫(kù) 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> show tables; ← 查看test數(shù)據(jù)庫(kù)中存在的表 +-------------------+ | Tables_in_test | +-------------------+ | test | +-------------------+ 1 row in set (0.00 sec) mysql> select * from test; ← 查看數(shù)據(jù)庫(kù)中的內(nèi)容 +------+--------------------+ | num | name | +------+--------------------+ | 1 | Hello,CentOS| +------+--------------------+ 1 row in set (0.01 sec) mysql> exit ← 退出MySQL服務(wù)器 Bye |
然后,我們?cè)俅芜\(yùn)行數(shù)據(jù)庫(kù)備份腳本,將當(dāng)前狀態(tài)的數(shù)據(jù)庫(kù),再做一次備份。
[root@CentOS ~]# cd ← 回到腳本所在的root用戶的根目錄 [root@CentOS ~]# ./mysql-backup.sh ← 運(yùn)行腳本進(jìn)行數(shù)據(jù)庫(kù)備份 |
接下來(lái),我們?cè)俅蔚卿浀組ySQL服務(wù)器中,對(duì)測(cè)試用的數(shù)據(jù)庫(kù)test進(jìn)行一些修改,以便于測(cè)試數(shù)據(jù)恢復(fù)能否成功。
[root@sample ~]# mysql -u root -p ← 用root登錄到MySQL服務(wù)器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 to server version: 4.1.20 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← 連接到test數(shù)據(jù)庫(kù) 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> update test set name='Shit,Windows'; ← 然后將test中表的值重新定義為“Shit,Windows”(原來(lái)為“Hello,CentOS”) Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from test; ← 確認(rèn)test中的表被定義的值 +------+--------------------+ | num | name | +------+-------------------+ | 1 | Shit,Windows | ← 確認(rèn)已經(jīng)將原test數(shù)據(jù)庫(kù)表中的值修改為新的值“Shit,Windows” +------+-------------------+ 1 row in set (0.00 sec) mysql> exit ← 退出MySQL服務(wù)器 Bye |
以上,我們就等于模擬了數(shù)據(jù)庫(kù)被篡改的過(guò)程。接下來(lái),是數(shù)據(jù)庫(kù)被“篡改”后,用備份進(jìn)行恢復(fù)的方法。
[root@CentOS ~]# /bin/cp -Rf /backup/mysql/test/ /var/lib/mysql/ ← 復(fù)制備份的數(shù)據(jù)庫(kù)test到相應(yīng)目錄 |
然后,再次登錄到MySQL服務(wù)器上,看數(shù)據(jù)庫(kù)是否被恢復(fù)到了被“篡改”之前的狀態(tài)。
[root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務(wù)器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 to server version: 4.1.20 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← 連接到test數(shù)據(jù)庫(kù) 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 test; ← 查看數(shù)據(jù)庫(kù)中的內(nèi)容 +------+----------------+ | num | name | +------+----------------+ | 1| Hello,CentOS | ← 確認(rèn)數(shù)據(jù)表中的內(nèi)容與被修改前定義的“Hello,CentOS”一樣! +------+----------------+ 1 row in set (0.01 sec) mysql> exit ← 退出MySQL服務(wù)器 Bye |
以上結(jié)果表示,數(shù)據(jù)庫(kù)被修改后,用備份后的數(shù)據(jù)庫(kù)成功的將數(shù)據(jù)恢復(fù)到了被“篡改”前的狀態(tài)。
測(cè)試后…
測(cè)試完成后,將測(cè)試用過(guò)的遺留信息刪除。
[root@CentOS ~]# mysql -u root -p ← 用root登錄到MySQL服務(wù)器 Enter password: ← 輸入MySQL的root用戶密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 to server version: 4.1.20 Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use test ← 連接到test數(shù)據(jù)庫(kù) 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> drop table test; ← 刪除test數(shù)據(jù)庫(kù)中的表 Query OK, 0 rows affected (0.01 sec) mysql> drop database test; ← 刪除測(cè)試用數(shù)據(jù)庫(kù)test Query OK, 0 rows affected (0.00 sec) mysql> show databases; ← 查看當(dāng)前存在的數(shù)據(jù)庫(kù) +-------------+ | Database | +-------------+ | mysql | ← 確認(rèn)測(cè)試用數(shù)據(jù)庫(kù)test不存在、已被刪除 +-------------+ 1 row in set (0.00 sec) mysql> exit ← 退出MySQL服務(wù)器 Bye |
以上介紹了用我們自己建立的一段Shell腳本,通過(guò)mysqlhotcopy來(lái)備份數(shù)據(jù)庫(kù)的方法。
對(duì)于許多個(gè)人愛(ài)好者來(lái)說(shuō),組建服務(wù)器可能不是很考慮數(shù)據(jù)被破壞以及數(shù)據(jù)被破壞后的恢復(fù)工作。但不能不說(shuō),對(duì)于服務(wù)器來(lái)說(shuō),數(shù)據(jù)破壞后的恢復(fù)效率也是區(qū) 別業(yè)余和專(zhuān)業(yè)的因素之一。所以筆者建議,在您配置好了Web服務(wù)器以及MySQL服務(wù)器等等的時(shí)候,千萬(wàn)不要急于應(yīng)用它,而要想辦法在有限的(硬件、軟件)條件下使它“堅(jiān)不可摧”之后,再考慮應(yīng)用的問(wèn)題。
而且,以上介紹的方法中提到的數(shù)據(jù)庫(kù)自動(dòng)備份腳本雖然被設(shè)置為每天定時(shí)運(yùn)行,但當(dāng)您運(yùn)行某些與MySQL相關(guān)聯(lián)的程序(論壇、社區(qū)等等)時(shí),做一些可 能危及數(shù)據(jù)安全的操作的時(shí)候,運(yùn)行數(shù)據(jù)庫(kù)備份腳本即時(shí)備份當(dāng)前狀態(tài)數(shù)據(jù)庫(kù),也是非常有幫助的,至少可以在出現(xiàn)問(wèn)題后保證數(shù)據(jù)庫(kù)方面的可恢復(fù)性。
- 教你如何恢復(fù)使用MEB備份的MySQL數(shù)據(jù)庫(kù)
- MySQL數(shù)據(jù)庫(kù)遭到攻擊篡改(使用備份和binlog進(jìn)行數(shù)據(jù)恢復(fù))
- 通過(guò)java備份恢復(fù)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- mysql數(shù)據(jù)庫(kù)備份及恢復(fù)命令 mysqldump,source的用法
- MySQL數(shù)據(jù)庫(kù)備份與恢復(fù)方法
- mysql 5.6 從陌生到熟練之_數(shù)據(jù)庫(kù)備份恢復(fù)的實(shí)現(xiàn)方法
相關(guān)文章
MySQL中Update、select聯(lián)用操作單表、多表,及視圖與臨時(shí)表的區(qū)別
本篇文章給大家分享了MySQL中Update、select聯(lián)用操作單表、多表,及視圖與臨時(shí)表的區(qū)別,有興趣的朋友學(xué)習(xí)下吧。2018-06-06關(guān)于mysql數(shù)據(jù)庫(kù)連接編碼問(wèn)題
這篇文章主要介紹了關(guān)于mysql數(shù)據(jù)庫(kù)連接編碼問(wèn)題,默認(rèn)的編碼和數(shù)據(jù)庫(kù)表中的數(shù)據(jù)使用的編碼是不一致的,如果是中文,那么在數(shù)據(jù)庫(kù)中執(zhí)行時(shí)已經(jīng)是亂碼了,需要的朋友可以參考下2023-04-04MySQL Community Server 8.0.11安裝配置方法圖文教程
這篇文章主要為大家詳細(xì) 介紹了MySQL Community Server 8.0.11安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Windows系統(tǒng)下MySQL添加到系統(tǒng)服務(wù)方法(mysql解壓版)
這篇文章主要介紹了Windows系統(tǒng)下MySQL添加到系統(tǒng)服務(wù)方法,主要針對(duì)mysql解壓版,感興趣的朋友參考下吧2016-05-05Mysql?InnoDB中B+樹(shù)索引使用注意事項(xiàng)
這篇文章主要為大家介紹了Mysql?InnoDB中B+樹(shù)索引的注意事項(xiàng),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05mysql日志文件General_log和Binlog開(kāi)啟及詳解
MySQL中的數(shù)據(jù)變化會(huì)體現(xiàn)在上面日志中,下面這篇文章主要給大家介紹了關(guān)于mysql日志文件General_log和Binlog開(kāi)啟及詳解的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07MySQL數(shù)據(jù)庫(kù)INNODB表?yè)p壞修復(fù)處理過(guò)程分享
突然收到MySQL報(bào)警,從庫(kù)的數(shù)據(jù)庫(kù)掛了,一直在不停的重啟,打開(kāi)錯(cuò)誤日志,發(fā)現(xiàn)有張表壞了。innodb表?yè)p壞不能通過(guò)repair table 等修復(fù)myisam的命令操作?,F(xiàn)在記錄下解決過(guò)程2013-08-08