MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法-創(chuàng)新互聯(lián)

這篇文章主要講解了“MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法”吧!

從策劃到設(shè)計制作,每一步都追求做到細膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、虛擬空間、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進步。

1、MySQL導(dǎo)入和導(dǎo)出

(1)、mysqlimport

此工具位于mysql/bin目錄中,是MySQL的一個載入(或者說導(dǎo)入)數(shù)據(jù)的一個非常有效的工具。這是一個命令行工具。有兩個參數(shù)以及大量的選項可供選擇。這個工具把一個文該篇文章件(textfile)導(dǎo)入到你指定的數(shù)據(jù)庫和表中。比方說大家要從文件student.txt中把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫class中的表student中:

mysqlimportclass.studentstudent.txt

(2)、loaddatainfile

這個命令與mysqlimport非常相似,但這個方法可以在MySQL命令行中使用。如mysqlimport工具一樣,這個命令也有一些可以選擇的參數(shù)。比如您需要把自己的電腦上的數(shù)據(jù)導(dǎo)入到遠程的數(shù)據(jù)庫服務(wù)器中,您可以使用下面的命令:

Loaddatalocalinfile"d:\student.txt"intotablestudent;

上面的local參數(shù)表示文件是本地的文件,服務(wù)器是您所登陸的服務(wù)器。這樣就省去了使用ftp來上傳文件到服務(wù)器,mysql替你完成了。

(3)、mysqldump

mysqldump工具很多方面類似相反作用的工具mysqlimport。它們有一些同樣的選項。但mysqldump能夠做更多的事情。它可以把整個數(shù)據(jù)庫裝載到一個單獨的文該篇文章件中。這個文件包含有所有重建您的數(shù)據(jù)庫所需要的SQL命令。這個命令取得所有的模式并且將其轉(zhuǎn)換成DDL語法,取得所有的數(shù)據(jù),并且從這些數(shù)據(jù)中創(chuàng)建INSERT語句。這個工具將您的數(shù)據(jù)庫中所有的設(shè)計倒轉(zhuǎn)。因為所有的東西都被包含到了一個文該篇文章件中。這個文該篇文章件可以用一個簡單的批處理和一個合適SQL語句導(dǎo)回到MySQL中。這個工具令人難以置信地簡單而快速。決不會有半點讓人頭疼地地方。因此,如果您像裝載整個數(shù)據(jù)庫mydb的內(nèi)容到一個文件中,可以使用下面的命令:

bin/mysqldump–pmydb>mydb.txt

2、MongoDB導(dǎo)入和導(dǎo)出

(1)、mongoexport導(dǎo)出工具

MongoDB提供了mongoexport工具,可以把一個collection導(dǎo)出成json格式或csv格式的文件??梢灾付▽?dǎo)出哪些數(shù)據(jù)項,也可以根據(jù)給定的條件導(dǎo)出數(shù)據(jù)。工具幫助信息如下:

[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學(xué)習(xí)MongoDB之導(dǎo)入和導(dǎo)出

下面我們將以一個實際的例子說明,此工具的用法:

將foo庫中的表t1導(dǎo)出成json格式:

[chinastor.com-root@localhostbin]#./mongoexport-dfoo-ct1-o/data/t1.jsonconnectedto:127.0.0.1exported1records[chinastor.com-root@localhostbin]#

導(dǎo)出成功后我們看一下/data/t1.json文件的樣式,是否是我們所希望的:

[chinastor.com-root@localhostdata]#moret1.json{"_id":{"$oid":"4f927e2385b7a6814a0540a0"},"age":2}[chinastor.com-root@localhostdata]#

通過以上說明導(dǎo)出成功,但有一個問題,要是異構(gòu)數(shù)據(jù)庫的遷移怎么辦呢例如大家要將MongoDB的數(shù)據(jù)導(dǎo)入到MySQL該怎么辦呢MongoDB提供了一種csv的導(dǎo)出格式,就可以解決異構(gòu)數(shù)據(jù)庫遷移的問題了.下面將foo庫的t2表的age和name列導(dǎo)出,具體如下:

[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的導(dǎo)出結(jié)果:

[chinastor.com-root@localhostdata]#moret2.csvage,name1,"wwl"[chinastor.com-root@localhostdata]#

可以看出MongoDB為我們提供了一個強在的數(shù)據(jù)導(dǎo)出工具。

(2)、mongoimport導(dǎo)入工具

MongoDB提供了mongoimport工具,可以把一個特定格式文件中的內(nèi)容導(dǎo)入到某張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表數(shù)據(jù):

>db.t1.find();{"_id":ObjectId("4f937a56450beadc560feaa9"),"age":5}>

t1其中有一條age=5的記錄,我們再看一下json文件中的數(shù)據(jù)是什么樣子的:

[chinastor.com-root@localhostdata]#moret1.json{"_id":{"$oid":"4f937a56450beadc560feaa7"},"age":8}[chinastor.com-root@localhostdata]#

看到的是t1.json文件中有一條age=8的數(shù)據(jù),下面我們將用mongoimport工具將json文件中的記錄導(dǎo)入到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的導(dǎo)入和導(dǎo)出方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

當前題目:MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://bm7419.com/article8/ihpip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管面包屑導(dǎo)航、定制開發(fā)、搜索引擎優(yōu)化、網(wǎng)站制作云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營