新聞中心
MySQL是一款常用的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),能夠提供高效、安全、靈活的數(shù)據(jù)管理和應(yīng)用開發(fā)。尤其是在Web應(yīng)用和企業(yè)級應(yīng)用中,MySQL的表現(xiàn)優(yōu)異,備受青睞。

成都創(chuàng)新互聯(lián)堅實的技術(shù)研發(fā)基礎(chǔ)贏得了行業(yè)內(nèi)的良好口碑,公司成立10多年來,為1000多家企業(yè)提供過網(wǎng)站建設(shè)、軟件開發(fā)、搜索引擎優(yōu)化技術(shù)、互聯(lián)網(wǎng)大數(shù)據(jù)整合營銷服務(wù),多年的技術(shù)服務(wù)成功經(jīng)驗、眾多的客戶使我們能懂得更多,做得更好。"讓您的網(wǎng)站跑起來"是我們一直追求的目標(biāo)!
然而,由于各種原因,MySQL的數(shù)據(jù)庫有可能會出現(xiàn)數(shù)據(jù)損壞、誤刪除、丟失等情況,導(dǎo)致數(shù)據(jù)丟失,這時需要進行數(shù)據(jù)庫還原操作。下面,將介紹一些簡單易學(xué)的MySQL數(shù)據(jù)庫還原方法。
一、使用mysqldump命令
mysqldump命令是MySQL中的一個備份工具,能夠?qū)⒄麄€數(shù)據(jù)庫或者單個表的數(shù)據(jù)備份到一個文件中。因此,通過mysqldump命令備份的數(shù)據(jù)可以用于數(shù)據(jù)庫還原操作。具體步驟如下:
1. 打開命令行窗口,輸入以下命令:
“`
mysqldump -u root -p databasename > filename.sql
“`
其中,root是MySQL的用戶名,databasename是要備份的數(shù)據(jù)庫名,filename.sql是備份文件名。
2. 數(shù)據(jù)庫備份成功后,通過以下命令進行數(shù)據(jù)庫還原:
“`
mysql -u root -p databasename
“`
其中,root是MySQL的用戶名,databasename是要還原的數(shù)據(jù)庫名,filename.sql是備份文件名。
二、使用phpMyAdmin
phpMyAdmin是一款Web應(yīng)用程序,能夠通過Web瀏覽器管理MySQL數(shù)據(jù)庫。phpMyAdmin能夠?qū)崿F(xiàn)對數(shù)據(jù)庫、表、字段等的管理,也能夠進行數(shù)據(jù)備份和還原。通過phpMyAdmin進行還原操作的步驟如下:
1. 打開phpMyAdmin網(wǎng)頁,選擇要還原的數(shù)據(jù)庫,在菜單欄中點擊“導(dǎo)入”。
2. 選擇要還原的備份文件,點擊“開始導(dǎo)入”。
3. 還原操作完成后,phpMyAdmin會提示還原是否成功。
三、使用MySQL Workbench
MySQL Workbench是MySQL官方推出的一款強大的數(shù)據(jù)庫管理工具,提供了數(shù)據(jù)建模、SQL開發(fā)、服務(wù)器管理等多種功能。使用MySQL Workbench進行數(shù)據(jù)庫還原的步驟如下:
1. 打開MySQL Workbench,連接到MySQL數(shù)據(jù)庫。
2. 在“管理”選項卡中,選擇數(shù)據(jù)庫,右鍵點擊鼠標(biāo),選擇“導(dǎo)入數(shù)據(jù)”,進入“導(dǎo)入向?qū)А薄?/p>
3. 選擇要還原的備份文件,設(shè)置數(shù)據(jù)還原選項,點擊“下一步”進行還原。
4. 還原完成后,MySQL Workbench會提示還原是否成功。
四、使用命令行工具
除了mysqldump命令外,MySQL還提供了命令行工具,能夠進行數(shù)據(jù)庫還原操作。具體步驟如下:
1. 打開命令行窗口,輸入以下命令,連接到MySQL數(shù)據(jù)庫:
“`
mysql -u root -p
“`
其中,root是MySQL的用戶名。
2. 進入到要還原的數(shù)據(jù)庫,輸入以下命令:
“`
use databasename;
“`
其中,databasename是要還原的數(shù)據(jù)庫名。
3. 執(zhí)行還原命令,輸入以下命令:
“`
source filename.sql;
“`
其中,filename.sql是備份文件名。
4. 數(shù)據(jù)庫還原完成后,退出MySQL命令行,輸入以下命令:
“`
exit;
“`
以上就是一些簡單易學(xué)的MySQL數(shù)據(jù)庫還原方法,各種方法各有優(yōu)缺點,根據(jù)實際情況選擇合適的還原方式。同時,在進行數(shù)據(jù)庫還原操作前,務(wù)必要備份好原有的數(shù)據(jù),以免發(fā)生意外情況導(dǎo)致數(shù)據(jù)丟失。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗豐富以策略為先導(dǎo)10多年以來專注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,響應(yīng)式網(wǎng)站制作,設(shè)計師量身打造品牌風(fēng)格,熱線:028-86922220mysql刪除數(shù)據(jù)庫后怎么恢復(fù)
使用mysql中的日志功能。
首先:將最近的二進制類型的日志文件存成文本文件導(dǎo)出,命令如下:
mysqlbinlog F:/wamp/mysql-bin.> F:/test.txt
其次:打開保存的txt文件,文件格式如下:
其中at 1099代表猜升雀第1099個命令(姑且這么穗早記),查詢這些命令,看看需要回復(fù)的命令在什么地方,然后執(zhí)行命令。
最后:執(zhí)行回復(fù)命令,如下:
mysqlbinlog –start-position=”123″ –stop-position=”531″ F:/wamp/笑碼mysql-bin.F:/test.txt | mysql -uroot -p
這樣就可以完成數(shù)據(jù)庫的回復(fù)了?。。?/p>
每個 DBA 是不是都有過刪庫的經(jīng)歷?刪庫了沒有備份怎么辦?備份恢復(fù)后無法啟動服務(wù)什么情況?表定義損壞數(shù)據(jù)無法讀取怎么辦?
我曾遇到某初創(chuàng)互聯(lián)網(wǎng)企業(yè),因維譽鉛睜護人員不規(guī)范的備份恢復(fù)操作,導(dǎo)致系統(tǒng)表空間文件被初始化,上萬張表無法讀取,花了數(shù)小慶歲時才搶救回來。
當(dāng)你發(fā)現(xiàn)數(shù)據(jù)無法讀取時,也許并非數(shù)據(jù)丟失了,可能是 DBMS 找不到描述數(shù)據(jù)的信息。
背景
先激昌來了解下幾張關(guān)鍵的 InnoDB 數(shù)據(jù)字典表,它們保存了部分表定義信息,在我們恢復(fù)表結(jié)構(gòu)時需要用到。
SYS_TABLES 描述 InnoDB 表信息CREATE TABLE `SYS_TABLES` (`NAME` varchar(255) NOT NULL DEFAULT ”, 表名`ID` bigint(20) unsigned NOT NULL DEFAULT ‘0’, 表id`N_COLS` int(10) DEFAULT NULL,`TYPE` int(10) unsigned DEFAULT NULL,`MIX_ID` bigint(20) unsigned DEFAULT NULL,`MIX_LEN` int(10) unsigned DEFAULT NULL,`CLUSTER_NAME` varchar(255) DEFAULT NULL,`SPACE` int(10) unsigned DEFAULT NULL, 表空間idPRIMARY KEY (`NAME`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SYS_INDEXES 描述 InnoDB 索引信息CREATE TABLE `SYS_INDEXES` ( `TABLE_ID` bigint(20) unsigned NOT NULL DEFAULT ‘0’, 與sys_tables的id對應(yīng) `ID` bigint(20) unsigned NOT NULL DEFAULT ‘0’, 索引id `NAME` varchar(120) DEFAULT NULL,索引名稱 `N_FIELDS` int(10) unsigned DEFAULT NULL, 索引包含字段的個數(shù) `TYPE` int(10) unsigned DEFAULT NULL, `SPACE` int(10) unsigned DEFAULT NULL, 存儲索引的表空間id `PAGE_NO` int(10) unsigned DEFAULT NULL, 索引的root page id PRIMARY KEY (`TABLE_ID`,`ID`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SYS_COLUMNS 描述 InnoDB 表的字段信息CREATE TABLE `SYS_COLUMNS` ( `TABLE_ID` bigint(20) unsigned NOT NULL, 與sys_tables的id對應(yīng) `POS` int(10) unsigned NOT NULL, 字段相對位置 `NAME` varchar(255) DEFAULT NULL, 字段名稱 `MTYPE` int(10) unsigned DEFAULT NULL, 字段編碼 `PRTYPE` int(10) unsigned DEFAULT NULL, 字段校驗類型 `LEN` int(10) unsigned DEFAULT NULL, 字段字節(jié)長度 `PREC` int(10) unsigned DEFAULT NULL, 字段精度 PRIMARY KEY (`TABLE_ID`,`POS`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SYS_FIELDS 描述全部索引的字段列CREATE TABLE `SYS_FIELDS` ( `INDEX_ID` bigint(20) unsigned NOT NULL, `POS` int(10) unsigned NOT NULL, `COL_NAME` varchar(255) DEFAULT NULL, PRIMARY KEY (`INDEX_ID`,`POS`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
./storage/innobase/include/dict0boot.h 文件定義了每個字典表的 index id,對應(yīng) id 的 page 中存儲著字典表的數(shù)據(jù)。
這里我們需要借助 undrop-for-innodb 工具恢復(fù)數(shù)據(jù),它能讀取表空間信息得到 page,將數(shù)據(jù)從 page 中提取出來。
# wget yum install -y gcc flex bison# make# make sys_parser
# ./sys_parser 讀取表結(jié)構(gòu)信息
sys_parser databases/table
stream_parser 讀取 InnoDB page 從 ibdata1 或 ibd 或分區(qū)表
# ./stream_parserYou must specify file with -f optionUsage: ./stream_parser -f Where: -hPrint this help -V or -g – Print debug information -s size – Amount of memory used for disk cache (allowed examples 1G 10M). Default 100M -Tretrieves only pages with index id = NM (N – high word, M – low word of id) -t size – Size of InnoDB tablespace to scan. Use it only if the parser can’t determine it by himself.
c_parser 從 innodb page 中讀取記錄保存到文件
# ./c_parserError: Usage: ./c_parser -4|-5|-6 -f -t table.sql Where -f — InnoDB page or directory with pages(all pages should have same index_id) -t — CREATE statement of a table -o — Save dump in this file. Otherwise print to stdout -l — Save SQL statements in this file. Otherwise print to stderr -h — Print this help -d — Process only those pages which potentially could have deleted records (default = NO) -D — Recover deleted rows only (default = NO) -U — Recover UNdeleted rows only (default = YES) -V — Verbose mode (lots of debug information)innodb_datafile is in REDUNDANT formatinnodb_datafile is in COMPACT formatinnodb_datafile is in MySQL 5.6 format -T — retrieves only pages with index id = NM (N – high word, M – low word of id) -b — Directory where external pages can be found. Usually it is pages-XXX/FIL_PAGE_TYPE_BLOB/ -i — Read external pages at their offsets from . -p prefix — Use prefix for a directory name in LOAD DATA INFILE command
接下來,我們演示場景的幾種數(shù)據(jù)恢復(fù)場景。
場景1:drop table
是否啟用了 innodb_file_per_table 其恢復(fù)方法有所差異,當(dāng)發(fā)生誤刪表時,應(yīng)盡快停止MySQL服務(wù),不要啟動。若 innodb_file_per_table=ON,更好只讀方式重新掛載文件系統(tǒng),防止其他進程寫入數(shù)據(jù)覆蓋之前塊設(shè)備的數(shù)據(jù)。
如果評估記錄是否被覆蓋,可以表中某些記錄的作為關(guān)鍵字看是否能從 ibdata1 中篩選出。
# grep WOODYHOFFMAN ibdata1
Binary file ibdata1 matches
也可以使用 bvi(適用于較小文件)或 hexdump -C(適用于較大文件)工具
以表 sakila.actor 為例CREATE TABLE `actor` (`actor_id` allint(5) unsigned NOT NULL AUTO_INCREMENT,`first_name` varchar(45) NOT NULL,`last_name` varchar(45) NOT NULL,`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`actor_id`),KEY `idx_actor_last_name` (`last_name`)) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
首先恢復(fù)表結(jié)構(gòu)信息1. 解析系統(tǒng)表空間獲取 page 信息
./stream_parser -f /var/lib/mysql/ibdata1
2. 新建一個 schema,把系統(tǒng)字典表的 DDL 導(dǎo)入
cat dictionary/SYS_* | mysql recovered
3. 創(chuàng)建恢復(fù)目錄
mkdir -p dumps/default
4. 解析系統(tǒng)表空間包含的字典表信息,
./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/01.page -t dictionary/SYS_TABLES.sql > dumps/default/SYS_TABLES 2> dumps/default/SYS_TABLES.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/02.page -t dictionary/SYS_COLUMNS.sql > dumps/default/SYS_COLUMNS 2> dumps/default/SYS_COLUMNS.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/03.page -t dictionary/SYS_INDEXES.sql > dumps/default/SYS_INDEXES 2> dumps/default/SYS_INDEXES.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/04.page -t dictionary/SYS_FIELDS.sql > dumps/default/SYS_FIELDS 2> dumps/default/SYS_FIELDS.sql
5. 導(dǎo)入恢復(fù)的數(shù)據(jù)字典
cat dumps/default/*.sql | mysql recovered
6. 讀取恢復(fù)后的表結(jié)構(gòu)信息
./sys_parser -pmsandbox -d recovered sakila/actor
由于 5.x 版本 innodb 引擎并非完整記錄表結(jié)構(gòu)信息,會丟失 AUTO_INCREMENT 屬性、二級索引和外鍵約束, DECIMAL 精度等信息。
若是 mysql 5.5 版本 frm 文件被從系統(tǒng)刪除,在原目錄下 touch 與原表名相同的 frm 文件,還能讀取表結(jié)構(gòu)信息和數(shù)據(jù)。若只有 frm 文件,想要獲得表結(jié)構(gòu)信息,可使用 mysqlfrm –diagnostic /path/to/xxx.frm,連接 mysql 會顯示字符集信息。
innodb_file_per_table=OFF
因為是共享表空間模式,數(shù)據(jù)頁都存儲在 ibdata1,可以從 ibdata1 文件中提取數(shù)據(jù)。
1. 獲取表的 table id,sys_table 存有表的 table id,sys_table 表 index id 是1,所以從01.page 獲取表 id./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/01.page -t dictionary/SYS_TABLES.sql | grep sakila/actorB28 2AD4D SYS_TABLES “sakila/actor” 0 “”B28 2AD4D SYS_TABLES “sakila/actor” 0 “” 0
2. 利用 table id 獲取表的主鍵 id,sys_indexes 存有表索引信息,innodb 索引組織表,找到主鍵 id 即找到數(shù)據(jù),sys_indexes 的 index id 是3,所以從03.page 獲取主鍵 id
./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/03.page -t dictionary/SYS_INDEXES.sql | grepBABCA SYS_INDEXES”PRIMARY”BAC3C SYS_INDEXES”idx_actor_last_name”BABCA SYS_INDEXES”PRIMARY”BAC3C SYS_INDEXES”idx_actor_last_name”
3. 知道了主鍵 id,就可以從對應(yīng) page 中提取表數(shù)據(jù),并生成 sql 文件。
./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/76.page -t sakila/actor.sql > dumps/default/actor 2> dumps/default/actor_load.sql
4. 最后導(dǎo)入恢復(fù)的數(shù)據(jù)
cat dumps/default/*.sql | mysql sakila
更多詳細情況點擊
關(guān)于mysql數(shù)據(jù)庫還原操作的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
創(chuàng)新互聯(lián)【028-86922220】值得信賴的成都網(wǎng)站建設(shè)公司。多年持續(xù)為眾多企業(yè)提供成都網(wǎng)站建設(shè),成都品牌建站設(shè)計,成都高端網(wǎng)站制作開發(fā),SEO優(yōu)化排名推廣服務(wù),全網(wǎng)營銷讓企業(yè)網(wǎng)站產(chǎn)生價值。
當(dāng)前名稱:MySQL數(shù)據(jù)庫還原操作:簡單易學(xué)的方法(mysql數(shù)據(jù)庫還原操作)
文章源于:http://m.5511xx.com/article/dppdjop.html


咨詢
建站咨詢
