简体   繁体   English

连接到 AWS DocumentDB 时出现连接错误

[英]connection error while connecting to AWS DocumentDB

getting the following error while connecting to AWS DocumentDB from node.js从 node.js 连接到 AWS DocumentDB 时出现以下错误

connection error: { [MongoNetworkError: connection 1 to docdb-2019-01-28-06-57-37.cluster-cqy6h2ypc0dj.us-east-1.docdb.amazonaws.com:27017 timed out] name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ] }连接错误:{ [MongoNetworkError: connection 1 to docdb-2019-01-28-06-57-37.cluster-cqy6h2ypc0dj.us-east-1.docdb.amazonaws.com:27017 timed out] name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ] }

here is my node.js file这是我的 node.js 文件

app.js应用程序.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://abhishek:abhishek@docdb-2019-01-28-06-57-37.cluster-cqy6h2ypc0dj.us-east-1.docdb.amazonaws.com:27017/?ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0', {
    useNewUrlParser: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
    console.log("connected...");
});

By default aws documentdb is designed to connect only from same VPC.默认情况下,aws documentdb 设计为仅从同一 VPC 连接。 So to connect nodejs application from an ec2 in same vpc.因此,要从同一 vpc 中的 ec2 连接 nodejs 应用程序。 You need to have the pem file as by default SSL is enabled while db instance is created.您需要拥有 pem 文件,因为在创建 db 实例时默认启用 SSL。

step-1 : $ wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem in required directory第 1 步: $ wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem在所需目录中

step-2 : Change the mongoose connection with options pointing to pem file第 2 步:使用指向 pem 文件的选项更改猫鼬连接

mongoose.connect(database.url, {
    useNewUrlParser: true,
    ssl: true,
    sslValidate: false,
    sslCA: fs.readFileSync('./rds-combined-ca-bundle.pem')})
.then(() => console.log('Connection to DB successful'))
.catch((err) => console.error(err,'Error'));

Here am using mongoose 5.4.0这里使用的是猫鼬 5.4.0

To connnect from outside the VPC, please try to follow the below doc from aws: https://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html要从 VPC 外部连接,请尝试遵循以下来自 aws 的文档: https ://docs.aws.amazon.com/documentdb/latest/developerguide/connect-from-outside-a-vpc.html

Personally I tried only to connect from VPC and it worked fine.就我个人而言,我只尝试从 VPC 连接,并且效果很好。

Update =====:>更新 =====:>

To connect from Robo 3T outside VPC please follow the link - AWS DocumentDB with Robo 3T (Robomongo)要从 VPC 外的 Robo 3T 连接,请点击链接 - AWS DocumentDB with Robo 3T (Robomongo)

to use AWS DocumentDB outside VPC for example your development server EC2 or from the local machine will get a connection error unless you use ssh tunneling or port forwarding在 VPC 外部使用 AWS DocumentDB 例如您的开发服务器 EC2 或从本地计算机将出现连接错误,除非您使用 ssh 隧道或端口转发

and about tunneling it simple以及简单的隧道

  1. use this command in your local在你的本地使用这个命令

    ssh -i "ec2Access.pem" -L 27017:sample-cluster.node.us-east-1.docdb.amazonaws.com:27017 ubuntu@EC2-Host -N ssh -i "ec2Access.pem" -L 27017:sample-cluster.node.us-east-1.docdb.amazonaws.com:27017 ubuntu@EC2-Host -N

  2. in application configuration use在应用程序配置中使用

    { uri: 'mongodb://:@127.0.0.1:27017/Db', useNewUrlParser: true, useUnifiedTopology:true, directConnection: true } { uri: 'mongodb://:@127.0.0.1:27017/Db', useNewUrlParser: true, useUnifiedTopology:true, directConnection: true }

just make sure you can connect from this tunneling ec2 and database只要确保你可以从这个隧道 ec2 和数据库连接

and if you decide to use port forwarding steps 0- in ec2 security grou[p add inbound role with custom TCP and port 27017 All traffic 1- go to your ec2 instance and install Haproxy如果您决定在 ec2 安全组中使用端口转发步骤 0-[p 添加具有自定义 TCP 和端口 27017 的入站角色,所有流量 1- go 到您的 ec2 实例并安装 Haproxy

$ sudo apt install haproxy

2- edit Haproxy configuration 2- 编辑 Haproxy 配置

$ sudo nano haproxy.cfg

3- in end off file add 3-在结束文件添加

listen mongo
bind 0.0.0.0:27017
timeout connect 10s
timeout client 1m
timeout server 1m
mode TCP
server AWSmongo <database-host-url>:27017

4- now restart HaProxy 4- 现在重启 HaProxy

$ sudo service HaPoxy restart

5- now you can access your database using 5- 现在您可以使用访问您的数据库

{uri: 'mongodb://<database-user>:<database-pass>@<EC2-IP>:27017/<db>'}

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

相关问题 从 Docker C# 应用连接到 AWS DocumentDB 的证书错误 - Cert error connecting to AWS DocumentDB from Docker C# app 连接到 PostgreSQL 13.4 时出现 AWS SCT 错误 - AWS SCT error while connecting to PostgreSQL 13.4 AWS EC2:(连接被拒绝)同时连接到上游 - AWS EC2: (Connection refused) while connecting to upstream 在 aws lambda 中通过 pymongo 连接 documentdb 时任务超时 - Task timed out when connecting documentdb through pymongo in aws lambda Django; 44 connect() 在连接到 AWS Elastic Bean 上的上游时失败(111:连接被拒绝) - Django; 44 connect() failed (111: Connection refused) while connecting to upstream on AWS Elastic Bean 来自 Mongo shell 的 SSH 隧道的 AWS DocumentDB ECONNRESET 错误 - AWS DocumentDB ECONNRESET error with SSH tunneling from Mongo shell AWS Lambda 连接 DocumentDb 实例得到 MongoServerSelectionError: getaddrinfo EAI_AGAIN - AWS Lambda connecting DocumentDb instance got MongoServerSelectionError: getaddrinfo EAI_AGAIN ToroDB 是否可用于 aws DocumentDB - Is ToroDB available for aws DocumentDB Orion 是否与 AWS DocumentDB 兼容 - Is Orion compatible with AWS DocumentDB 使用 boto3 或 smart_open 连接到 AWS S3 - 调试连接错误 - connecting to AWS S3 using boto3 or smart_open - debugging connection error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM