简体   繁体   English

MongoDB Atlas 和 AWS 之间的连接出现超时错误?

[英]Connection between MongoDB Atlas and AWS giving timeout error?

I'm new to AWS so I apologize for any newbie stuff.我是 AWS 的新手,所以对于任何新手问题我深表歉意。

I'm trying to connect a MongoDB Atlas M0 cluster with our AWS EC2 instance, which is running a nodejs / react stack.我正在尝试将 MongoDB Atlas M0 集群与我们的 AWS EC2 实例连接起来,该实例正在运行 nodejs / react 堆栈。 The problem is that I can't make these two instances connect - AWS and MongoDB that is.问题是我无法连接这两个实例 - AWS 和 MongoDB。 When trying to use the backend sign in function (our nodejs api), it just gives this error:当尝试在 function(我们的 nodejs api)中使用后端登录时,它只是给出了这个错误:

Operation `user_profile.findOne()` buffering timed out after 10000ms

This is our index / connection:这是我们的索引/连接:

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}`);
});

One of the endpoints giving the timeout error:给出超时错误的端点之一:

 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.'
                        });

                    }

                })

        })
    });

The strange thing is that I can connect from my local developer machine, when running our frontend.奇怪的是,当运行我们的前端时,我可以从我的本地开发机器连接。 Just as I can connect from wsl2 ubuntu cli.正如我可以从 wsl2 ubuntu cli 连接一样。

On the Mongo side, I have whitelisted every possible ip address.在 Mongo 方面,我已将所有可能的 ip 地址列入白名单。 On the AWS side, I have created the outbound security group policy required.在 AWS 端,我已经创建了所需的出站安全组策略。 Regarding the inbound, I think it is correct.关于inbound,我觉得是对的。 I've allowed access on the ports 27000 - 28018.我已允许访问端口 27000 - 28018。

Again - I'm new to AWS, so if anyone can tell me what it is I'm simply not understanding here, I would be very grateful再次 - 我是 AWS 的新手,所以如果有人能告诉我它是什么我在这里根本不理解,我将非常感激

Thanks谢谢

open mongodb atlas Network Access open 0.0.0.0/0 (includes your current IP address) open mongodb atlas Network Access open 0.0.0.0/0 (包括你现在的IP地址)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM