简体   繁体   English

从 Google Cloud Functions 控制台访问 MongoDB Atlas 集群

[英]Accessing a MongoDB Atlas Cluster from within Google Cloud Functions Console

I'm writing a basic Google Cloud Function that is going to query a MongoDB Cluster from MongoDB Atlas.我正在编写一个基本的谷歌云函数,它将从 MongoDB Atlas 查询 MongoDB 集群。 I'm writing inside the Google Console and I was sure to add "mongodb": "^3.0.2" to the dependencies in the package.json file.我正在 Google 控制台中编写内容,并且确定将 "mongodb": "^3.0.2" 添加到 package.json 文件中的依赖项。

Here's the function (I replaced the valid password, etc. in the uri for security):这是函数(为了安全起见,我在 uri 中替换了有效密码等):

/**
* Responds to any HTTP request that can provide a "message" field in the 
body.
 *
 * @param {!Object} req Cloud Function request context.
 * @param {!Object} res Cloud Function response context.
 */
exports.myPackage = (req, res) => {    
  var MongoClient = require('mongodb').MongoClient;
  var uri = "mongodb+srv://<USERNAME>:<PASSWORD>@<CLUSTER-NAME>-vtbhs.mongodb.net/test";
  MongoClient.connect(uri, function(err, client) {
    if (err) {
      console.log('err');
      console.log(err);
    } else {
      const collection = client.db("test").collection("devices");
    }
  });

  res.status(200).send('Success');
}

I'm sure the driver is up to date, and I copied most of this code directly from the Atlas docs.我确定驱动程序是最新的,并且我直接从 Atlas 文档中复制了大部分代码。 I've whitelisted all IPs from Atlas for testing.我已将 Atlas 的所有 IP 列入白名单以进行测试。

Every time the function runs, I get the following error in the connect callback:每次函数运行时,我都会在连接回调中收到以下错误:

"{ Error: querySrv ESERVFAIL _mongodb._tcp.<CLUSTER-NAME>-vtbhs.mongodb.net
at errnoException (dns.js:28:10)
at QueryReqWrap.onresolve [as oncomplete] (dns.js:219:19)
code: 'ESERVFAIL',
errno: 'ESERVFAIL',
syscall: 'querySrv',
hostname: '_mongodb._tcp.<CLUSTER-NAME>-vtbhs.mongodb.net' }"

I also previously got an error like:我之前也遇到过类似的错误:

URI does not have hostname, domain name and tld at module.exports

Although that doesn't seem to be popping up anymore since I adjusted my password inside of Mongo (there may have been a non-html-encoded char in it).尽管自从我在 Mongo 内部调整了密码后,这似乎不再出现了(其中可能有一个非 html 编码的字符)。

Thanks in advance for any help!在此先感谢您的帮助!

I had the exact same issue.我有完全相同的问题。 It was failing for both MongoDB Atlas and mLab connections after running firebase deploy , but working locally using firebase serve .运行 firebase firebase deploy ,MongoDB Atlas 和 mLab 连接均失败,但使用firebase serve在本地工作。

I believe there are two problems:我认为有两个问题:

  1. Outbound Networking is disabled on the free Spark plan.免费的 Spark 计划禁用了出站网络。 You have to upgrade to the Blaze Plan (pay-as-you-go) in order to do that.您必须升级到 Blaze 计划(即用即付)才能做到这一点。 I just switched tiers and it works now.我刚换了层,现在可以用了。

The Spark plan allows outbound network requests only to Google-owned services. Spark 计划只允许对 Google 拥有的服务发出出站网络请求。 Inbound invocation requests are allowed within the quota.配额内允许入站调用请求。 On the Blaze plan, Cloud Functions provides a perpetual free tier.在 Blaze 计划中,Cloud Functions 提供永久免费层。 The first 2,000,000 invocations, 400,000 GB-sec, 200,000 CPU-sec, and 5 GB of Internet egress traffic is provided for free each month.每月免费提供前 2,000,000 次调用、400,000 GB 秒、200,000 CPU 秒和 5 GB 的 Internet 出口流量。 You are only charged on usage past this free allotment.您只需为超过此免费配额的使用量付费。 Pricing is based on total number of invocations, and compute time.定价基于调用总数和计算时间。 Compute time is variable based on the amount of memory and CPU provisioned for a function.计算时间根据为函数配置的内存和 CPU 量而变化。 Usage limits are also enforced through daily and 100s quotas.使用限制也通过每日和 100 秒配额强制执行。 For more information, see Cloud Functions Pricing.有关更多信息,请参阅 Cloud Functions 定价。

https://firebase.google.com/pricing/ https://firebase.google.com/pricing/

You have to give a credit card, but as long as you remain under their data quota you don't get charged, apparently.你必须提供信用卡,但只要你保持在他们的数据配额内,你就不会被收费,显然。 I will try it out and see how it goes.我会尝试一下,看看效果如何。

  1. After upgrading, your MongoDB Atlas connection might still be failing (while an mLab string would work).升级后,您的 MongoDB Atlas 连接可能仍然失败(而 mLab 字符串可以工作)。 This is because your query URI is using the Mongo 3.6 SRV address, rather than the Mongo 3.4 driver's "mongodb://" protocol.这是因为您的查询 URI 使用的是 Mongo 3.6 SRV 地址,而不是 Mongo 3.4 驱动程序的“mongodb://”协议。 Try switching the driver version and see if it works.尝试切换驱动程序版本,看看它是否有效。 在此处输入图片说明

Try by issuing HTTP requests to query MongoDB . 尝试发出HTTP请求查询MongoDB Cloud Functions allow only HTTP/S outbound traffic. 云功能仅允许HTTP / S出站流量。

在 Mongo Atlas 面板的白名单中设置“0.0.0.0/0”。

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

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