共計 4976 個字符,預計需要花費 13 分鐘才能閱讀完成。
這篇文章主要講解了“MySQL 和 MongoDB 的導入和導出方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“MySQL 和 MongoDB 的導入和導出方法”吧!
1、MySQL 導入和導出
(1)、mysqlimport
此工具位于 mysql/bin 目錄中,是 MySQL 的一個載入 (或者說導入) 數據的一個非常有效的工具。這是一個命令行工具。有兩個參數以及大量的選項可供選擇。這個工具把一個文該篇文章件 (textfile) 導入到你指定的數據庫和表中。比方說大家要從文件 student.txt 中把數據導入到數據庫 class 中的表 student 中:
mysqlimportclass.studentstudent.txt
(2)、loaddatainfile
這個命令與 mysqlimport 非常相似,但這個方法可以在 MySQL 命令行中使用。如 mysqlimport 工具一樣,這個命令也有一些可以選擇的參數。比如您需要把自己的電腦上的數據導入到遠程的數據庫服務器中,您可以使用下面的命令:
Loaddatalocalinfile d:\student.txt intotablestudent;
上面的 local 參數表示文件是本地的文件,服務器是您所登陸的服務器。這樣就省去了使用 ftp 來上傳文件到服務器,mysql 替你完成了。
(3)、mysqldump
mysqldump 工具很多方面類似相反作用的工具 mysqlimport。它們有一些同樣的選項。但 mysqldump 能夠做更多的事情。它可以把整個數據庫裝載到一個單獨的文該篇文章件中。這個文件包含有所有重建您的數據庫所需要的 SQL 命令。這個命令取得所有的模式并且將其轉換成 DDL 語法,取得所有的數據,并且從這些數據中創建 INSERT 語句。這個工具將您的數據庫中所有的設計倒轉。因為所有的東西都被包含到了一個文該篇文章件中。這個文該篇文章件可以用一個簡單的批處理和一個合適 SQL 語句導回到 MySQL 中。這個工具令人難以置信地簡單而快速。決不會有半點讓人頭疼地地方。因此,如果您像裝載整個數據庫 mydb 的內容到一個文件中,可以使用下面的命令:
bin/mysqldump–pmydb mydb.txt
2、MongoDB 導入和導出
(1)、mongoexport 導出工具
MongoDB 提供了 mongoexport 工具,可以把一個 collection 導出成 json 格式或 csv 格式的文件。可以指定導出哪些數據項,也可以根據給定的條件導出數據。工具幫助信息如下:
[chinastor.com-root@localhostbin]#./mongoexport–helpoptions:–helpproducehelpmessage-v[–verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[–host]argmongohosttoconnectto(/s1,s2forsets)–portargserverport.Canalsouse–hosthostname:port–ipv6enableIPv6support(disabledbydefault)-u[–username]argusername-p[–password]argpassword–dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath–directoryperdbifdbpathspecified,eachdbisinaseparatedirectory-d[–db]argdatabasetouse-c[–collection]argcollectiontouse(somecommands)-f[–fields]argcommaseparatedlistoffieldnamese.g.-fname,age–fieldFileargfilewithfieldsnames-1perline-q[–query]argqueryfilter,asaJSONstring–csvexporttocsvinsteadofjson-o[–out]argoutputfile;ifnotspecified,stdoutisused–jsonArrayoutputtoajsonarrayratherthanoneobjectperline[chinastor.com-root@localhostbin]#
如何利用 MySQL 學習 MongoDB 之導入和導出
下面我們將以一個實際的例子說明,此工具的用法:
將 foo 庫中的表 t1 導出成 json 格式:
[chinastor.com-root@localhostbin]#./mongoexport-dfoo-ct1-o/data/t1.jsonconnectedto:127.0.0.1exported1records[chinastor.com-root@localhostbin]#
導出成功后我們看一下 /data/t1.json 文件的樣式,是否是我們所希望的:
[chinastor.com-root@localhostdata]#moret1.json{_id :{ $oid : 4f927e2385b7a6814a0540a0}, age :2}[chinastor.com-root@localhostdata]#
通過以上說明導出成功,但有一個問題,要是異構數據庫的遷移怎么辦呢例如大家要將 MongoDB 的數據導入到 MySQL 該怎么辦呢 MongoDB 提供了一種 csv 的導出格式,就可以解決異構數據庫遷移的問題了. 下面將 foo 庫的 t2 表的 age 和 name 列導出, 具體如下:
[chinastor.com-root@localhostbin]#./mongoexport-dfoo-ct2–csv-fage,name-o/data/t2.csvconnectedto:127.0.0.1exported1records[chinastor.com-root@localhostbin]#
查看 /data/t2.csv 的導出結果:
[chinastor.com-root@localhostdata]#moret2.csvage,name1, wwl [chinastor.com-root@localhostdata]#
可以看出 MongoDB 為我們提供了一個強在的數據導出工具。
(2)、mongoimport 導入工具
MongoDB 提供了 mongoimport 工具,可以把一個特定格式文件中的內容導入到某張 collection 中。工具幫助信息如下:
[chinastor.com-root@localhostbin]#./mongoimport–helpoptions:–helpproducehelpmessage-v[–verbose]bemoreverbose(includemultipletimesformoreverbositye.g.-vvvvv)-h[–host]argmongohosttoconnectto(/s1,s2forsets)–portargserverport.Canalsouse–hosthostname:port–ipv6enableIPv6support(disabledbydefault)-u[–username]argusername-p[–password]argpassword–dbpathargdirectlyaccessmongoddatabasefilesinthegivenpath,insteadofconnectingtoamongodserver-needstolockthedatadirectory,socannotbeusedifamongodiscurrentlyaccessingthesamepath–directoryperdbifdbpathspecified,eachdbisinaseparatedirectory-d[–db]argdatabasetouse-c[–collection]argcollectiontouse(somecommands)-f[–fields]argcommaseparatedlistoffieldnamese.g.-fname,age–fieldFileargfilewithfieldsnames-1perline–ignoreBlanksifgiven,emptyfieldsincsvandtsvwillbeignored–typeargtypeoffiletoimport.default:json(json,csv,tsv)–fileargfiletoimportfrom;ifnotspecifiedstdinisused–dropdropcollectionfirst–headerlineCSV,TSVonly-usefirstlineasheaders–upsertinsertorupdateobjectsthatalreadyexist–upsertFieldsargcomma-separatedfieldsforthequerypartoftheupsert.Youshouldmakesurethisisindexed–stopOnErrorstopimportingatfirsterrorratherthancontinuing–jsonArrayloadajsonarray,notoneitemperline.Currentlylimitedto4MB.
下面我們將以一人實際的例子說明,此工具的用法:
先看一下 foo 庫中的 t1 表數據:
db.t1.find();{ _id :ObjectId( 4f937a56450beadc560feaa9), age :5}
t1 其中有一條 age= 5 的記錄, 我們再看一下 json 文件中的數據是什么樣子的:
[chinastor.com-root@localhostdata]#moret1.json{_id :{ $oid : 4f937a56450beadc560feaa7}, age :8}[chinastor.com-root@localhostdata]#
看到的是 t1.json 文件中有一條 age= 8 的數據,下面我們將用 mongoimport 工具將 json 文件中的記錄導入到 t1 表中:
[chinastor.com-root@localhostbin]#./mongoimport-dfoo-ct1/data/t1.jsonconnectedto:127.0.0.1imported1objects
工具返回信息說明向表中插入了一條記錄. 我們進庫里實際驗證一下:
[chinastor.com-root@localhostbin]#./mongoMongoDBshellversion:1.8.1connectingto:test usefooswitchedtodbfoo db.t1.find();{ _id :ObjectId( 4f937a56450beadc560feaa9), age :5}{_id :ObjectId( 4f937a56450beadc560feaa7), age :8}
感謝各位的閱讀,以上就是“MySQL 和 MongoDB 的導入和導出方法”的內容了,經過本文的學習后,相信大家對 MySQL 和 MongoDB 的導入和導出方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!