mongod

mongodb密码和传统数据如mysql等有些区别:

  1. mongodb的用户名和密码是基于特定数据库的,而不是基于整个系统的。所有所有数据库db都需要设置密码

mongodb设置管理用户和密码:

  1. show dbs
    在mongodb新版本里并没有admin数据库,但是并不妨碍第2步操作。

  2. use admin 进入admin数据库

  3. 创建管理员账户
    db.createUser({ user: "useradmin", pwd: "adminpassword", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
    mongodb中的用户是基于身份role的,该管理员账户的 role是 userAdminAnyDatabase。 ‘userAdmin’代表用户管理身份,’AnyDatabase’ 代表可以管理任何数据库。

  4. 验证第3步用户添加是否成功
    db.auth("useradmin", "adminpassword") 如果返回1,则表示成功。
    exit退出系统
    db.auth()方法理解为 用户的验证功能

  5. 修改配置

1
sudo vi /etc/mongod.conf

找到

1
#security:
取消注释,修改为:
  1. security:
  2. authorization: enabled #注意缩进,缩进参照配置文件其他配置。缩进错误可能第6步重启不成功。
  1. 重启mongodb sudo service mongod restart

  2. 进入mongodb,用第3步的 管理员账户登录,用该账户创建其他数据库管理员账号

  1. use admin
  2. db.auth("useradmin", "adminpassword")
  1. 新建你需要管理的mongodb 数据的账号密码。

    #必须要use否则创建的不生效,连接也不生效,否则连接的时候只能用admin的数据库去连rotbo3T,但也可以看到自己的Database,也可以操作数据库

    1. use yourdatabase
    2. db.createUser({ user: "youruser", pwd: "yourpassword", roles: [{ role: "dbOwner", db: "yourdatabase" }] })

    rote:dbOwner 代表数据库所有者角色,拥有最高该数据库最高权限。比如新建索引等

  2. 新建数据库读写账户

  1. use yourdatabase
  2. db.createUser({ user: "youruser2", pwd: "yourpassword2", roles: [{ role: "readWrite", db: "yourdatabase" }] })

    该用户用于该数据的读写,只拥有读写权限。

  1. 现在数据的用户名和密码就建好了。
    可以使用:mongodb://youruser2:yourpassword2@localhost/yourdatabase来链接

  2. 验证
    ps aux|grep mongo

    1
    2
    3
    4
    5
    mongod    3609  0.2  8.6 1087064 87484 ?       Sl   13:32   0:30 /usr/bin/mongod -f /etc/mongod.conf
    root 4150 0.0 0.0 112724 988 pts/0 R+ 17:10 0:00 grep --color=auto mongo

    没起服务之前
    root 3574 0.0 0.0 112724 988 pts/0 R+ 13:28 0:00 grep --color=auto mongo

验证安全认证:

use admin
witched to db admin
show dbs –没有认证查看数据库报错
014-09-14T13:28:45.953+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47

db.auth(“super”,”super”) —认证后再次查看ok
1
show dbs
dmin 0.078GB
ocal 0.078GB
est 0.078GB
angwei 0.078GB

普通用户认证

show dbs –没有认证查看数据
014-09-14T13:31:19.265+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47

db.auth(“test”,”test”)
1
show dbs –认证后查看数据库还报错,原因这个用户属于test不属于admin
014-09-14T13:33:30.062+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47

E:\mongodb\bin>mongo 127.0.0.1:27019
MongoDB shell version: 2.6.4
connecting to: 127.0.0.1:27019/test

db.mycol.insert({“id”:222}) –没有认证情况插入文档失败
WriteResult({
“writeError” : {
“code” : 13,
“errmsg” : “not authorized on test to execute command { insert:
“mycol\”, documents: [ { _id: ObjectId(‘5415292f131751676caa7881’), id: 222.0 }
], ordered: true }”
}
})
db.auth(“test”,”test”) –认证后插入文档成功
1
db.mycol.insert({“id”:222})
riteResult({ “nInserted” : 1 })

只读用户认证
E:\mongodb\bin>mongo 127.0.0.1:27019
MongoDB shell version: 2.6.4
connecting to: 127.0.0.1:27019/test

db.mycol.find() –没有认证查询失败
rror: { “$err” : “not authorized for query on test.mycol”, “code” : 13 }
db.auth(“readonly”.”readonly”)
014-09-14T13:38:16.265+0800 SyntaxError: Unexpected string
db.auth(“readonly”,”readonly”)
1
db.mycol.find() –认证后查询成功
“_id” : ObjectId(“5415294b131751676caa7882”), “id” : 222 }

db.mycol.insert({“id”:5555}) –只读认证后,插入文档失败,原因用户是只读的
WriteResult({
“writeError” : {
“code” : 13,
“errmsg” : “not authorized on test to execute command { insert:
“mycol\”, documents: [ { _id: ObjectId(‘541529ead090e8f5c50762b9’), id: 5555.0
], ordered: true }”
}
})


参考文档

-------------本文结束感谢您的阅读-------------

本文标题:mongod

文章作者:yangpu

发布时间:2019年04月22日 - 09:45

最后更新:2019年04月22日 - 09:45

原始链接:https://mongofeng.github.io/2019/04/22/mongodb/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。