node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

sometime my server is crashed and down (can't connect SSH, need restart ec2 instance)

Open dancinlife opened this issue 3 years ago • 1 comments

sometime my server is crashed and down (can't connect SSH, need restart ec2 instance)

what's wrong my code

PM2 Log

2022-07-20T02:16:06: GET /article/1537 - - ms - -
2022-07-20T02:16:11: GET /article/1527 - - ms - -
2022-07-20T02:16:11: GET /article/1527 - - ms - -
2022-07-20T02:16:11: GET /article/1527 - - ms - -
2022-07-20T02:16:11: GET /article/1324 - - ms - -
2022-07-20T02:16:11: GET /article/1619 - - ms - -
2022-07-20T02:16:11: GET /article/1619 - - ms - -
2022-07-20T02:16:11: GET /article/1468 500 64843.785 ms - 28
2022-07-20T02:16:13: GET /article/1468 500 65710.830 ms - 28
2022-07-20T02:16:13: GET /article/1440 500 65720.501 ms - 28
2022-07-20T02:16:18: GET /article/1619 - - ms - -
2022-07-20T02:16:18: GET /article/1313 - - ms - -
2022-07-20T02:16:18: GET /article/1600 - - ms - -
2022-07-20T02:16:18: GET /article/1422 - - ms - -

Database Middleware

const mysql = require('mysql2/promise');
const config = require('./config');

const sql = config.getDatabase();

const { host, user, password, port, database, connectionLimit, waitForConnections, queueLimit } = sql;

const pool = mysql.createPool({
  host,
  user,
  password,
  port,
  database,
  timezone: 'utc',
  connectionLimit: connectionLimit || 10,
  waitForConnections: waitForConnections || true,
  queueLimit: queueLimit || 0,
});

// Ping database to check for common exception errors.
pool.getConnection((err, connection) => {
  if (err) {
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
      console.error('Database connection was closed.');
    }
    if (err.code === 'ER_CON_COUNT_ERROR') {
      console.error('Database has too many connections.');
    }
    if (err.code === 'ECONNREFUSED') {
      console.error('Database connection was refused.');
    }
  }

  if (connection) connection.release();

  return;
});

pool.on('error', (err) => {
  console.error('Database Error');
  console.error(err);
});

module.exports = pool;

Query Example

const pool = require('../middleware/database');

const doAsync = fn => async (req, res, next) => await fn(req, res, next).catch(next);

exports.example = doAsync(async (req, res, next) => {
  const conn = await pool.getConnection();
  try {
    const [boards, ] = await conn.query(`SELECT * FROM board`);
    const [articles, ] = await conn.query(`SELECT * FROM article`);
    const [comments, ] = await conn.query(`SELECT * FROM comment`);
    res.send({
      boards,
      articles,
      comments,
    });
  } finally {
    conn.release();
  }
});

App.js

app.use(timeout('60s'));

// Error Handling
app.use('*', (err, req, res, next) => {
  console.error(err);
  if (err.name === 'TypeError' || err.name === 'ReferenceError') {
    res.status(500).json({
      error: 'EJS Rendering Error',
    });
  } else if (err.name === 'ServiceUnavailableError') {
    res.status(500).json({
      error: `Database Unavailable Error`,
    });
  } else if (err.name === 'URIError') {
    res.status(500).json({
      error: `URI Decode Error`,
    });
  } else {
    res.status(500).json({
      error: err.message,
    });
  }
});

dancinlife avatar Jul 20 '22 17:07 dancinlife

error handling looks ok to me, I'm afraid I need more information to debug. Might be not directly related to mysql2 driver ( instance out of memory ? node process taking 100% of cpu? ) What are your memory/cpu graphs look like?

sidorares avatar Aug 02 '22 23:08 sidorares