当前位置: 首页 > 知识库问答 >
问题:

如何让heroku使用数据库的环境配置

仲孙鸿畴
2023-03-14

Heroku在日志中标识它正在尝试访问一个我没有使用的MongoDB碎片(但使用过一次)。错误消息显示:

“无法连接到服务器[Swarmage-Shard-00-00-ekq8j.gcp.MongoDB.net:27017]”

但我不知道它为什么要连接那个服务器。

我正在将node.js应用程序部署到heroku。Heroku链接到我的mlab数据库。我可以通过mongo shell与数据库交互。当我在本地服务器上运行时,postman可以对MLAB数据库进行所有CRUD操作。然而,当我使用邮差中的heroku网址时,我得到一个503错误。日志显示“MongoNetworkError”和“TransitientTransactionError”。但主要的问题,我认为,是它试图伸向一个我不再运行的云图碎片。我的代码中没有连接字符串;它们都是由环境变量设置的。我已经更新并仔细检查了我的本地系统是否设置为新的连接字符串,以及是否为新的连接字符串配置了heroku。我不知道heroku(或者mlab?或者mongoDB?)正在从中获取旧的连接碎片。

我尝试为heroku和本地服务器取消设置配置。我尝试在cluster atlas上找到碎片的旧实例,但无法找到。一切正常,直到我试着用邮差联系到heroku的地址。

我在heroku文档或mongo中找不到任何关于只点击连接字符串并将其放入代码的内容。我还尝试将正确的连接字符串直接放入代码中--没有任何更改。

我想有一个缺省设置,我需要改变,但我不知道在哪里。

我不认为代码会有帮助,但为了显示代码(这一切都在Github上)

生产配置:

{
  "name": "SwarmAge - Production",
  "mail": {
    "host": "prod-mail-server"
  },
  "title": "Welcome to the Swarm Age"

}

自定义-环境-变量

{
  "mail": {
    "password": "SwarmAge_password"
  },
  "jwtPrivateKey": "SwarmAge_jwtPrivateKey",
  "connectionString": "SwarmAge_db"
}

index.js

const express = require("express");
const app = express();
const winston = require("winston");

require("./startup/logging")();
require("./startup/routes")(app);
require("./startup/db")();
require("./startup/config")();
require("./startup/validation")();
require("./startup/prod")(app);
require("./startup/status")(app);

app.set("view engine", "pug");
app.set("views", "./views");

const port = process.env.PORT || 3000;
const server = app.listen(port, () =>
  winston.info(`Listening on port ${port}`)
);

module.exports = server;

启动配置

const config = require("config");

module.exports = function() {
  if (!config.get("jwtPrivateKey")) {
    throw new Error("FATAL ERROR: jwtPrivateKey is not defined.");
  }
};

启动数据库

module.exports = function() {
  const db = config.get("connectionString");
  mongoose
    .connect(db, {
      useNewUrlParser: true,
      useFindAndModify: false
    })
    .then(() => winston.info(`Connected to ${db} . . .`));
};

从终端,heroku配置(转到heroku,它从哪里获得碎片?):-------

C:\Users\tedgo\voter>heroku config

swarmage-backend-190625配置变量

NODE_ENV: production
SwarmAge_db: mongodb://AdminGLOC:fakopassword@ds155461.mlab.com:55461/heroku_6qxb8b19
SwarmAge_jwtPrivateKey: hidden
SwarmAge_password: not-important
jwtPrivateKey: took-this-out-as-well

我的设置(转到Cluster Atlas--这起作用)

SwarmAge_db=mongodb+srv://AdminGLOC:not-my-real-password@swarmage-0idyv.gcp.mongodb.net/development?retryWrites=true

以下是日志中的错误消息:

cat uncaughtExceptions.log{“error”:{“name”:“MongoNetworkError”,“errorLabels”:[“TransitientTransactionError”]},“level”:“error”,“message”:“uncaughtException:第一次连接时连接到服务器[swarmage-shard-00-00-ekq8j.gcp.mongoDB.net:27017]失败[mongoError:错误身份验证失败。]\nmongoNetworkError:第一次连接时连接到服务器node_module\mongoDB-core\lib\auth\scram.js:215:18)\n在connection.messagehandler(C:\users\tedgo\node_module\mongoDB-core\lib\connection\connection.js:334:5)\n在connection.emit(Events.js:189:13)\n在processMessage(C:\users\tedgo\node_module\mongoDB-core\lib\connection\connection.js:364:10)\n在b-core\lib\connection\pool.js:557:14)\n在回调(C:\users\tedgo\node_module\MongoDB-core\lib\connection\connect.js:109:5)\n在provider.auth.err(C:\users\tedgo\node_module\MongoDB-core\lib\connection\connect.js:352:21)\n在

共有1个答案

漆雕深
2023-03-14

这个答案来自编码器圣地亚哥·贝尔特拉姆。-几个问题。
--首先,default.config文件需要保存在custom-environment-variable中找到的所有变量。却没有。--第二,在models.supporters.js中有一个错误,“require('jsonWebToken')”被写成camel大小写“require('jason WebToken')”。这不需要适当的程序。--第三,并非所有依赖项都出现在package.json文件中。因此,heroku不知道包含它们。
通过使用--save标志修复代码并将丢失的依赖项包含在package.json中后,程序成功地部署到heroku。

 类似资料:
  • 我想使用heroku pg:push命令将本地postgresql数据库推送到heroku。命令如下所示: 。 我的应用程序的名称是。我尝试了。输出是: 我很惊讶我没有在数据库中输入任何内容。但我仍然运行heroku pg:reset DATABASE来重置我的数据库。之后,我再次尝试了heroku pg:推送mysitedb数据库——app secure-gorge-4090,但输出仍然相同。

  • 我想用客户端管理我的heroku数据库。到目前为止,我已经使用实现了这一点。当我使用中的数据使用连接de DB时,我获得: 连接到服务器时出错:致命:数据库“Postgres”的权限被拒绝详细信息:用户没有CONNECT特权。 有关于如何实现连接的指导吗?

  • 在xml方式中发布和引用服务的方式如下。 sofa:service 元素表示发布服务, sofa:reference 元素表示引用服务。 sofa:binding 表示服务发布或引用的协议。 <bean id="personServiceImpl" class="com.alipay.sofa.boot.examples.demo.rpc.bean.PersonServiceImpl"/> <so

  • 我想用Visual Studio代码编辑一个Python程序。为了保持可移植性,我希望Python程序包含在Python虚拟环境中。为了保持venv小,我想只安装Python程序需要的东西(特别是不安装VSCode需要的东西)。 我通过在设置中设置“python.pythonPath”变量来告诉VSCode要使用哪个venv。json文件(即我的工作区设置,而不是我的用户设置)。这在我可以运行程序

  • 问题内容: 您将如何在不同的环境中处理跨数据库查询。例如,db1-development和db2-development,db1-production和db2-production。 如果要在从db2到db1的开发中进行跨数据库查询,则可以使用完全限定的名称,即[db1-development]。[schema]。[table]。但是,如何在不同环境之间维护查询和存储过程?[db1-develop

  • 本文向大家介绍Spring Boot使用profile如何配置不同环境的配置文件,包括了Spring Boot使用profile如何配置不同环境的配置文件的使用技巧和注意事项,需要的朋友参考一下 在springboot的开发中,有时候我们会有不同的配置,例如日志打印,数据库连接等,开发,测试,生产每个环境可能配置都不一致,还好,springboot支持通过不同的profile来配置不同环境的配置,