1

What's the problem?

  • I'm using node-mysql to connect to mysql.
  • I have a really hard time dealing with the server disconnects / wait_timeouts as mentioned here: https://github.com/felixge/node-mysql#server-disconnects
  • I receive the error message: This socket has been ended by the other party every time after I'm trying to recreate the connection upon handling a PROTOCOL_CONNECTION_LOST error.

What I'm trying to do

// Connect Function
db.connect = function(callback){

    // Destory the Connection if there is already one
    if(db.connection) {
        console.log('[mysql]','connection destroy');
        db.connection.destroy();
        db.connection = null;

    }

    // Create the Connection
    db.connection = MySQL.createConnection(options);

    // Connect using the Connection
    db.connection.connect(function(error) {
        if(error){ 
            console.log('[mysql]', 'connect failed', error);
        } else {
            console.log('[mysql]', 'connection success');
            db.connection.database = options.database;
            callback(db.connection, error);
        }
    });

    // Listen on Connection Errors
    db.connection.on('error', function(error) {
        // Connection to the MySQL server is usually
        // lost due to either server restart, or a
        // connnection idle timeout (the wait_timeout server variable configures this)
        if(error.code === 'PROTOCOL_CONNECTION_LOST') { 
            console.log('[mysql]', 'PROTOCOL_CONNECTION_LOST')
            db.connect(callback);
        } else {                                    
            console.log('[mysql] Connection Error: ', error);               
        }
    });

    // Return Connection Instance
    return db.connection;
}

Additional Details

wait_timeout and interactive_timeout has to be set around 10 seconds in my.cnf to test the issue.

[mysqld]
wait_timeout = 10
interactive_timeout = 10

1 Answer 1

1

The library is accidentally trying to write over the TCP socket after a FIN packet has been received, which means the TCP connection is half-closed by the other side. I'll fix this to give you a better error message, but it's still an error; either your network is killing your MySQL connections or something.

Are you using the connection pool? It looks like you are just making a query on a connection after some timeout according to the stack trace. It's possible you are holding onto an inactive connection too long and something on the network or MySQL server is ending the connection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.