无法从 GCP Cloud Run 实例连接到 MongoDB Atlas

问题描述 投票:0回答:1

我按照 this 指南创建静态出站 IP 并将其附加到实例。我已使用以下快速代码片段确认出站 IP 已正确设置,并且出站请求来自该 IP:

const response = await axios.get('https://ipinfo.io/ip');
const externalIP = response.data.trim();
console.log(`Outbound External IP Address: ${externalIP}`);

我还确认了记录的IP与数据库白名单的IP完全相同。但是,我仍然无法连接。与 Mongoose 一样,连接错误没有非常具有描述性的错误,因此我只是收到 SSL 握手失败错误。有什么想法吗?

编辑1:

这是我连接到集群的方式:

const mongoose = require('mongoose');

const uri = process.env.MONGODB_URI;

let connection;
const connect = async () => {
  try {
    connection = await mongoose.connect(uri, {
      bufferCommands: false, // Disable mongoose buffering
    });
    return connection;
  } catch (e) {
    console.error("Could not connect to MongoDB...");
    throw e;
  }
};

function getConnection() {
  return connection;
}

module.exports = { connect, getConnection };

这是错误:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist

error: MongoNetworkError: 08123BD0DA3E0000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1590:SSL alert number 80
google-cloud-platform router mongodb-atlas vpc
1个回答
0
投票

嗨,这只是另一个建议。

您可以尝试利用 Cloud Run 到 MongoDB Atlas 集群的私有 IP 访问来建立连接。此配置可确保 MongoDB 仅通过私有 IP 地址进行连接。此配置将使用 VPC 对等互连。

首先您需要创建一个VPC无服务器访问连接器,请确保创建时您的区域应与无服务器服务相匹配。使用自定义 IP 范围并配置带有 /28 掩码的私有 IP 范围。

请注意,MongoDB Atlas 集群中的此配置使用专用集群进行 VPC 网络对等互连。在 MongoDB atlas 访问列表下的安全部分中,单击网络访问,然后添加 IP 地址。在访问列表条目中,您可以输入您在 VPC 访问连接器下创建的 /28 私有 IP 地址。

接下来是配置 MongoDB Atlas VPC 对等互连。您可以使用此文档作为指导。

为了对等,下一步是在 GCP 上创建 VPC 对等互连。输入为配置 MongoDB Atlas VPC 对等互连提供的项目 ID 和 VPC 网络名称。

下面是下一步需要执行的屏幕截图。有关此建议的详细说明,您可以访问此链接

image

© www.soinside.com 2019 - 2024. All rights reserved.