备份前检查:

[root@Load29 tmp]# mongo localhost:
MongoDB shell version: 3.2.
connecting to: localhost:/test
Server has startup warnings:
--21T01::54.413+ I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
--21T01::54.413+ I CONTROL [initandlisten]
--21T01::54.414+ I CONTROL [initandlisten]
--21T01::54.414+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
--21T01::54.414+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--21T01::54.414+ I CONTROL [initandlisten]
> show dbs;
admin .000GB
local .000GB
test .005GB
>

整库备份:

[root@Load29 ~]# mongodump -h localhost:25019 -d test -o /tmp

-h MongDB服务器地址

-d 数据库实例名称

-o 导出数据存放目录

-drop 恢复数据时会先删除当前数据

删除old_dbname:

>use old_dbname
>db.dropDatabase()

整库还原:

[root@Load29 ~]# mongorestore -h localhost:25019 -d test -directoryperdb /tmp/test --drop
[root@Load29 ~]# mongorestore -h localhost: -u xxx-p xxxx--authenticationDatabase "admin" -d test --drop ./test 

数据库重命名:

>db.copyDatabase('old_dbname', 'new_dbname');
>use old_dbname
>db.dropDatabase();

导出具体某个字段:

[root@Load29 ~]# mongoexport -h localhost: -d test -c share_order -f shareOrderId -o /tmp/shareid.csv
--28T19::07.116+ connected to: localhost:
--28T19::07.605+ exported records

-c 指定表(即collection)

-f 指定column字段导出(即field)

导出某个集合:

 mongoexport -h 127.0.0.1: -u aijia -p aijia1234567 --authenticationDatabase "admin" -d test  -c app_product_comment -o comment.json

导入某个集合:

mongoimport --host localhost --port  --username aijia --password aijia1234567 --authenticationDatabase "admin" --collection app_product_comment --db test --file comment.json
05-08 08:45