在未开启auth模式下新建sa用户
//进入admin数据库
mongo admin
//新建sa超级用户1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22db.createUser(
{
user: "sa",
pwd: "1qaz2wsx",
roles:
[
{ db: "uadb", role: "readWrite" },
{ db: "uadb", role: "dbAdmin" },
{ db: "uadb", role: "userAdmin" },
{ db: "uadb", role: "dbOwner" },
{ db: "uadb_attachment", role: "readWrite" },
{ db: "uadb_attachment", role: "dbAdmin" },
{ db: "uadb_attachment", role: "userAdmin" },
{ db: "uadb_attachment", role: "dbOwner" },
{ db: "admin", role: "readWrite" },
{ db: "admin", role: "dbAdmin" },
{ db: "admin", role: "userAdmin" },
{ db: "admin", role: "dbOwner" },
{ db: "admin", role: "root" }
]
}
)
//sa用户授权测试
db.auth(“sa”,”1qaz2wsx”)
启用MongoDB权限控制
Windows
卸载现有MongoDB服务
C:\WINDOWS\system32>sc delete “MongoDB”
启动服务
按照MongoDB服务(设置权限控制):E:\mongodb\bin\mongod —logpath “E:\mongodb\log\mongo.log” —logappend —dbpath “E:\mongodb\data” —directoryperdb —auth —serviceName “MongoDB” —serviceDisplayName “MongoDB” —install
Linux
启动服务
/mnt/data/mongodb/bin/mongod —dbpath /mnt/data/mongodb/data —logpath /mnt/data/mongodb/log/mongodb.log —auth
开启auth后新建用户
1 | //以admin登陆获取权限 |