【发布时间】:2023-01-18 23:48:01
【问题描述】:
我是 AWS 的新手,所以对于任何新手问题我深表歉意。
我正在尝试将 MongoDB Atlas M0 集群与我们运行 nodejs / react 堆栈的 AWS EC2 实例连接起来。问题是我无法连接这两个实例——AWS 和 MongoDB。当尝试使用后端登录功能(我们的 nodejs api)时,它只是给出了这个错误:
Operation `user_profile.findOne()` buffering timed out after 10000ms
这是我们的索引/连接:
import config from './config';
import app from './app';
import { connect } from 'mongoose'; // MongoDB
import { ServerApiVersion } from 'mongodb';
import https from 'https';
import AWS from 'aws-sdk';
const makeLogger = (bucket: string) => {
const s3 = new AWS.S3({
accessKeyId: <ACCESS_KEY_ID>,
secretAccessKey: <SECRET_ACCESS_KEY>
});
return (logData: any, filename: string) => {
s3.upload({
Bucket: bucket, // pass your bucket name
Key: filename, // file will be saved as testBucket/contacts.csv
Body: JSON.stringify(logData, null, 2)
}, function (s3Err: any, data: any) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
});
console.log(`log (${filename}): ${logData}`);
};
};
const log = makeLogger('xxx-xxxx');
log(config.MONGO_DB_ADDRESS, 'mongo_db_address.txt');
const credentials = <CREDENTIALS>
connect(config.MONGO_DB_ADDRESS, {
sslKey: credentials,
sslCert: credentials,
serverApi: ServerApiVersion.v1
}) //, { useNewUrlParser: true })
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('Failed connection to MongoDB', err));
app.on('error', error => {
console.error('app error: ' + error);
});
app.listen(config.WEB_PORT, () => {
console.log(`Example app listening on port ${config.WEB_PORT}`);
});
给出超时错误的端点之一:
router.post('/signin', async (req, res) => {
var form_validation = signin_schema.validate({
email: req.body.email,
password: req.body.password,
});
if (form_validation.error) {
console.log('form validation sent');
//return res.status(400).send(form_validation);
return res.status(400).send({
kind: 'ERROR',
message: 'Sorry - something didn\'t go well. Please try again.'
});
}
var User = model('model', UserSchema, 'user_profile');
User.findOne({ email: req.body.email }, (err: any, the_user: any) => {
if (err) {
return res.status(400).send({
kind: 'ERROR',
message: err.message
});
}
if (!the_user) {
return res.status(400).send({
kind: 'ERROR',
message: 'the_user undefined',
});
}
compare(req.body.password, the_user.password)
.then((result) => {
if (result == true) {
const user_payload = { name: the_user.name, email: the_user.email };
const access_token = sign(user_payload, config.SECRET_TOKEN);
res.cookie('authorization', access_token, {
httpOnly: true,
secure: false,
maxAge: 3600000,
});
return res.send({ kind: "LOADING" });
// return res.send(access_token);
} else {
return res.status(400).send({
kind: 'ERROR',
message: 'Sorry - wrong password or email used.'
});
}
})
})
});
奇怪的是,在运行我们的前端时,我可以从我的本地开发机器连接。就像我可以从 wsl2 ubuntu cli 连接一样。
在 Mongo 方面,我已将每个可能的 IP 地址列入白名单。在 AWS 端,我已经创建了所需的出站安全组策略。关于inbound,我觉得是对的。我已允许访问端口 27000 - 28018。
再次 - 我是 AWS 的新手,所以如果有人能告诉我它是什么我在这里根本不理解,我将非常感激
谢谢
【问题讨论】:
-
是的,我是——目前正在从猫鼬进口。
-
不能使用 mongoose 吗?
-
我认为您的连接方式存在问题。
-
你对这可能是什么有什么建议吗?我已经研究了好几天了,我就是想不通。
标签: mongodb amazon-web-services amazon-ec2