title: Reproduce MySQL error: The server closed the connection (node.js)
body: <p>I'm trying to reproduce a MySQL error I'm seeing in my node.js app on EC2 with the <a href="https://github.com/felixge/node-mysql">node mysql library</a>:</p>
  
  <blockquote>
    <p>Connection lost: The server closed the connection.</p>
  </blockquote>
  
  <p>I  am unable to reproduce the error locally- killing the database is handled just fine by my code- it just rechecks every few seconds and reconnects to the db once it is restarted. On EC2, it happens around 4am Pacific, but the db is still up and running fine.</p>
  
  <p>I'd like to</p>
  
  <ol>
  <li>Reproduce the crash with my local mysql</li>
  <li>Add whatever logic I need in my mysql helper module to handle this</li>
  </ol>
  
  <hr>
  
  <p>Here's the error in my node.js app:</p>
  
  <blockquote>
    <p>2012-10-22T08:45:40.518Z - error: uncaughtException date=Mon Oct 22
    2012 08:45:40 GMT+0000 (UTC), pid=14184, uid=0, gid=0,
    cwd=/home/ec2-user/my-app, execPath=/usr/bin/nodejs,
    version=v0.6.18, argv=[/usr/local/bin/node,
    /home/ec2-user/my-app/app.js, --my-app], rss=15310848,
    heapTotal=6311392, heapUsed=5123292, loadavg=[0.0029296875,
    0.0146484375, 0.04541015625], uptime=3238343.511107486, trace=[column=13,
    file=/home/ec2-user/my-app/node_modules/mysql/lib/protocol/Protocol.js,
    function=Protocol.end, line=63, method=end, native=false, column=10,
    file=stream.js, function=Socket.onend, line=80, method=onend,
    native=false, column=20, file=events.js, function=Socket.emit,
    line=88, method=emit, native=false, column=51, file=net.js,
    function=TCP.onread, line=388, method=onread, native=false],
    stack=[Error: Connection lost: The server closed the connection.,<br>
    at Protocol.end
    (/home/ec2-user/my-app/node_modules/mysql/lib/protocol/Protocol.js:63:13),     at Socket.onend (stream.js:80:10),     at Socket.emit
    (events.js:88:20),     at TCP.onread (net.js:388:51)]</p>
  </blockquote>
  
  <p>Here's my code (mysql helper module):</p>
  
  <pre><code>module.exports = function (conf,logger) {
    var mysql = require('mysql');
  
    var connectionState = false;
    var connection = mysql.createConnection({
      host: conf.db.hostname,
      user: conf.db.user,
      password: conf.db.pass,
      database: conf.db.schema,
      insecureAuth: true
    });
  
    function attemptConnection(connection) {
      if(!connectionState){
        connection = mysql.createConnection(connection.config);
        connection.connect(function (err) {
          // connected! (unless `err` is set)
          if (err) {
            logger.error('mysql db unable to connect: ' + err);
            connectionState = false;
          } else {
            logger.info('mysql connect!');
            connectionState = true;
          }
        });
        connection.on('close', function (err) {
          logger.error('mysqldb conn close');
          connectionState = false;
        });
        connection.on('error', function (err) {
          logger.error('mysqldb error: ' + err);
          connectionState = false;
  
          /*
          if (!err.fatal) {
            return;
          }
          if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
            throw err;
          }
          */
        });
      }
    }
    attemptConnection(connection);
  
    var dbConnChecker = setInterval(function(){
      if(!connectionState){
        logger.info('not connected, attempting reconnect');
        attemptConnection(connection);
      }
    }, conf.db.checkInterval);
  
    return connection;
  };
  </code></pre>