MongoDB整库备份+整库导入
备份前检查:
[root@Load29 tmp]# mongo localhost:25019 MongoDB shell version: 3.2.10 connecting to: localhost:25019/test Server has startup warnings: 2017-07-21T01:27:54.413+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2017-07-21T01:27:54.413+0800 I CONTROL [initandlisten] 2017-07-21T01:27:54.414+0800 I CONTROL [initandlisten] 2017-07-21T01:27:54.414+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-07-21T01:27:54.414+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-07-21T01:27:54.414+0800 I CONTROL [initandlisten] > show dbs; admin 0.000GB local 0.000GB test 0.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:27017 -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:25019 -d test -c share_order -f shareOrderId -o /tmp/shareid.csv 2017-07-28T19:14:07.116+0800 connected to: localhost:25019 2017-07-28T19:14:07.605+0800 exported 49367 records
-c 指定表(即collection)
-f 指定column字段导出(即field)
导出某个集合:
mongoexport -h 127.0.0.1:25019 -u aijia -p aijia1234567 --authenticationDatabase "admin" -d test -c app_product_comment -o comment.json
导入某个集合:
mongoimport --host localhost --port 25019 --username aijia --password aijia1234567 --authenticationDatabase "admin" --collection app_product_comment --db test --file comment.json