mongodb3.4开启用户认证

2017-12-25 16:37:14

db version v3.4.0


初始安装的时候没有admin数据库


开启认证

修改配置文件/etc/mongodb.conf

打开auth的注释,设置为auth = true


上面这个方法我没有成功,我用的下面的

添加这个

security:

    authorization: enabled


重启mongodb

sudo service mongodb restart


添加管理员

使用命令mongo进入命令行

创建第一个用户,该用户需要有用户管理权限

这里设置其角色为root

use admin
db.createUser({user:"admin",pwd:"password",roles:["root"]})

新增的用户在system.users中

> db.getCollectionNames()
[ "system.indexes", "system.users", "system.version" ]


第一个用户添加完成后,便需要认证才能继续添加其他用户

使用db.auth("admin", "password")认证


添加数据库用户

为其他数据库添加用户,添加用户前需要切换到该数据库

这里设置其角色为dbOwner

use testdb1

db.createUser({user: "testdb1u1", pwd: "xyz123", roles: [{ role: "dbOwner", db: "testdb1" }]})


查看用户

> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Fdh2ldIW3Aw8Cxz9Dt+96g==", "storedKey" : "zbkfj6ZQH1xwGoOg8JJ6OjtR3Cs=", "serverKey" : "yqkqHABZ64rEeq1X0htOAtUnwFU=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "testdb1.testdb1u1", "user" : "testdb1u1", "db" : "testdb1", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Xxt2uET3jRtAYVigyLUydw==", "storedKey" : "yinLG61nRFzfC+3NtB5p9RR+avM=", "serverKey" : "OX/Pdft7JWJm/g0jg07q49OC4c8=" } }, "roles" : [ { "role" : "dbOwner", "db" : "testdb1" } ] }



参考

https://docs.mongodb.com/manual/tutorial/enable-authentication/

https://docs.mongodb.com/manual/core/security-built-in-roles/

https://docs.mongodb.com/manual/reference/built-in-roles/


  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。

  • 2017-11-23 02:00:51

    js 分页插件twbs-pagination

    ​cdn地址 http://www.bootcdn.cn/twbs-pagination/ 官网地址 可以在cdn地址上面查看到