33.2. 命令工具

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介:

33.2.1. mongo - MongoDB Shell

33.2.1.1. eval

			
# 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()"		
			

33.2.1.2. help

help

db.help()                    help on DB methods
db.foo.help()                help on collection methods
			

33.2.1.3. 登陆认证

			
# mongo -u<user> -p<password> --authenticationDatabase <db> <host>/<db> 
						
			

33.2.2. mongodump - Backup

# 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
		

33.2.2.1. 远程备份

短参数

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			
			

33.2.2.2. 本地备份

通过指定dbpath在本地导出bson文件

mongodump --dbpath /var/lib/mongodb --out /opt/backup --db test --username backup --password passwd			
			

33.2.3. mongorestore

直接从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
		

33.2.3.1. 远程回复

mongorestore --host mongodb.example.net --port 27017 --username backup --password password --db test --collection some /data/backup
			

33.2.3.2. 本地恢复

mongorestore --dbpath /var/lib/mongodb --journal /opt/backup
			

33.2.3.3. filter

如果只想恢复部分数据,可以使用--filter

$ mongorestore -h 127.0.0.1 -d test123 /tmp/test --filter '{"field": 1}'			
			

33.2.4. mongostat

# 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
		

33.2.5. mongotop

# 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
		

33.2.6. mongofiles - Browse and modify a GridFS filesystem.

33.2.6.1. list 浏览文件

# mongofiles list
connected to: 127.0.0.1
/etc/passwd	2176
/tmp/test1.php	192
			
			

33.2.6.2. put 上传文件

# 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

33.2.6.3. get 下载

如果 /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
			

33.2.6.4. delete 删除

# mongofiles list 
connected to: 127.0.0.1
/etc/passwd	2176
/tmp/test1.php	192
/bin/ls	109208
/tmp/test123	6

# mongofiles delete /tmp/test123
connected to: 127.0.0.1
done!

# mongofiles list 
connected to: 127.0.0.1
/etc/passwd	2176
/tmp/test1.php	192
/bin/ls	109208
	





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
5月前
|
安全 Java Shell
一篇文章讲明白LinuxShell远程执行命令(命令行与脚本方式)
一篇文章讲明白LinuxShell远程执行命令(命令行与脚本方式)
458 0
|
5月前
|
Linux
探索 Linux 中的 `bzmore` 命令:一个方便的文本查看工具
探索 Linux 中的 `bzmore` 命令:一个方便的文本查看工具
|
6月前
|
网络协议 Unix Shell
第十一章 Shell常用命令与工具(二)
第十一章 Shell常用命令与工具(二)
|
6月前
|
移动开发 Shell Linux
第十一章 Shell常用命令与工具(一)
第十一章 Shell常用命令与工具(一)
|
6月前
|
Unix Shell Linux
【Shell 命令集合 文档编辑】Linux 文本编辑器 ex命令使用指南
【Shell 命令集合 文档编辑】Linux 文本编辑器 ex命令使用指南
96 0
|
前端开发 JavaScript Python
用命令行运行脚本并生成报告
用命令行运行脚本并生成报告
181 0
|
Linux 开发工具
|
Rust JavaScript Java
命令行工具开发:如何快速实现命令行提示?
对于稍微复杂一些的命令行工具,命令行的提示功能必不可少。那么对于不同语言的开发者,有没有一种简单快捷的实现方式呢?本文分享一种快速实现的方法,使用YAML文件定义命令行工具的使用规范,再通过工具自动生成各种shell的命令行提示脚本,最后分享一些至关重要的命令行解析器。
命令行工具开发:如何快速实现命令行提示?
|
运维 Shell Linux
shell 处理文档信息导入数据库-自动化运维linux
对文档的信息进行分段处理
312 0
下一篇
无影云桌面