开启MongoDB服务时不添加任何参数时,默认是没有权限验证的,登录的用户可以对数据库任意操作而且可以远程访问数据库!
   在刚安装完毕的时候MongoDB都默认有一个admin数据库,此时admin数据库是空的,没有记录权限相关的信息!当admin.system.users一个用户都没有时,即使mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作(不管是否是以--auth 参数启动),直到在admin.system.users中添加了一个用户。
需要注意的是:admin.system.users中将会保存比在其它数据库中设置的用户权限更大的用户信息,拥有超级权限,也就是说在admin中创建的用户可以对mongodb中的其他数据库数据进行操作。
1 mongodb系统中,数据库是由超级用户来创建的,一个数据库可以包含多个用户,一个用户只能在一个数据库下,不同数据库中的用户可以同名!
2 当admin.system.users一个用户都没有时,即使mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作(不管是否是以--auth 参数启动),直到在admin.system.users中添加了一个用户。
3 特定数据库比如DB1下的用户User1,不能够访问其他数据库DB2,但是可以访问本数据库下其他用户创建的数据!
4 不同数据库中同名的用户不能够登录其他数据库!比如DB1,DB2都有user1,以user1登录DB1后,不能够登录到DB2进行数据库操作!
5 在admin数据库创建的用户具有超级权限,可以对mongodb系统内的任何数据库的数据对象进行操作!


下面通过实验的方式进程验证(可能不全面,欢迎大家指正):
1 第一次安装monogdb时,admin数据库中没有任何用户,此时不管是否以--auth方式启动数据库,其他数据库(比如test数据库)中的用户都可以对另外的数据库(比如db1数据库)中的数据进行操作~!
a)以默认的方式启动mongodb
[mongodb@rac3 bin]$ ./mongod  --dbpath=/opt/mongodata/data --port=27000
在另一个窗口进入mongodb shell,默认是直接进入test 数据库的,并且此时用户拥有超级权限,可以操作任何数据库对象!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
> show dbs
local   (empty)
#查看admin 数据库中的用户信息,因为是刚建立的数据库所以user 为空~!
> use admin
switched to db admin
> db.system.users.find() ;
#创建test数据库,并创建对象yql,插入数据!
> use test
switched to db test
>
> db.yql.insert({id:2,val:"yangql is learing monogdb master slave!"});
#创建db1数据库,并创建对象db1_test,插入数据!
> use db1
switched to db db1
>
> db.db1_test.insert({id:1,val:"this data is in db1 !"});
> db.db1_test.insert({id:2,val:"this data is in db1 !"});
> db.db1_test.insert({id:3,val:"this data is in db1 !"});
#创建db2数据库,并创建对象db2_test,插入数据!
> use db2
switched to db db2
>
> db.db2_test.insert({id:1,val:"this data is in db2!"});
> db.db2_test.insert({id:2,val:"this data is in db2!"});
> db.db2_test.insert({id:3,val:"this data is in db2!"});
> db.db2_test.find();
{ "_id" : ObjectId("4f2bbcdf2a801e73e6493f31"), "id" : 1, "val" : "this data is in db2!" }
{ "_id" : ObjectId("4f2bbce52a801e73e6493f32"), "id" : 2, "val" : "this data is in db2!" }
{ "_id" : ObjectId("4f2bbce92a801e73e6493f33"), "id" : 3, "val" : "this data is in db2!" }
>
> show dbs
admin   (empty)
db1     0.203125GB
db2     0.203125GB
local   (empty)
test    0.203125GB
#在test 数据库中创建用户yql,密码为yql
> use test
switched to db test
>
> db.addUser("yql","yql")
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
        "user" : "yql",
        "readOnly" : false,
        "pwd" : "868ed7035435f33b60ebeba2f363ad91",
        "_id" : ObjectId("4f2bbed556f179b1ccc295d1")
}

> db.auth("yql","yql") #验证函数,验证数据库中是否存在对应的用户
1
>
> db.system.users.find();
{ "_id" : ObjectId("4f2bbed556f179b1ccc295d1"), "user" : "yql", "readOnly" : false, "pwd" : "868ed7035435f33b60ebeba2f363ad91" }
>
> exit
bye

b)关闭mongod 服务,并以认证方式启动数据库
[mongodb@rac3 bin]$ ./mongod  --dbpath=/opt/mongodata/data --port=27000 --auth
再次登录,虽然在test中创建了用户,但是没有在admin 数据库中创建用户,所以以默认方式登录的用户依然具有超级权限
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
>
> use test
switched to db test
>
> db.system.users.find();
{ "_id" : ObjectId("4f2bbed556f179b1ccc295d1"), "user" : "yql", "readOnly" : false, "pwd" : "868ed7035435f33b60ebeba2f363ad91" }
>
>
> use db1
switched to db db1
>
> db.db1_test.find();
{ "_id" : ObjectId("4f2bb3a42a801e73e6493f2b"), "id" : 1, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3ae2a801e73e6493f2c"), "id" : 2, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3b32a801e73e6493f2d"), "id" : 3, "val" : "this data is in db1 !" }
>
> exit
bye
使用特定用户登录数据库,也可以访问其他的数据库。下面的例子说明,test的用户可以访问db1的数据
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000 -uyql -pyql
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
>
> use db1
switched to db db1
> db.db1_test.find();
{ "_id" : ObjectId("4f2bb3a42a801e73e6493f2b"), "id" : 1, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3ae2a801e73e6493f2c"), "id" : 2, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3b32a801e73e6493f2d"), "id" : 3, "val" : "this data is in db1 !" }
>
bye

2 在admin.system.users中添加用户之后,mongodb的认证,授权服务生效! 
#在admin 数据库中创建用户!supper 密码为sup
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
> use admin
switched to db admin
>
> db.addUser("supper", "sup")  
{ "n" : 0, "connectionId" : 4, "err" : null, "ok" : 1 }
{
        "user" : "supper",
        "readOnly" : false,
        "pwd" : "51a481f72b8b8218df9fee50b3737c44",
        "_id" : ObjectId("4f2bc0d357a309043c6947a4")
}
>
> db.auth("supper","sup")
1
>
> exit
bye
[mongodb@rac3 bin]$
默认方式登录,即以无认证用户登录,查询的时候会显示无权限!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
>
> db.system.users.find();
error: {
        "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
        "code" : 10057
}
>
> show dbs 
Fri Feb  3 19:12:30 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
>
>
> exit
bye
在admin数据库创建用户后,使用认证方式登录,可进行对应数据库的查询操作且仅仅能够查询对应的数据库中的信息!不能够查询其他mongodb系统的其他数据库信息!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000 -uyql -pyql
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
>
> db.system.users.find();
{ "_id" : ObjectId("4f2bbed556f179b1ccc295d1"), "user" : "yql", "readOnly" : false, "pwd" : "868ed7035435f33b60ebeba2f363ad91" }
>
> db.yql.find();
{ "_id" : ObjectId("4f2bb3662a801e73e6493f2a"), "id" : 2, "val" : "yangql is learing monogdb master slave!" }
>查询系统数据库信息时,报如下错误!
> show dbs; 
Fri Feb  3 19:15:56 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
#登录db1
> use db1
switched to db db1
#查询的时候,会报错,非授权用户!
> db.db1_test.find();
error: {
        "$err" : "unauthorized db:db1 lock type:-1 client:127.0.0.1",
        "code" : 10057
}
> use db2
switched to db db2
>
> db.db2_test.find()
error: {
        "$err" : "unauthorized db:db2 lock type:-1 client:127.0.0.1",
        "code" : 10057
}
>
> exit
bye
使用db1的用户可以查询db1的数据,但是不能查看其他的数据库的数据!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db1 -udb1 -pdb1
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db1
>
> db.db1_test.find()
{ "_id" : ObjectId("4f2bb3a42a801e73e6493f2b"), "id" : 1, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3ae2a801e73e6493f2c"), "id" : 2, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3b32a801e73e6493f2d"), "id" : 3, "val" : "this data is in db1 !" }
>
> db.system.users.find();
{ "_id" : ObjectId("4f2bc2d7b85653a70aa4fc50"), "user" : "db1", "readOnly" : false, "pwd" : "08a3bfa3cdef4464c4738a7180465adf" }
>
> db.auth("db1","db1")
1
>
> show dbs
Fri Feb  3 19:21:08 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
>
> use db2
switched to db db2
>
> show collections
Fri Feb  3 19:21:24 uncaught exception: error: {
        "$err" : "unauthorized db:db2 lock type:-1 client:127.0.0.1",
        "code" : 10057
}
> use db1
switched to db db1
> show collections
db1_test
system.indexes
system.users
>
特定数据库比如DB1下的用户User1,是可以访问本数据库下其他用户创建的数据
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db2 -udb1 -pdb1
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db2
> 
> db.user_db1.insert({id:1,val:"this data is created by db1 in db2!"});
> db.user_db1.insert({id:2,val:"this data is created by db1 in db2!"});
> exit
bye
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db2 -udb2 -pdb2
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db2
> 
> db.user_db1.find();
{ "_id" : ObjectId("4f2bd237c19753688c950aaf"), "id" : 1, "val" : "this data is created by db1 in db2!" }
{ "_id" : ObjectId("4f2bd23bc19753688c950ab0"), "id" : 2, "val" : "this data is created by db1 in db2!" }
> 
 
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000 -usupper -psup
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test
Fri Feb  3 19:16:55 uncaught exception: login failed
exception: login failed
3 使用supper 用户登录!可以对mongodb系统内的所有数据库进行查询,DML操作!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/admin  -usupper -psup
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/admin
>
> show dbs
admin   0.203125GB
db1     0.203125GB
db2     0.203125GB
local   (empty)
test    0.203125GB
>
> use db1
switched to db db1
> db.db1_test.find()
{ "_id" : ObjectId("4f2bb3a42a801e73e6493f2b"), "id" : 1, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3ae2a801e73e6493f2c"), "id" : 2, "val" : "this data is in db1 !" }
{ "_id" : ObjectId("4f2bb3b32a801e73e6493f2d"), "id" : 3, "val" : "this data is in db1 !" }
> use db2
switched to db db2
> db.db2_test.find()
{ "_id" : ObjectId("4f2bbcdf2a801e73e6493f31"), "id" : 1, "val" : "this data is in db2!" }
{ "_id" : ObjectId("4f2bbce52a801e73e6493f32"), "id" : 2, "val" : "this data is in db2!" }
{ "_id" : ObjectId("4f2bbce92a801e73e6493f33"), "id" : 3, "val" : "this data is in db2!" }
>
> use test
switched to db test
>
> db.system.users.find();
{ "_id" : ObjectId("4f2bbed556f179b1ccc295d1"), "user" : "yql", "readOnly" : false, "pwd" : "868ed7035435f33b60ebeba2f363ad91" }
>
> db.yql.find();
{ "_id" : ObjectId("4f2bb3662a801e73e6493f2a"), "id" : 2, "val" : "yangql is learing monogdb master slave!" }
>
> db.yql.remove();###删除数据###
>
> db.yql.find();
>
> use db1
switched to db db1
>
> db.addUser("db1", "db1")  
{ "n" : 0, "connectionId" : 9, "err" : null, "ok" : 1 }
{
        "user" : "db1",
        "readOnly" : false,
        "pwd" : "08a3bfa3cdef4464c4738a7180465adf",
        "_id" : ObjectId("4f2bc2d7b85653a70aa4fc50")
}
> exit
bye
4 不同数据库中的用户可以同名,不同数据库中同名的用户依然不登录其他数据库!比如DB1,DB2都有user1,以user1登录DB1后,不能够登录到DB2进行数据库操作!
在不同数据库中创建相同的用户,进行测试!
测试场景:在test,db2数据库中创建用户db1,密码db1
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/admin  -usupper -ppwd2
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/admin

> use db2
switched to db db2

> db.addUser("db1", "db1")  
{ "n" : 0, "connectionId" : 17, "err" : null, "ok" : 1 }
{
        "user" : "db1",
        "readOnly" : false,
        "pwd" : "08a3bfa3cdef4464c4738a7180465adf",
        "_id" : ObjectId("4f2bccb3e39cb674302ce2dd")
}

> exit
bye
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000 -uyql -ppwd1
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/test

> db.addUser("db1", "db1")  
{ "n" : 0, "connectionId" : 19, "err" : null, "ok" : 1 }
{
        "user" : "db1",
        "readOnly" : false,
        "pwd" : "08a3bfa3cdef4464c4738a7180465adf",
        "_id" : ObjectId("4f2bcce9b5accbdac9e71a93")
}
> exit
bye
[mongodb@rac3 bin]$ 
使用用户db1登录db2数据库,然后尝试登录db1,并进行查询测试,报错显示未授权!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db2 -udb1 -pdb1
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db2

> use db1
switched to db db1

> db.db1_test.find();
error: {
        "$err" : "unauthorized db:db1 lock type:-1 client:127.0.0.1",
        "code" : 10057
}

某个数据库中对应的用户只能对本数据库进行操作,而不能操作其他数据库,包括查询和创建其他数据库!
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db1 -udb1 -pdb1       
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db1


> use db3
switched to db db3

> db.db3_test.insert({id:1,val:"this data is in db3!"});
unauthorized
> db.db3_test.insert({id:2,val:"this data is in db3!"});
unauthorized
> db.db3_test.find();
error: {
        "$err" : "unauthorized db:db3 lock type:-1 client:127.0.0.1",
        "code" : 10057

}
> exit
bye

使用db.auth()可以对数据库中的用户进行验证,如果验证成功则返回1,否则返回0!
 db.auth() 只能针对登录用户所属的数据库的用户信息进行验证,不能验证其他数据库的用户信息,因为访问不了其他数据库(有点小白的解释)
[mongodb@rac3 bin]$ ./mongo 127.0.0.1:27000/db1 -udb1 -pdb1
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27000/db1
>
> db.auth("yql","pwd")
0
> db.auth("db1","db1")
1
>
> exit
bye
------------------------------EOF-----------------------------
10-28 18:25