我正在尝试使用NodeJS和Express连接运行在VPS上的Mongo数据库。这是我的代码:
我希望它打印“已连接到数据库”,但实际上它抛出了以下异常:
const app = express();
const MongoClient = require('mongodb').MongoClient
const connectionString = "mongodb://[Username]:[Password]@[IP Address]:[Port Number]/[Database]"
MongoClient.connect(connectionString, { useUnifiedTopology: true }, (err, client) => {
if (err) return console.error(err)
console.log('Connected to Database')
})
app.get('/', function(req, res) {
res.send('Hello World')
})
app.listen(3000, function() {
console.log('listening on 3000')
})
我希望它打印“已连接到数据库”,但实际上它抛出了以下异常:
MongoServerSelectionError: connection timed out
at Timeout._onTimeout (/home/[user]/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
reason: TopologyDescription {
type: 'Unknown',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { '[IP Adress]:[Port Number]' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
我成功地通过浏览器访问了IP地址。这使我进入了Apache Web服务器的默认索引页面。
在选择我的数据库后,在mongo
终端内运行show users
命令会得到以下响应:
{
"_id" : "[Database].[Username]",
"userId" : UUID("1545d0ec-1309-4e40-82e4-7777afb266fe"),
"user" : [Username],
"db" : [Database],
"roles" : [
{
"role" : "readWrite",
"db" : [Database]
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
我该采取哪些适当的步骤来继续调试?我需要提供更多信息吗?