文章

MongoDB-初始化配置

登录进入MongoDB

1
2
3
4
5
6
7
bash> mongosh
Current Mongosh Log ID:	63e51d21435b808fe1db5c3a
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB:		6.0.4
Using Mongosh:		1.6.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

创建用户及设置密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
=======================进入MongoDB==================
test> show dbs

#切换用户:
test> use admin
switched to db admin

# 设置管理员用户名和密码
admin> db.createUser({ user: "admin", pwd: "******", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })

#验证是否成功:返回1成功
admin> db.auth("admin", "******")
{ ok: 1 }

# 查询用户
admin> db.system.users.find()

创建用户角色

1
db.createRole({ role: "allDBRole", privileges: [ { resource: { db: "", collection: "" }, actions: ["find", "insert", "update", "remove"] }], roles: [] })

授予用户权限

1
db.grantRolesToUser("admin", ["allDBRole"])
本文由作者按照 CC BY 4.0 进行授权