进入
mongo.exe
创建用户
use admin
db.createUser(
{
user: "root",
pwd: "root",
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)
远程访问
mongod.cfg
net:
port: 27017
#bindIp: 127.0.0.1
bindIp: 0.0.0.0
MongoDB中每个数据库之间是相互独立的,都有独立的权限,正确的做法是使用root账号在【将要操作的数据库】中创建一个【子账号】,在用这个子账号连接mongo
例如:
>use admin
switched to db admin
>db.auth("root","123456")
1
>show dbs
admin
local
testDB
>use testDB
switched to db testDB
>db.createUser(
{
user:"usertest",
pwd:"123456",
roles:[{role:"dbOwner",db:"testBD"}]
}
)
Successfully added user: {
"user" : "usertest",
"roles" : [
{
"role" : "dbOwner",
"db" : "testDB"
}
]
}