mongodb 用户认证

       添加用户认证,必须在启动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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值