MySQL和MongoDB的导入和导出方法

发布时间:2021-08-20 18:34:05 作者:chen
来源:亿速云 阅读:142

这篇文章主要讲解了“MySQLMongoDB的导入和导出方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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的导入和导出方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. mongodb导出与导入
  2. mongodb导出导入

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql mongodb

上一篇:如何提高mysql数据库的性能

下一篇:Hive中的锁的用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》