mongod默认启动不加任何参数时,是没有身份认证的,任何人都可以登录上进行任何操作
启动时添加--auth可以使用身份验证模式
使用mongod -f mongod.conf配置文件启动时,配置文件的security.authorization为enabled,也是使用身份认证模式
同时使用配置文件若想强制不使用身份验证则添加--noauth参数
若首次启动即添加身份认证,因为mongodb的身份只能使用客户端连接后
use admin
再在admin下使用
db.createUser(canshu)
创建对应用户
所以,可能会令一个用户也添加不了。
其实这是多虑的,mongodb添加了Localhost Exception
官方文档:
The localhost exception allows you to enable authorization before creating the first user in the system. When active, the localhost exception allows connections from the localhost interface to create the first user on theadmin database. The exception applies only when there are no users created in the MongoDB instance.
Changed in version 3.0: The localhost exception changed so that these connections only have access to create the first user on the admin database. In previous versions, connections that gained access using the localhost exception had unrestricted access to the MongoDB instance.
If you use the localhost exception when deploying a new MongoDB system, the first user you create must be in the admin database with privileges to create other users, such as a user with the userAdmin oruserAdminAnyDatabase role. See Enable Client Access Control and Create a User Administrator for more information.
即允许使用localhost(127.0.0.1)的客户端连接
在系统中没有用户时
第一次连接进来产生例外,允许创建一个用户
(类似于mysql安装时设置root的密码)
创建带权限用户步骤:
1、use admin;
只有在admin上才能创建高权限账户,即用于所有数据库管理权限的用户。
2、db.createUser({user:"root",pwd:"root",roles:[{role:"root",db:"admin"}]})
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
参数为一个js对象,参考上面。
常用的roles
root:即root权限,可以管理所有的数据库及用户,拥有所有角色的所有权限。只能创建在admin上
userAdminAnyDatabase:拥有所有数据库的用户管理权限
dbAdminAnyDatabase:拥有所有数据库的数据库管理权限
readWriteAnyDatabase:拥有所有数据库的读写权限
这些角色都只能创建在admin数据库上。
去掉AnyDatabase后可以创建在普通数据库上。
一旦执行过一次createUser,Localhost Exception立即消失,只能通过验证后才能进行创建操作
db.auth("root","root")。
3、修改一个用户
db.updateUser("admin",{roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
4、删除一个用户
db.dropUser("admin")
原文:http://www.cnblogs.com/guangshan/p/4846165.html