本地無法用nodejs連接雲端服務器的mongodb服務器

来源:6-6 使用Node链接MongoDB

LydiaSo

2023-10-13

老師你好,我的mongodb是安裝到aws的EC2上了,並不是在本地。然後index.js的文件就是在本地的,我把localhost改成自己服務器的ip地址,卻說連接超時,請問怎麼回事。

報錯信息如下:
/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/sdam/topology.js:278
const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${serverSelectionTimeoutMS} ms, this.description);
^

MongoServerSelectionError: connection timed out
at Timeout._onTimeout (/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/sdam/topology.js:278:38)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
reason: TopologyDescription {
type: ‘Unknown’,
servers: Map(1) {
‘184.73.61.141:27017’ => ServerDescription {
address: ‘184.73.61.141:27017’,
type: ‘Unknown’,
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 113296477,
lastWriteDate: 0,
error: MongoNetworkTimeoutError: connection timed out
at connectionFailureError (/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/cmap/connect.js:381:20)
at Socket. (/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/cmap/connect.js:285:22)
at Object.onceWrapper (node:events:628:28)
at Socket.emit (node:events:514:28)
at Socket._onTimeout (node:net:571:8)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
[Symbol(errorLabels)]: Set(2) { ‘ResetPool’, ‘InterruptInUseConnections’ },
[cause]: undefined
},
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
’$clusterTime’: null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {},
[cause]: MongoNetworkTimeoutError: connection timed out
at connectionFailureError (/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/cmap/connect.js:381:20)
at Socket. (/Users/lydiaso/Documents/study/node/5.mongoDB/node_modules/mongodb/lib/cmap/connect.js:285:22)
at Object.onceWrapper (node:events:628:28)
at Socket.emit (node:events:514:28)
at Socket._onTimeout (node:net:571:8)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
[Symbol(errorLabels)]: Set(2) { ‘ResetPool’, ‘InterruptInUseConnections’ },
[cause]: undefined
}
}

Node.js v18.17.0

写回答

1回答

北瑶

2024-12-30

MongDB数据默认情况下是没有用户名密码的,为了保证安全性,只能在本地(localhost、127.0.0.1)连接,如果想要远程连接数据库,需要做的修改:

1: 先给MongoDB配置好用户名密码,

2: 修改 MongoDB 的配置文件 允许远程访问 

3:然后再远程的服务器上配置防火墙开放 MongoDB的访问端口(27017)。

4:重启MongoDB服务,

这样就可以了。


配置MongoDB用户名和密码:

创建账户密码: db.createUser({user:'you-name',pwd:'you-pwd',roles:[{role:'root',db:'admin'}]})

/etc/mongod.conf
# 开启远程链接
# network interfaces
net:
 port: 27017
 bindIp: 0.0.0.0

# 开启身份认证
security:
 authorization: enabled

客户端远程链接: mongosh -u you-name -p you-pwd  IP_address:27017/admin



0
0

Node.js工程师养成计划 保姆级教程 快速入门实战收尾

保姆级教程,从0起步,吃透Node完整技术体系

257 学习 · 94 问题

查看课程