您的位置:首页 > 数据库 > Mongodb

mongodb权限设置[整理]

2016-12-21 17:45 211 查看
官方文档

https://docs.mongodb.com/manual/tutorial/create-users/


一、掌握权限,理解下面4条

1、mongodb是没有默认管理员账号,所以要先添加管理员账号,在开启权限认证。

2、切换到admin数据库,添加的账号才是管理员账号。

3、用户只能在用户所在数据库登录,包括管理员账号。

4、管理员可以管理所有数据库,但是不能直接管理其他数据库,要先在admin数据库认证后才可以。这一点比较怪

二、说明

定义:
创建一个数据库新用户用db.createUser()方法,如果用户存在则返回一个用户重复错误。
语法:
db.createUser(user, writeConcern)
    user这个文档创建关于用户的身份认证和访问信息;

    writeConcern这个文档描述保证MongoDB提供写操作的成功报告。

· user文档,定义了用户的以下形式:
{ user: "<name>",
  pwd: "<cleartext password>",
  customData: { <any information> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ]
}

user文档字段介绍:

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

    pwd字段,用户的密码;

    cusomData字段,为任意内容,例如可以为用户全名介绍;

    roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色;

    在roles字段,可以指定内置角色和用户定义的角色。

    Built-In Roles(内置角色):
    1. 数据库用户角色:read、readWrite;

    2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;

    3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;

    4. 备份恢复角色:backup、restore;

    5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase

    6. 超级用户角色:root  
    // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
    7. 内部角色:__system
    PS:关于每个角色所拥有的操作权限可以点击上面的内置角色链接查看详情。

· writeConcern文档(官方说明
    w选项:允许的值分别是 1、0、大于1的值、"majority"、<tag set>;

    j选项:确保mongod实例写数据到磁盘上的journal(日志),这可以确保mongd以外关闭不会丢失数据。设置true启用。

    wtimeout:指定一个时间限制,以毫秒为单位。wtimeout只适用于w值大于1。

例如:在products数据库创建用户accountAdmin01,并给该用户admin数据库上clusterAdmin和readAnyDatabase的角色,products数据库上readWrite角色。
use products
db.createUser( { "user" : "accountAdmin01",
                 "pwd": "cleartext password",
                 "customData" : { employeeId: 12345 },
                 "roles" : [ { role: "clusterAdmin", db: "admin" },
                             { role: "readAnyDatabase", db: "admin" },
                             "readWrite"
                             ] },
               { w: "majority" , wtimeout: 5000 } )

验证:
mongo -u accountAdmin01 -p yourpassward --authenticationDatabase products

三、实例

1. 创建一个超级用户

?
use admin
db.createUser(
{
user: "adminUserName",
pwd: "userPassword",
roles:
[
{
roles: "userAdminAnyDatabase",
db: "admin"
}
]
}
)

超级用户的role有两种,userAdmin或者userAdminAnyDatabase(比前一种多加了对所有数据库的访问)。

db是指定数据库的名字,admin是管理数据库。

2. 用新创建的用户登录

mongo --host xxx -u adminUserName -p userPassword --authenticationDatabase admin

3. 查看当前用户的权限

db.runCommand(
{
usersInfo:"userName",
showPrivileges:true
}
)
4. 创建一般用户,也是用createUser

use db01
db.createUser(
{
user:"oneUser",
pwd:"12345",
roles:[
{role:"read",db:"db01"},
{role:"read",db:"db02"},
{role:"read",db:"db03"}
]
}
)

5. 创建一个不受访问限制的超级用户

use admin
db.createUser(
{
user:"superuser",
pwd:"pwd",
roles:["root"]
}
)

6. 修改密码

use admin
db.changeUserPassword("username", "xxx")

7. 查看用户信息

db.runCommand({usersInfo:"userName"})

8. 修改密码和用户信息

db.runCommand(
{
updateUser:"username",
pwd:"xxx",
customData:{title:"xxx"}
}
)


四、开启动用户权限认证

开户用户权限认证是在启动时加入 --auth参数即可,如:

mongod --dbpath 数据库路径 --auth

如果使用配置文件启动,则在配置文件中加入:auth=true,然后启动即可

mongod -f 配置文件路径

五、java客户端连接

public class MongoAuth {
public static void main(String[] args) throws Exception {
Mongo mongo = new Mongo("192.168.62.1", 27017);
DB db = mongo.getDB("test");
//数据库认证
db.authenticate("root", "root".toCharArray());
DBCollection users = db.getCollection("custome");
// 查询用户登录数据
DBObject queryUser = new BasicDBObject();
queryUser.put("name", "Joy");
DBCursor loginUser = users.find(queryUser);
System.out.println(loginUser);
}
}


六、官方相关

1

Start MongoDB without access control.

For example, the following starts a standalone mongod instance
without access control.

mongod --port 27017 --dbpath /data/db1


2

Connect to the instance.

For example, connect a mongo shell
to the instance.

mongo --port 27017


Specify additional command line options as appropriate to connect the mongo shell
to your deployment, such as --host.

3

Create the user administrator.

In the admin database, add a user with the userAdminAnyDatabase role.
For example, the following creates the user myUserAdmin in the admin database:

NOTE
The database where you create the user (in this example, admin) is the user’s authentication
database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)


Disconnect the mongo shell.

4

Re-start the MongoDB instance with access control.

Re-start the mongod instance
with the --auth command line option or, if using a configuration file, the security.authorization setting.

mongod --auth --port 27017 --dbpath /data/db1


Clients that connect to this instance must now authenticate themselves as a MongoDB user. Clients can only perform actions as determined by their assigned roles.

5

Connect and authenticate as the user administrator.

Using the mongo shell,
you can:
Connect with authentication by passing in user credentials, or
Connect first withouth authentication, and then issue the db.auth() method
to authenticate.

To authenticate during connection

Start a mongo shell
with the -u <username>, -p <password>,
and the --authenticationDatabase <database> command line options:

mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"


To authenticate after connecting

Connect the mongo shell
to the mongod:

mongo --port 27017


Switch to the authentication database (in this case, admin), and use db.auth(<username>,<pwd>) method
to authenticate:

use admin
db.auth("myUserAdmin", "abc123" )


6

Create additional users as needed for your deployment.

Once authenticated as the user administrator, use db.createUser() to
create additional users. You can assign any built-in roles or user-defined
roles to the users.
The myUserAdmin user only has privileges to manage
users and roles. As myUserAdmin, if you attempt to perform any other operations, such as read from a foo collection
in the test database, MongoDB returns an error.
The following operation adds a user myTester to the test database
who has readWrite role
in the test database as well as read role
in the reporting database.

NOTE
The database where you create the user (in this example, test) is that user’s authentication
database. Although the user would authenticate to this database, the user can have roles in other databases; i.e. the user’s authentication database does not limit the user’s privileges.

use test
db.createUser(
{
user: "myTester",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)


7

Connect and authenticate as myTester.

To authenticate during connection

Start a mongo shell
with the -u <username>, -p <password>,
and the --authenticationDatabase <database> command line options:

mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"


To authenticate after connecting

Connect the mongo shell
to the mongod:

mongo --port 27017


Switch to the authentication database (in this case, test), and use db.auth(<username>,<pwd>) method
to authenticate:

use test
db.auth("myTester", "xyz123" )


Insert into a collection as myTester.

As myTester, you have privileges to perform read and write operations in the test database
(as well as perform read operations in the reporting database). For example, you can peform the following insert operation in the test database:

db.foo.insert( { x: 1, y: 1 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: