0

I have a connection between Node and MySQL with a external database:

const connectionDB = mysql.createConnection({ host: '178.129.145.252', port: 3306, user: '', password: '', database: '*****' })

when I execute my script to start it runs and I can view the users in a table, but later it crashes enter image description here anyone knows how can I connect with the external database? Thanks

1
  • 1
    please add the complete error in the description Commented Jul 31, 2020 at 5:13

1 Answer 1

1

Your code does not properly handle connection loss to MySQL server.

A quick solution would be to use connection pool.

var db_config = {
    host: 'localhost',
    user: 'username',
    password: 'password',
    database: 'example'
};
let pool = mysql.createPool(dbConfig);
pool.on('connection', function (conn) {
    if (conn) {
        logger.info('Connected');
    }
});

// DO SOMETHING HERE
// pool.query('QUERY GOES HERE', (err, rows) => { /* Handle query response */});
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.