mongodb配置远程访问

安装

按照官方文档即可

配置文件

默认的配置是无验证但仅限本机IP (127.0.0.1) 访问

1
2
3
4
net:
bindIp: 0.0.0.0
security:
auth: true

创建用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 创建管理员用户
> db.createUser({user: 'root', pwd:'YOUR PASSWORD', roles:['root']})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
# 验证测试
> db.auth('root', 'PASSWORD')
1
# 创建数据库用户 ( 以 test 数据库为例)
> db.createUser({user:'rocket',pwd:'YOUR ANOTHER PASSWORD',roles:[{role:'readWrite',db:'test'}]})
Successfully added user: {
"user" : "rocket",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}

远程连接数据库

MongoDB Compass 为例