Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mysql connection error :Error: Connection lost: The server closed the connection.
- { Error: Connection lost: The server closed the connection.
- at Protocol.end (/home/yeju/server/roomatching/node_modules/mysql/lib/protocol/Protocol.js:112:13)
- at Socket.<anonymous> (/home/yeju/server/roomatching/node_modules/mysql/lib/Connection.js:97:28)
- at Socket.<anonymous> (/home/yeju/server/roomatching/node_modules/mysql/lib/Connection.js:502:10)
- at emitNone (events.js:91:20)
- at Socket.emit (events.js:188:7)
- at endReadableNT (_stream_readable.js:975:12)
- at _combinedTickCallback (internal/process/next_tick.js:80:11)
- at process._tickCallback (internal/process/next_tick.js:104:9)
- --------------------
- at Protocol._enqueue (/home/yeju/server/roomatching/node_modules/mysql/lib/protocol/Protocol.js:144:48)
- at Protocol.handshake (/home/yeju/server/roomatching/node_modules/mysql/lib/protocol/Protocol.js:51:23)
- at Connection.connect (/home/yeju/server/roomatching/node_modules/mysql/lib/Connection.js:118:18)
- at Object.test_open (/home/yeju/server/roomatching/db/db_con.js:17:11)
- at Object.<anonymous> (/home/yeju/server/roomatching/database.js:3:11)
- at Module._compile (module.js:571:32)
- at Object.Module._extensions..js (module.js:580:10)
- at Module.load (module.js:488:32)
- at tryModuleLoad (module.js:447:12)
- at Function.Module._load (module.js:439:3) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
- function handleDisconnect() {
- connection = mysql.createConnection(db_config); // Recreate the connection, since
- // the old one cannot be reused.
- connection.connect(function(err) { // The server is either down
- if(err) { // or restarting (takes a while sometimes).
- console.log('error when connecting to db:', err);
- setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
- } // to avoid a hot loop, and to allow our node script to
- }); // process asynchronous requests in the meantime.
- // If you're also serving http, display a 503 error.
- connection.on('error', function(err) {
- console.log('db error', err);
- if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
- handleDisconnect(); // lost due to either server restart, or a
- } else { // connnection idle timeout (the wait_timeout
- throw err; // server variable configures this)
- }
- });
- }
- module.exports = (function () {
- return {
- local: { // localhost
- host: 'localhost',
- port: '4000',
- user: 'root',
- password: 'password',
- database: 'database'
- },
- real: { // real server db info
- host: '',
- port: '',
- user: '',
- password: '!',
- database: ''
- },
- dev: { // dev server db info
- host: '',
- port: '',
- user: '',
- password: '',
- database: ''
- }
- }
- })();
- var mysql = require('mysql');
- var config = require('../db/db_info').local;
- module.exports = function () {
- return {
- init: function () {
- return mysql.createConnection({
- host: config.host,
- port: config.port,
- user: config.user,
- password: config.password,
- database: config.database
- })
- },
- test_open: function (con) {
- con.connect(function (err) {
- console.log('hi');
- if (err) {
- console.error('mysql connection error :' + err);
- } else {
- console.info('mysql is connected successfully.');
- }
- })
- }
- }
- };
- var mysql_dbc = require('./db/db_con')();
- var connection = mysql_dbc.init();
- mysql_dbc.test_open(connection);
- var express = require('express');
- var router = express.Router();
- var db = require('../database');
Add Comment
Please, Sign In to add comment