kylin

醉里论道,醒时折花。

kylin

使用mongo shell命令行工具在指定数据库下新建用户

在指定数据库下新建用户

输入mongo命令

1
2
3
4
5
root@iZm5e3ljqlvdsruj2dkr7pZ:~# mongo
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.2
>

先在admin数据库完成校验

1
2
3
4
> use admin
switched to db admin
> db.auth('xx','xxx')
1

切换到想要新建用户的数据库

1
2
> use test
switched to db test

使用db.createUser()新建用户,参考文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.createUser({
... ... user:"root",
... ... pwd:"root",
... ... roles:[{
... ... role:'readWrite',db:'test'
... ... }]
... ... })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}