欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongodb 开启用户密码访问

程序员文章站 2024-03-19 13:57:58
...

连接mongo

mongo --port 27017

创建admin账号:

use admin

db.createUser(
  {
    user: "admin",
    pwd: "passwd",
    roles: [{ role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase"]
  }
)

admin登录

mongo --port 27017  --authenticationDatabase "admin" -u "admin" -p "SPb365U44s"

创建库用户

use file
db.createUser(
  {
    user: "file_owner",
    pwd:  "passwd",
    roles: [ { role: "dbOwner", db: "file" }]
  }
)

登录测试

mongo --port 27017  --authenticationDatabase "file" -u "file_owner" -p "passwd"