[英]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以及简单的隧道
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
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.