码迷,mamicode.com
首页 > 数据库 > 详细

mongodb 备份、还原、导入、导出

时间:2017-08-23 14:53:53      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:user   tput   类型   server   cannot   led   理解   erb   支持   


mongodump备份数据库

常用的备份命令格式

mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径

如果想导出所有数据库,可以去掉-d

 

mongodump 语法:

[root@hcdb0 ~]# mongodump --help  
Export MongoDB data to BSON files.

options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --version                             print the program‘s version and exit
  -h [ --host ] arg                     mongo host to connect to ( <set
                                        name>/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
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --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                      each db is in a separate directly
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -o [ --out ] arg (=dump)              output directory or "-" for stdout
  -q [ --query ] arg                    json query
  --oplog                               Use oplog for point-in-time
                                        snapshotting
  --repair                              try to recover a crashed database
  --forceTableScan                      force a table scan (do not use
                                        $snapshot)



导出数据库
[root@local ~]# mongodump -h 127.0.0.1 --port 30216 -d test -uxxxx -pxxxxx -o home/mongodb/
connected to: 10.10.3.245:30216
Thu Aug 11 02:15:04.529 DATABASE: test to /home/mongodb/test


mongorestore还原数据库

常用命令格式

mongorestore -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 --drop 文件存在路径

[root@localhost mongodb]# mongorestore -d test /home/mongodb/test #test这个数据库的备份路径
这二个命令,可以实现数据库的备份与还原,文件格式是json和bson的


mongoexport导出表,或者表中部分字段

常用命令格式

mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段

    -q 条件导出 --csv -o 文件名 上面的参数好理解,重点说一下:
    -f 导出指字段,以字号分割,-f name,email,age导出name,email,age这三个字段
    -q 可以根查询条件导出,-q ‘{ "_id" : "10001" }‘ 导出uid为100的数据
    --csv 表示导出的文件格式为csv的,这个比较有用,因为大部分的关系型数据库都是支持csv,在这里有共同点

导出整张表

[root@localhost mongodb]# mongoexport -d test -c users -o /home/mongodb/test/users.dat
connected to: 127.0.0.1
exported 24 records

导出表中部分字段

[root@localhost mongodb]# mongoexport -d test -c users --csv -f uid,name,sex -o test/users.csv
connected to: 127.0.0.1
exported 24 records

根据条件敢出数据

[root@localhost mongodb]# mongoexport -d test -c users -q ‘{uid:{$gt:1}}‘ -o test/users.json
connected to: 127.0.0.1
exported 12 records


mongoimport导入表,或者表中部分字段

还原整表导出的非csv文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsert --drop 文件名

重点说一下--upsert,其他参数上面的命令已有提到,--upsert 插入或者更新现有数据

 

还原部分字段的导出文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsertFields 字段 --drop 文件名  

--upsertFields根--upsert一样

还原导出的csv文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --type 类型 --headerline --upsert --drop 文件名

上面三种情况,还可以有其他排列组合的。

还原导出的表数据

[root@localhost mongodb]# mongoimport -d test -c users --upsert test/users.dat
connected to: 127.0.0.1
............

部分字段的表数据导入

[root@localhost mongodb]# mongoimport -d test -c users  --upsertFields uid,name,sex  test/users.dat  
connected to: 127.0.0.1  
...............................................

还原csv文件

[root@localhost mongodb]# mongoimport -d test -c users --type csv --headerline --file test/users.csv
connected to: 127.0.0.1
...........................................


mongodb 备份、还原、导入、导出

标签:user   tput   类型   server   cannot   led   理解   erb   支持   

原文地址:http://www.cnblogs.com/liang545621/p/7417777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!