关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » MySQL »教你如何利用MySQL学习MongoDB之导入和导出

    教你如何利用MySQL学习MongoDB之导入和导出

    2011-05-24 09:51:00 出处:ITJS
    分享

    前一篇文章里,,我们了解了教你如何利用MySQL学习MongoDB之授权和权限,该篇文章中我们继续我们的学习之旅,学习两者的导入和导出。

    1、MySQL导入和导出

    (1)、mysqlimport

    此工具位于mysql/bin目录中,是MySQL的一个载入(或者说导入)数据的一个非常有效的工具。这是一个命令行工具。有两个参数以及大量的选项可供选择。这个工具把一个文该篇文章件(text file)导入到你指定的数据库和表中。比方说大家要从文件student.txt中把数据导入到数据库class中的表 student中:

    mysqlimport class.student student.txt

    (2)、load data infile

    这个命令与mysqlimport非常相似,但这个方法可以在MySQL命令行中使用。 如mysqlimport工具一样,这个命令也有一些可以选择的参数。比如您需要把自己的电脑上的数据导入到远程的数据库服务器中,您可以使用下面的命令:

    Load data local infile "d:student.txt" into table student;

    上面的local参数表示文件是本地的文件,服务器是您所登陆的服务器。这样就省去了使用ftp来上传文件到服务器,mysql替你完成了。

    (3)、mysqldump

    mysqldump工具很多方面类似相反作用的工具mysqlimport。它们有一些同样的选项。但mysqldump能够做更多的事情。它可以把整个数据库装载到一个单独的文该篇文章件中。这个文件包含有所有重建您的数据库所需要的SQL命令。这个命令取得所有的模式并且将其转换成DDL语法,取得所有的数据,并且从这些数据中创建INSERT语句。这个工具将您的数据库中所有的设计倒转。因为所有的东西都被包含到了一个文该篇文章件中。这个文该篇文章件可以用一个简单的批处理和一个合适SQL语句导回到MySQL中。这个工具令人难以置信地简单而快速。决不会有半点让人头疼地地方。因此,如果您像装载整个数据库mydb的内容到一个文件中,可以使用下面的命令:

    bin/mysqldump –p mydb > mydb.txt

    2、MongoDB导入和导出

    (1)、mongoexport导出工具

    MongoDB提供了mongoexport工具,可以把一个collection导出成json格式或csv格式的文件。可以指定导出哪些数据项,也可以根据给定的条件导出数据。工具帮助信息如下:

    [chinastor.com-root@localhost bin]# ./mongoexport --help  options:  --help produce help message   -v [ --verbose ] be more verbose (include multiple times for more   verbosity e.g. -vvvvv)   -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   --port arg server port. Can also use --host hostname:port   --ipv6 enable IPv6 support (disabled by default)   -u [ --username ] arg username   -p [ --password ] arg password   --dbpath arg directly access mongod database files in the given   path, instead of connecting to a mongod server -   needs to lock the data directory, so cannot be used   if a mongod is currently accessing the same path   --directoryperdb if dbpath specified, each db is in a separate   directory   -d [ --db ] arg database to use   -c [ --collection ] arg collection to use (some commands)   -f [ --fields ] arg comma separated list of field names e.g. -f name,age   --fieldFile arg file with fields names - 1 per line   -q [ --query ] arg query filter, as a JSON string   --csv export to csv instead of json   -o [ --out ] arg output file; if not specified, stdout is used   --jsonArray output to a json array rather than one object per   line   [chinastor.com-root@localhost bin]#  

    下面我们将以一个实际的例子说明,此工具的用法:

    将foo库中的表t1导出成json格式:

    [chinastor.com-root@localhost bin]# ./mongoexport -d foo -c t1 -o /data/t1.json   connected to: 127.0.0.1   exported 1 records   [chinastor.com-root@localhost bin]#  

    导出成功后我们看一下/data/t1.json文件的样式,是否是我们所希望的:

    [chinastor.com-root@localhost data]# more t1.json   { "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 }   [chinastor.com-root@localhost data]#  

    通过以上说明导出成功,但有一个问题,要是异构数据库的迁移怎么办呢 例如大家要将MongoDB的数据导入到MySQL该怎么办呢 MongoDB提供了一种csv的导出格式,就可以解决异构数据库迁移的问题了. 下面将foo库的t2表的age和name列导出, 具体如下:

    [chinastor.com-root@localhost bin]# ./mongoexport -d foo -c t2 --csv -f age,name -o /data/t2.csv   connected to: 127.0.0.1   exported 1 records   [chinastor.com-root@localhost bin]#  

    查看/data/t2.csv的导出结果:

    [chinastor.com-root@localhost data]# more t2.csv   age,name   1,"wwl"   [chinastor.com-root@localhost data]#  

    可以看出MongoDB为我们提供了一个强在的数据导出工具。

    (2)、mongoimport导入工具

    MongoDB提供了mongoimport工具,可以把一个特定格式文件中的内容导入到某张collection中。工具帮助信息如下:

    [chinastor.com-root@localhost bin]# ./mongoimport --help   options:   --help produce help message   -v [ --verbose ] be more verbose (include multiple times for more   verbosity e.g. -vvvvv)   -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   --port arg server port. Can also use --host hostname:port   --ipv6 enable IPv6 support (disabled by default)   -u [ --username ] arg username   -p [ --password ] arg password   --dbpath arg directly access mongod database files in the given   path, instead of connecting to a mongod server -   needs to lock the data directory, so cannot be used   if a mongod is currently accessing the same path   --directoryperdb if dbpath specified, each db is in a separate   directory   -d [ --db ] arg database to use   -c [ --collection ] arg collection to use (some commands)   -f [ --fields ] arg comma separated list of field names e.g. -f name,age   --fieldFile arg file with fields names - 1 per line   --ignoreBlanks if given, empty fields in csv and tsv will be ignored   --type arg type of file to import. default: json (json,csv,tsv)   --file arg file to import from; if not specified stdin is used   --drop drop collection first   --headerline CSV,TSV only - use first line as headers   --upsert insert or update objects that already exist   --upsertFields arg comma-separated fields for the query part of the   upsert. You should make sure this is indexed   --stopOnError stop importing at first error rather than continuing   --jsonArray load a json array, not one item per line. Currently   limited to 4MB.  

    下面我们将以一人实际的例子说明,此工具的用法:

    先看一下foo库中的t1表数据:

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

    t1其中有一条age=5的记录, 我们再看一下json文件中的数据是什么样子的:

    [chinastor.com-root@localhost data]# more t1.json   { "_id" : { "$oid" : "4f937a56450beadc560feaa7" }, "age" : 8 }   [chinastor.com-root@localhost data]#  

    看到的是t1.json文件中有一条age=8的数据,下面我们将用mongoimport工具将json文件中的记录导入到t1表中:

    [chinastor.com-root@localhost bin]# ./mongoimport -d foo -c t1 /data/t1.json   connected to: 127.0.0.1   imported 1 objects  

    工具返回信息说明向表中插入了一条记录. 我们进库里实际验证一下:

    [chinastor.com-root@localhost bin]# ./mongo   MongoDB shell version: 1.8.1   connecting to: test   > use foo   switched to db foo   > db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   { "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   >  

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    hiberfil.sys文件可以删除吗?了解该文件并手把手教你删除C盘的hiberfil.sys文件

    Window 10和 Windows 11哪个好?答案是:看你自己的需求

    盗版软件成公司里的“隐形炸弹”?老板们的“法务噩梦” 有救了!

    帝国CMS7.5编辑器上传图片取消宽高的三种方法

    帝国cms如何自动生成缩略图的实现方法

    Windows 12即将到来,将彻底改变人机交互

    帝国CMS 7.5忘记登陆账号密码怎么办?可以phpmyadmin中重置管理员密码

    帝国CMS 7.5 后台编辑器换行,修改回车键br换行为p标签

    Windows 11 版本与 Windows 10比较,新功能一览

    Windows 11激活产品密钥收集及专业版激活方法

    IT头条

    智能手机市场风云:iPhone领跑销量榜,华为缺席引争议

    15:43

    大数据算法和“老师傅”经验叠加 智慧化收储粮食尽显“科技范”

    15:17

    严重缩水!NVIDIA将推中国特供RTX 5090 DD:只剩24GB显存

    00:17

    无线路由大厂 TP-Link突然大裁员:补偿N+3

    02:39

    Meta 千万美金招募AI高级人才

    00:22

    技术热点

    windows 7应用程序无法启动出现窗口提示找不到应用程序

    SQL中数据类型转换函数的使用

    MySQL使用变量的注意事项

    SQL Server 锁自定义的示例演示

    如何在Linux命令行中创建以及展示演示稿

    windows 7任务栏显示标题的方法(windows 7任务栏缩略图不显示出

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。