kuyu 发表于 2014-10-14 13:40:46

mongodb的用户管理

简介:由于mongodb数据的用户管理是基于单个库的管理,他的管理策略大概如下    如果验证了admin库的账户,那么所有的库都可以访问
    如果验证了非admin库的账户,那么此权限只能访问当前库下的数据
步骤建议:    如果要对数据库进行账户设置,最好我们首先不要开启数据库验证,然后进入admin库,创建密码退出添加 -auth验证重启mongodb然后使用admin库的账户进行验证,如果通过那么进入其它库进行账户创建,完成后重新登录进行验证验证如下:    启动mongod
      mongod.exe --dbpath=E:\mongodb\db -auth-auth开启用户验证,如果启动没有添加此参数那么用户验证将失败    首先进入admin库
      use admin
    查看当前数据的用户
      show collections 能返回两个表    添加admin库的sa账户密码为sa
      db.addUser('sa','sa')
    ctrl+c 退出当前登录
    重新登录并进入admin库和test库检查是否能够查询当前表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
> use admin
switched to db admin
> show collections
Mon Oct 13 17:11:01 uncaught exception: error: {
      "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
      "code" : 10057
}
> use test
switched to db admin
> show collections
Mon Oct 13 17:13:51 uncaught exception: error: {
      "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
      "code" : 10057
}




    验证admin库里面的sa账户是否能够查看admin库和test库的信息


1
2
3
4
5
6
7
8
9
10
11
> db.auth('sa','sa')
1
> show collections
system.indexes
system.users
> use test
switched to db test
> show collections
system.indexes
system.users
>




    进入test库并创建用户test密码test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> use test
switched to db test
> db.addUser('test','test')
{
      "updatedExisting" : true,
      "n" : 1,
      "connectionId" : 10,
      "err" : null,
      "ok" : 1
}
{
      "_id" : ObjectId("543b80be1d60b11044c2fc59"),
      "user" : "test",
      "readOnly" : false,
      "pwd" : "a6de521abefc2fed4f5876855a3484f5"
}
>




    ctrl+c退出重新登录,验证test账户能够访问test库和admin库

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.auth('test','test')
1
> show collections
system.indexes
system.users
> use admin
switched to db admin
> show collections
Mon Oct 13 17:21:06 uncaught exception: error: {
      "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
      "code" : 10057
}
>




    验证admin库的admin帐号,看是否能查看admin库的信息

1
2
3
4
5
6
7
8
> use admin
switched to db admin
> db.auth('sa','sa')
1
> show collections
system.indexes
system.users
>




PS:当验证用户的时候,如果返回1证明有此用户,如果返回0证明没有此用户 如:

1
2
3
4
> use admin
switched to db admin
> db.auth('sa','sa')
1






页: [1]
查看完整版本: mongodb的用户管理