# mongo MongoDB shell version: 2.2.3 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user >
3.4
[root@netkiller ~]# mongo MongoDB shell version v3.4.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.1 Server has startup warnings: 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2017-01-03T11:26:57.516+0800 I CONTROL [initandlisten] >
# mongo 127.0.0.1:27017/admin --eval "db.stats()"
# mongodump -uneo -p -d test -o /tmp/ connected to: 127.0.0.1 Enter password: Tue Sep 8 10:12:33.011 DATABASE: test to /tmp/test Tue Sep 8 10:12:33.012 test.system.indexes to /tmp/test/system.indexes.bson Tue Sep 8 10:12:33.043 12 objects Tue Sep 8 10:12:33.043 test.bios to /tmp/test/bios.bson Tue Sep 8 10:12:33.043 1 objects Tue Sep 8 10:12:33.043 Metadata for test.bios to /tmp/test/bios.metadata.json Tue Sep 8 10:12:33.043 test.system.users to /tmp/test/system.users.bson Tue Sep 8 10:12:33.044 2 objects Tue Sep 8 10:12:33.044 Metadata for test.system.users to /tmp/test/system.users.metadata.json Tue Sep 8 10:12:33.044 test.fs.chunks to /tmp/test/fs.chunks.bson Tue Sep 8 10:12:33.045 2 objects Tue Sep 8 10:12:33.045 Metadata for test.fs.chunks to /tmp/test/fs.chunks.metadata.json Tue Sep 8 10:12:33.045 test.fs.files to /tmp/test/fs.files.bson Tue Sep 8 10:12:33.046 2 objects Tue Sep 8 10:12:33.046 Metadata for test.fs.files to /tmp/test/fs.files.metadata.json Tue Sep 8 10:12:33.046 test.images.chunks to /tmp/test/images.chunks.bson Tue Sep 8 10:12:33.167 12 objects Tue Sep 8 10:12:33.167 Metadata for test.images.chunks to /tmp/test/images.chunks.metadata.json Tue Sep 8 10:12:33.167 test.images.files to /tmp/test/images.files.bson Tue Sep 8 10:12:33.168 3 objects Tue Sep 8 10:12:33.168 Metadata for test.images.files to /tmp/test/images.files.metadata.json Tue Sep 8 10:12:33.168 test.img.chunks to /tmp/test/img.chunks.bson Tue Sep 8 10:12:33.175 4 objects Tue Sep 8 10:12:33.175 Metadata for test.img.chunks to /tmp/test/img.chunks.metadata.json Tue Sep 8 10:12:33.175 test.img.files to /tmp/test/img.files.bson Tue Sep 8 10:12:33.176 1 objects Tue Sep 8 10:12:33.176 Metadata for test.img.files to /tmp/test/img.files.metadata.json
短参数
mongodump -h mongodb.example.net -p 27017 -u neo -p password -d netkiller -c yourcollection
长参数
mongodump --host mongodb.example.net --port 27017 --username backup --password passwd --db mdb --collection some
直接从dump回复备份
[root@netkiller www]# ls backup dump [root@netkiller www]# mongorestore dump/
# mongorestore -h 127.0.0.1 -u neo -p chen /tmp/test/ connected to: 127.0.0.1 Tue Sep 8 10:18:31.360 /tmp/test/system.users.bson Tue Sep 8 10:18:31.360 going into namespace [test.system.users] Tue Sep 8 10:18:31.361 warning: Restoring to test.system.users without dropping. Restored data will be inserted without raising errors; check your server log 2 objects found Tue Sep 8 10:18:31.361 Creating index: { key: { _id: 1 }, ns: "test.system.users", name: "_id_" } Tue Sep 8 10:18:31.406 Creating index: { key: { user: 1, userSource: 1 }, unique: true, ns: "test.system.users", name: "user_1_userSource_1" } Tue Sep 8 10:18:31.406 /tmp/test/img.chunks.bson Tue Sep 8 10:18:31.406 going into namespace [test.img.chunks] Tue Sep 8 10:18:31.407 warning: Restoring to test.img.chunks without dropping. Restored data will be inserted without raising errors; check your server log 4 objects found Tue Sep 8 10:18:31.409 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.img.chunks" } Tue Sep 8 10:18:31.409 Creating index: { name: "files_id_1_n_1", key: { files_id: 1, n: 1 }, unique: true, ns: "test.img.chunks" } Tue Sep 8 10:18:31.409 /tmp/test/fs.files.bson Tue Sep 8 10:18:31.409 going into namespace [test.fs.files] Tue Sep 8 10:18:31.410 warning: Restoring to test.fs.files without dropping. Restored data will be inserted without raising errors; check your server log 2 objects found Tue Sep 8 10:18:31.410 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.fs.files" } Tue Sep 8 10:18:31.410 /tmp/test/images.chunks.bson Tue Sep 8 10:18:31.410 going into namespace [test.images.chunks] Tue Sep 8 10:18:31.411 warning: Restoring to test.images.chunks without dropping. Restored data will be inserted without raising errors; check your server log 12 objects found Tue Sep 8 10:18:31.414 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.images.chunks" } Tue Sep 8 10:18:31.414 Creating index: { name: "files_id_1_n_1", key: { files_id: 1, n: 1 }, unique: true, ns: "test.images.chunks" } Tue Sep 8 10:18:31.414 /tmp/test/images.files.bson Tue Sep 8 10:18:31.414 going into namespace [test.images.files] Tue Sep 8 10:18:31.414 warning: Restoring to test.images.files without dropping. Restored data will be inserted without raising errors; check your server log 3 objects found Tue Sep 8 10:18:31.415 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.images.files" } Tue Sep 8 10:18:31.415 /tmp/test/fs.chunks.bson Tue Sep 8 10:18:31.415 going into namespace [test.fs.chunks] Tue Sep 8 10:18:31.415 warning: Restoring to test.fs.chunks without dropping. Restored data will be inserted without raising errors; check your server log 2 objects found Tue Sep 8 10:18:31.416 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.fs.chunks" } Tue Sep 8 10:18:31.416 Creating index: { name: "files_id_1_n_1", key: { files_id: 1, n: 1 }, unique: true, ns: "test.fs.chunks" } Tue Sep 8 10:18:31.416 /tmp/test/img.files.bson Tue Sep 8 10:18:31.416 going into namespace [test.img.files] Tue Sep 8 10:18:31.417 warning: Restoring to test.img.files without dropping. Restored data will be inserted without raising errors; check your server log 1 objects found Tue Sep 8 10:18:31.417 Creating index: { name: "_id_", key: { _id: 1 }, ns: "test.img.files" } Tue Sep 8 10:18:31.417 /tmp/test/bios.bson Tue Sep 8 10:18:31.417 going into namespace [test.bios] Tue Sep 8 10:18:31.417 warning: Restoring to test.bios without dropping. Restored data will be inserted without raising errors; check your server log 1 objects found Tue Sep 8 10:18:31.417 Creating index: { key: { _id: 1 }, ns: "test.bios", name: "_id_" }
恢复到指定数据库
# mongorestore -h 127.0.0.1 -d test123 /tmp/test
mongorestore --host mongodb.example.net --port 27017 --username backup --password password --db test --collection some /data/backup
# mongostat connected to: 127.0.0.1 insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time *0 *0 *0 *0 0 1|0 0 848m 1.92g 162m 0 wechat:0.0% 0 0|0 0|0 62b 4k 1 10:38:53 *0 *0 *0 *0 0 1|0 0 848m 1.92g 162m 0 wechat:0.0% 0 0|0 0|0 62b 4k 1 10:38:54 *0 *0 *0 *0 0 1|0 0 848m 1.92g 162m 0 wechat:0.0% 0 0|0 0|0 62b 4k 1 10:38:55 *0 *0 *0 *0 0 1|0 0 848m 1.92g 162m 0 wechat:0.0% 0 0|0 0|0 62b 4k 1 10:38:56 *0 *0 *0 *0 0 1|0 0 848m 1.92g 162m 0 wechat:0.0% 0 0|0 0|0 62b 4k 1 10:38:57
# mongotop connected to: 127.0.0.1 ns total read write 2015-09-08T02:23:46 passport.system.users 0ms 0ms 0ms passport.system.namespaces 0ms 0ms 0ms passport.system.indexes 0ms 0ms 0ms member.system.users 0ms 0ms 0ms member.system.namespaces 0ms 0ms 0ms member.system.indexes 0ms 0ms 0ms
# mongofiles put /bin/ls connected to: 127.0.0.1 added file: { _id: ObjectId('55ee4c68bd053b7418404c53'), filename: "/bin/ls", chunkSize: 261120, uploadDate: new Date(1441680488106), md5: "ca226dd605e91b72e0d2060a6357c28f", length: 109208 } done! # mongofiles list connected to: 127.0.0.1 /etc/passwd 2176 /tmp/test1.php 192 /bin/ls 109208
上传指定数据库
# mongofiles put -d images -c img /etc/fstab connected to: 127.0.0.1 added file: { _id: ObjectId('55ee4d5416377f58d0a9e714'), filename: "/etc/fstab", chunkSize: 261120, uploadDate: new Date(1441680724579), md5: "381185dc0c4807b88406b452b4acc2e8", length: 1067 } done! # mongofiles list -d images -c img connected to: 127.0.0.1 /etc/fstab 1067
collection 参数有 bug 需要注意。 | |
---|---|
-c img 似乎无效,可能是mongofiles的bug. 使用PHP测试上传是可以指定 collection,并且没有任何问题。 # mongofiles put -d images --collection abc /etc/nfsmount.conf connected to: 127.0.0.1 added file: { _id: ObjectId('55ee4f5ef4b26bc3189dc8a5'), filename: "/etc/nfsmount.conf", chunkSize: 261120, uploadDate: new Date(1441681246083), md5: "ce3b9fee8612087cbb69d46db34ce8ec", length: 3605 } done! # mongofiles -d images --collection abc list connected to: 127.0.0.1 /etc/fstab 1067 /etc/passwd 2555 /etc/goaccess.conf 6956 /etc/krb5.conf 449 /etc/nfsmount.conf 3605 # mongo images > show collections; abc.fs.chunks abc.fs.files fs.chunks fs.files system.indexes > > db.abc.fs.files.find(); > 使用 --collection 参数可以看到abc已经创建,但我们去db.abc.fs.files.find();发现里面没有任何数据,文件仍然被上传到 abc.fs.files |
如果 /tmp/test123 存在则会覆盖
# mongofiles get /tmp/test123 connected to: 127.0.0.1 done write to: /tmp/test123
-l 指定路径,相当于另存。
# mongofiles get /tmp/test123 -l /tmp/aabbcc connected to: 127.0.0.1 done write to: /tmp/aabbcc
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。