添加用户认证,必须在启动mongod的时候使用参数--auth
用户的认证信息是保存在每个数据库的system.users中的
1.在admin中创建用户admin,密码adminpasswd
> use admin
switched to db admin
> db.addUser("admin","adminpasswd")
{
"user" : "admin",
"readOnly" : false,
"pwd" : "20e0a3ab4c58c290137d3d71
67bcf08b"
}
默认情况下是赋于用户读写的权限的,如果只想给某个用户只读权限,可以用
db.addUser("name","passwd",true)
2.添加用户认证
> db.auth("admin","adminpasswd")
1
如果认证成功,会显示“1”
3.查看数据库的用户信息
> use admin
switched to db admin
> db
admin
> show collections
system.indexes
system.users
> db.system.users.find()
{ "_id" : ObjectId("4e0d739eb3c346f3e43adba4 "), "user" : "admin","readOnly" : false, "pwd" : "e96d1448efad8a776534903d
dff78e9a"}
4.数据库test添加用户名tt,密码tt,并添加认证
> use test
> db.addUser("tt","tt")
> db.auth("tt","tt")
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d "), "user" : "tt","readOnly" : false, "pwd" : "20e0a3ab4c58c290137d3d71
67bcf08b"}
5.测试用户的权限认证是否正确
bin/mongo --port 27017
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27017/test
> show dbs
Mon Jul 4 11:54:13 uncaught exception:listDatabases failed:{
"assertion" : "unauthorized db:admin lock type:-1client:127.0.0.1",
"assertionCode" : 10057,
"errmsg" : "db assertion failure",
"ok" : 0
}
>db
admin
> show collections
Mon Jul 4 11:55:22 uncaught exception: error:{
"$err" : "unauthorized db:admin lock type:-1client:127.0.0.1",
"code" : 10057
}
结果显示,登录时不指定用户名和密码时会报错,说明安全性的部署生效了
使用用户名/密码登录
bin/mongo --port 27017 test -u tt -p
MongoDB shell version: 1.8.1
Enter password:
connecting to: 127.0.0.1:27017/test
> use admin
switched to db admin
> db.system.users.find()
error: {
"$err" : "unauthorized db:admin lock type:-1client:127.0.0.1",
"code" : 10057
}
> use test
switched to db test
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d "), "user" : "tt","readOnly" : false, "pwd" : "20e0a3ab4c58c290137d3d71
67bcf08b"}
可以看到,由于tt用户是在test数据库创建的用户,所以它不具有对其他数据库操作的权限
那么admin用户呢?
bin/mongo --port 27017 admin -u admin -p
MongoDB shell version: 1.8.1
Enter password:
connecting to: 127.0.0.1:27017/admin
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : ObjectId("4e0d739eb3c346f3e43adba4 "), "user" : "admin","readOnly" : false, "pwd" : "e96d1448efad8a776534903d
dff78e9a"}
> use test
switched to db test
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d "), "user" : "tt","readOnly" : false, "pwd" : "20e0a3ab4c58c290137d3d71
67bcf08b"}
admin这个用户对所有数据库都具有操作的权限
需认证的主从处理:
mongod --port 27017 --dbpath /data/mongodb/dbs/master --logpath /data/mongodb/dbs/master/master.log --fork --master --auth
mongod --port 27018 --dbpath /data/mongodb/dbs/slave --logpath /data/mongodb/dbs/slave/slave.log --fork --slave --source localhost:27017 --auth
无需认证的主从处理:
mongod --port 27017 --dbpath /data/mongodb/dbs/master --master
mongod --port 27018 --dbpath /data/mongodb/dbs/slave --slave --source localhost:27017
用户的认证信息是保存在每个数据库的system.users中的
1.在admin中创建用户admin,密码adminpasswd
> use admin
switched to db admin
> db.addUser("admin","adminpasswd")
{
}
默认情况下是赋于用户读写的权限的,如果只想给某个用户只读权限,可以用
db.addUser("name","passwd",true)
2.添加用户认证
> db.auth("admin","adminpasswd")
1
如果认证成功,会显示“1”
3.查看数据库的用户信息
> use admin
switched to db admin
> db
admin
> show collections
system.indexes
system.users
> db.system.users.find()
{ "_id" : ObjectId("4e0d739eb3c346f3e43adba4
4.数据库test添加用户名tt,密码tt,并添加认证
> use test
> db.addUser("tt","tt")
> db.auth("tt","tt")
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d
5.测试用户的权限认证是否正确
bin/mongo --port 27017
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27017/test
> show dbs
Mon Jul
}
>db
admin
> show collections
Mon Jul
}
结果显示,登录时不指定用户名和密码时会报错,说明安全性的部署生效了
使用用户名/密码登录
bin/mongo --port 27017
MongoDB shell version: 1.8.1
Enter password:
connecting to: 127.0.0.1:27017/test
> use admin
switched to db admin
> db.system.users.find()
error: {
}
> use test
switched to db test
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d
可以看到,由于tt用户是在test数据库创建的用户,所以它不具有对其他数据库操作的权限
那么admin用户呢?
bin/mongo --port 27017 admin -u admin -p
MongoDB shell version: 1.8.1
Enter password:
connecting to: 127.0.0.1:27017/admin
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : ObjectId("4e0d739eb3c346f3e43adba4
> use test
switched to db test
> db.system.users.find()
{ "_id" : ObjectId("4e112c34f5fd77418d7b437d
admin这个用户对所有数据库都具有操作的权限
需认证的主从处理:
mongod --port 27017 --dbpath /data/mongodb/dbs/master --logpath /data/mongodb/dbs/master/master.log --fork --master --auth
mongod --port 27018 --dbpath /data/mongodb/dbs/slave --logpath /data/mongodb/dbs/slave/slave.log --fork --slave --source localhost:27017 --auth
无需认证的主从处理:
mongod --port 27017 --dbpath /data/mongodb/dbs/master --master
mongod --port 27018 --dbpath /data/mongodb/dbs/slave --slave --source localhost:27017