【问题标题】:NodeJS + MySQL connects from localhost, but not herokuNodeJS + MySQL 从本地主机连接,但不是 heroku
【发布时间】:2017-02-27 02:26:07
【问题描述】:

我有一个简单的网络应用程序需要连接到我的数据库。我创建了一个名为 public 的用户,它只能从数据库中选择。

从 localhost 一切正常。我在 Heroku 上部署了该应用程序,它给了我以下错误:

2016-10-18T00:17:59.282323+00:00 app[web.1]: 无法连接 :( 错误:连接 ETIMEDOUT 2016-10-18T00:17:59.282562+00:00 app[web.1]:出了点问题...错误:连接 ETIMEDOUT

app.get("/", function(request, response){
    response.render("index");
});

// request for quotes is received
app.get("/get", function(req, res){

    // open a connection to the database
    var connection = mysql.createConnection({
        connectionLimit: 100,
        host: "my-test-database.c3j6vph7cyv2.us-west-2.rds.amazonaws.com",
        user: "public",
        password:"helloworld",
        database:"quotes"
    });

    // connect to the database
    connection.connect(function(error){
        if(error){
            console.log("Couldn't connect :(  " + error);
        } else {
            console.log("Connected successfully~!");
        }    
    });

    // retrieve quotes from database
    connection.query("SELECT * FROM Quotes", function(error, rows, fields){
       if (error) {
           console.log("Something went wrong... " + error);
           res.end();
       } else {
          res.jsonp({"quotes": rows});
       }
    });

    connection.end();
});

【问题讨论】:

    标签: mysql node.js heroku


    【解决方案1】:

    您似乎需要授权您的 Heroku dynos 访问 RDS(从您的代码中的 url 我假设您正在使用它)。检查this link的步骤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2012-01-01
      • 2014-03-26
      • 2013-12-24
      • 2016-10-22
      • 2015-11-28
      • 2019-04-25
      相关资源
      最近更新 更多