美文网首页
MongoDB创建数据库和用户

MongoDB创建数据库和用户

作者: 灵魂函数 | 来源:发表于2017-02-21 18:30 被阅读0次
step 1. 为mongodb添加admin管理员
root@12.154.29.163:~
# mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
> use admin
switched to db admin
> db.createUser({
... user:'admin',
... pwd:'admin',
... roles:[{ role: "userAdminAnyDatabase", db: "admin" }]
...})
Successfully added user: {
    "user" : "dba",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
step 2. 修改mongod.conf配置文件
# vi /etc/mongod.conf 
//去掉注释
security:
  authorization: enabled
# service mongod restart
# mongo
step 3. 为album添加用户
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
> use admin
switched to db admin
> db.auth('admin','admin')
1
> show dbs
admin  0.000GB
local  0.000GB
> use album
switched to db album
> show collections    //结果为空
> db.foo.insert({_id:1,name:"test"})
WriteResult({ "nInserted" : 1 })
> show collections
foo
> db.createUser({
... user:'admin',
... pwd:'admin',
... roles:['readWrite']
... })
Successfully added user: { "user" : "admin", "roles" : [ "readWrite" ] }
> exit
bye
Tips:
  1.添加的管理员或者用户名称都为:admin,但是对应的数据库不同
  2.mongoDB之前的版本,用addUser
  3.更多详细操作示例可参见:http://www.cnblogs.com/zhoujinyi/p/4610050.html

相关文章

网友评论

      本文标题:MongoDB创建数据库和用户

      本文链接:https://www.haomeiwen.com/subject/ijmuwttx.html