1:创建管理员
D:\mongodb\bin\mongo --port 27017
MongoDB shell version v4.2.15
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("df917c43-c160-41af-8f13-ad42e0a31abe") }
MongoDB server version: 4.2.15
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
Server has startup warnings:
> use admin
switched to db admin
> db.createUser({user: "root", pwd: "eshop", roles: [{role: "root", db: "admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>
user: "用户名", pwd: "密码"
2:数据库用户
use testdb
db.createUser({user: "test", pwd: "test", roles: [{role: "readWrite", db: "testdb" }]})