`

MongoDB 身份认证例子(译)

阅读更多

 

原文出自:http://www.mkyong.com/mongodb/mongodb-authentication-example/

返回目录:http://ysj5125094.iteye.com/blog/2192754 

 

 

MongoDB Authentication Example

This guide shows you how to enable authentication in MongoDB. The authentication is disabled by default. To configure it, you must first add a user to the “admin” database.

译:本指南将告诉你,如果在MongoDB中启用身份认证。 默认情况下身份认证是禁用的。如果想要配置他,首先必须把该用户添加到 "admin" 数据库。

 

 

> show dbs
admin  #add single user to this database
testdb

 

Note
Users with normal access in “admin” database, HAVE read and write access to all the other databases. Users with read only access to the “admin” database HAVE only read to all databases.

译:注,用户如果有一般管理员数据库权限,那就有读写访问所有其他的数据库的权利。如果用户只能访问管理员数据库,那他对其他数据库只有读权限。

 

P.S This example is using MongoDB version 2.2.3

译:这个例子使用的是MongoDB 2.2.3版本。

Authentication example

See a full example to add a “admin” user to the admin database, and a normal user to the “testdb” database, and how to perform the authentication.

译:看下面一个完整的实例,添加一个"admin"用户到 admin 数据库,普通用户操作"testdb"数据库,以及如何进行身份认证。

 

Terminal 1 – Start MongoDB in secure mode, authentication is required.

译:终端1 - 在安全模式下启动MongoDB,需要认证。

$mongod --auth

Terminal 2 – MongoDB client, see comment “#” for self-explanatory. 

译:终端2 - MongoDB客户端,以#开头的是注释部分。

$ mongo
MongoDB shell version: 2.2.3
connecting to: test
> use admin             		#1. connect to the "admin" database.
switched to db admin			
> db.addUser("admin","password")	#2. add a user "admin" to the admin database. 
{
	"user" : "admin",
	"readOnly" : false,
	"pwd" : "90f500568434c37b61c8c1ce05fdf3ae",
	"_id" : ObjectId("513af8cac115e7a6b4bcceb9")
}
addUser succeeded, but cannot wait for replication since we no longer have auth
 
> use testdb				#3. connect to the "testdb" database.
switched to db testdb
> show collections			#4. now, read and write need authentication
Sat Mar  9 16:54:57 uncaught exception: error: {
	"$err" : "unauthorized db:testdb ns:testdb.system.namespaces lock type:0 client:127.0.0.1",
	"code" : 10057
}
> use admin				#5. connect back to the "admin" database.
switched to db admin
> db.auth("admin","password")		#6. performs authentication, 1 means succeed, 0 means failed
1
> use testdb				#7. connect to the "testdb" database.
switched to db testdb
> show collections			#8. no problem, it shows all collections
system.indexes
user
> db.addUser("testdb","password")       #9. add another user "testdb" to the "testdb" database.
{
	"user" : "testdb",
	"readOnly" : false,
	"pwd" : "b9ff75cbf18bd98d8554efec12c72090",
	"_id" : ObjectId("513af934c115e7a6b4bcceba")
}
> show collections
system.indexes
system.users				#10. All users' data are stored in this system.users collection.
user
> db.system.users.find()
{ "_id" : ObjectId("513af934c115e7a6b4bcceba"), "user" : "testdb", "readOnly" : false, "pwd" : "b9ff75cbf18bd98d8554efec12c72090" }
>

 

Done.

 

References

  1. MongoDB : Security Practices and Management
  2. Java MongoDB authentication example

 

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics