博客
关于我
MongoDB创建用户
阅读量:794 次
发布时间:2023-02-09

本文共 2068 字,大约阅读时间需要 6 分钟。

创建超级管理员

创建admin超级管理员用户

指定用户的角色和数据库:
(注意此时添加的用户都只用于admin数据库,而非你存储业务数据的数据库)
(在cmd中敲多行代码时,直接敲回车换行,最后以分号首尾)

> use admin    switched to db admin    > db.createUser(    ... {    ... user:"admin",    ... pwd:"pwd",    ... roles:[{role:"userAdminAnyDatabase",db:"admin"}]    ... })    Successfully added user: {    	"user" : "admin",    	"roles" : [    		{    			"role" : "userAdminAnyDatabase",    			"db" : "admin"    		}    	]    }

user字段,为新用户的名字;

pwd字段,用户的密码;
cusomData字段,为任意内容,例如可以为用户全名介绍;
roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色。在roles字段,可以指定内置角色和用户定义的角色。
超级用户的role有两种,userAdmin或者userAdminAnyDatabase(比前一种多加了对所有数据库的访问,仅仅是访问而已)。
db是指定数据库的名字,admin是管理数据库。
不能用admin数据库中的用户登录其他数据库。注:只能查看当前数据库中的用户,哪怕当前数据库admin数据库,也只能查看admin数据库中创建的用户。

退出mongo shell
> exitbye
删除某个数据库
> show dbsadmin         0.078GBlocal         0.078GBtest    0.078GB> use testswitched to db test> db.dropDatabase(){ "dropped" : "test", "ok" : 1 }> show dbsadmin         0.078GBlocal         0.078GB
显示集合
> show collectionscountmoodtestsystem.indexes
查询collection中数据
> db.test.find(){ "_id" : ObjectId("5d3faf819c5201d5d90c60b2"), "name" : "test" }
移除collection中某个数据
> db.test.remove({})WriteResult({ "nRemoved" : 1 })> db.test.find()
删除某个collection
> db.test.drop()true> show collectionscountmoodsystem.indexes>
连接数据库
[test@user bin]$ ./mongo localhost:47017MongoDB shell version: 3.0.6connecting to: localhost:47017/testServer has startup warnings: 2019-07-30T10:02:36.997+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2019-07-30T10:02:36.997+0800 I CONTROL  [initandlisten] 2019-07-30T10:02:36.998+0800 I CONTROL  [initandlisten] 2019-07-30T10:02:36.998+0800 I CONTROL  [initandlisten] ** WARNING: You are running on a NUMA machine.2019-07-30T10:02:36.998+0800 I CONTROL  [initandlisten] **          We suggest launching mongod like this to avoid performance problems:2019-07-30T10:02:36.998+0800 I CONTROL  [initandlisten] **              numactl --interleave=all mongod [other options]2019-07-30T10:02:36.998+0800 I CONTROL  [initandlisten]

转载地址:http://vjffk.baihongyu.com/

你可能感兴趣的文章
MessageDigest
查看>>
Mes的理解
查看>>
MES系统如何实现远程访问?
查看>>
Metabase RCE漏洞复现(CVE-2023-38646)
查看>>
metaclass
查看>>
metaq杂记
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Framework中最全show命令及使用
查看>>
Metasploit GUI图形界面使用
查看>>
Metasploit SCADA渗透测试实战
查看>>
Metasploit SQL注入漏洞渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
Metasploit Web认证渗透测试实战
查看>>
Metasploit Windows AD渗透测试实战
查看>>
Metasploit 信息收集实战
查看>>
Metasploit 命令注入漏洞渗透测试实战
查看>>
Metasploit 客户端漏洞利用实战
查看>>
Metasploit 文件上传漏洞渗透测试实战
查看>>
Metasploit 文件包含与跨站请求伪造渗透测试实战
查看>>
Metasploit 渗透测试框架快速入门
查看>>