MongoDB 设置远程连接

修改配置文件mongod.conf

Linux配置文件路径,/etc/mongod.conf

net:
port: 27017
- bindIp: 127.0.0.1
+ bindIp: 0.0.0.0

# 密码登录
security:
authorization: enabled

创建用户

> use admin
switched to db admin
> db.createUser({
user: '??????',
pwd: '123456',
roles: [{role: 'userAdminAnyDatabase', db: 'admin'}],
});

查看刚才创建的用户

db.getUsers();

重启数据库

mongod - f /etc/mongod.conf;
root@VM-4-9-debian:~# mongo 127.0.0.1/admin -u ?????? -p
MongoDB shell version v4.2.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ee412fa7-b774-477e-b086-082da2c35335") }
MongoDB server version: 4.2.6
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
notebooks-alpha 0.000GB
notebooks-production 0.000GB
notebooks-staging 0.000GB
作者

江北饮马、江南折花

发布于

2024-02-12

更新于

2024-02-13

许可协议

评论