MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法

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

創(chuàng)新互聯(lián)建站長(zhǎng)期為上千家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為婺源企業(yè)提供專(zhuān)業(yè)的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作,婺源網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。

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

(1)、mysqlimport

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

mysqlimportclass.studentstudent.txt

(2)、loaddatainfile

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

Loaddatalocalinfile"d:\student.txt"intotablestudent;

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

(3)、mysqldump

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

bin/mysqldump–pmydb>mydb.txt

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

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

MongoDB提供了mongoexport工具,可以把一個(gè)collection導(dǎo)出成json格式或csv格式的文件。可以指定導(dǎo)出哪些數(shù)據(jù)項(xiàng),也可以根據(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)出

下面我們將以一個(gè)實(shí)際的例子說(shuō)明,此工具的用法:

將foo庫(kù)中的表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]#

通過(guò)以上說(shuō)明導(dǎo)出成功,但有一個(gè)問(wèn)題,要是異構(gòu)數(shù)據(jù)庫(kù)的遷移怎么辦呢例如大家要將MongoDB的數(shù)據(jù)導(dǎo)入到MySQL該怎么辦呢MongoDB提供了一種csv的導(dǎo)出格式,就可以解決異構(gòu)數(shù)據(jù)庫(kù)遷移的問(wèn)題了.下面將foo庫(kù)的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為我們提供了一個(gè)強(qiáng)在的數(shù)據(jù)導(dǎo)出工具。

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

MongoDB提供了mongoimport工具,可以把一個(gè)特定格式文件中的內(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.

下面我們將以一人實(shí)際的例子說(shuō)明,此工具的用法:

先看一下foo庫(kù)中的t1表數(shù)據(jù):

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

t1其中有一條age=5的記錄,我們?cè)倏匆幌耲son文件中的數(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

工具返回信息說(shuō)明向表中插入了一條記錄.我們進(jìn)庫(kù)里實(shí)際驗(yàn)證一下:

[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)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

當(dāng)前文章:MySQL和MongoDB的導(dǎo)入和導(dǎo)出方法
分享URL:http://muchs.cn/article20/pdgdjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、云服務(wù)器、搜索引擎優(yōu)化、網(wǎng)站制作、關(guān)鍵詞優(yōu)化、Google

廣告

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

成都app開(kāi)發(fā)公司