【问题标题】:Heroku and NodeJs - MySql Connection lost: The server closed the connectionHeroku 和 NodeJs - MySql 连接丢失:服务器关闭了连接
【发布时间】:2013-08-28 07:02:20
【问题描述】:

我有一个非常简单的 nodeJS 应用程序连接到 MySql ClearDB。第一次运行它与数据库的连接没有问题并执行简单的 SELECT 但几分钟后服务器因数据库失败连接而崩溃。知道为什么出了什么问题吗?

错误: 连接丢失:服务器关闭了连接。

这是我的代码:

web.js

var express = require("express");
var mysql      = require('mysql');
var app = express();
app.use(express.logger());

var connection = mysql.createConnection({
  host     : 'us-cdbr-east-04.cleardb.com',
  user     : 'b6#####',
  password : 'b3f####',
  database : 'heroku_1d########5291'
});

connection.connect();

app.get('/', function(request, response) {
  connection.query('SELECT * from t_users', function(err, rows, fields) {
      if (err) {
        console.log('error: ', err);
        throw err;
      }
      response.send(['Hello World!!!! HOLA MUNDO!!!!', rows]);
    });
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
  console.log("Listening on " + port);
});

过程文件

web: node web.js

package.json

{
  "name": "node-example",
  "version": "0.0.1",
  "dependencies": {
    "express": "3.1.x"
    , "mysql": ""
  },
  "engines": {
    "node": "0.10.x",
    "npm": "1.2.x"
  }
}

日志

Aug 25 12:31:30 polar-beach-8042 heroku/slug-compiler:  Slug compilation finished 
Aug 25 12:31:30 polar-beach-8042 heroku/web.1:  Stopping all processes with SIGTERM 
Aug 25 12:31:33 polar-beach-8042 heroku/web.1:  Process exited with status 143 
Aug 25 12:31:51 polar-beach-8042 app/web.1:  - - - [Sun, 25 Aug 2013 19:31:51 GMT] "GET / HTTP/1.1" 200 131 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 
Aug 25 12:31:51 polar-beach-8042 app/web.1:  - - - [Sun, 25 Aug 2013 19:31:51 GMT] "GET /favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" 
Aug 25 12:31:51 polar-beach-8042 heroku/router:  at=info method=GET path=/favicon.ico host=polar-beach-8042.herokuapp.com fwd="67.86.230.11" dyno=web.1 connect=2ms service=5ms status=404 bytes=34 
Aug 25 12:31:52 polar-beach-8042 heroku/router:  at=info method=GET path=/ host=polar-beach-8042.herokuapp.com fwd="67.86.230.11" dyno=web.1 connect=2ms service=7ms status=200 bytes=131 
Aug 25 12:32:51 polar-beach-8042 app/web.1:  Error: Connection lost: The server closed the connection. 
Aug 25 12:32:51 polar-beach-8042 app/web.1:      at Protocol.end (/app/node_modules/mysql/lib/protocol/Protocol.js:73:13) 
Aug 25 12:32:51 polar-beach-8042 app/web.1:      at Socket.onend (stream.js:79:10) 
Aug 25 12:32:51 polar-beach-8042 app/web.1:      at Socket.EventEmitter.emit (events.js:117:20) 
Aug 25 12:32:51 polar-beach-8042 app/web.1:      at _stream_readable.js:910:16 
Aug 25 12:32:51 polar-beach-8042 app/web.1:      at process._tickCallback (node.js:415:13) 
Aug 25 12:32:52 polar-beach-8042 heroku/web.1:  Process exited with status 8 
Aug 25 12:32:52 polar-beach-8042 heroku/web.1:  State changed from up to crashed 

谢谢!!!!!!!!!

【问题讨论】:

    标签: mysql node.js heroku


    【解决方案1】:

    你似乎已经知道发生了什么; MySQL 服务器正在关闭连接。许多数据库服务,包括 ClearDB,都可以做到这一点(即关闭不活动的连接)。您将不得不检测断开连接事件,并重新创建连接。

    在 node-mysql 文档中有 instructions 用于执行此操作。

    【讨论】:

      【解决方案2】:

      这是因为 ClearDB 上的 MySQL 服务器关闭了连接。您可以检测断开连接事件,然后重新创建连接。

      更多信息在这里:https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/

      const mysql = require("mysql");    
      const dbConfig = require("../config/db.config.js");
          
      var connection = mysql.createPool({
        host: dbConfig.HOST,
        user: dbConfig.USER,
        password: dbConfig.PASSWORD,
        database: dbConfig.DB
      });
      
      module.exports = connection;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-11
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多