mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Error: Connection lost: The server closed the connection.

Open herdma opened this issue 5 years ago • 3 comments

I'm trying to run the following code:

const mysql = require('mysql');

const dbPool = mysql.createPool({
  connectionLimit: 10,
  host: '10.1.2.3', // a different server
  user: 'user',
  password: 'pw',
  database: 'database',
});

dbPool.query('SELECT * FROM database.accounts;', (error, results, fields) => {
  console.log('Error:', error);
  console.log('Results: ', results?.[0]);
});

This works just fine on my laptop and prints the data I am expecting. Node Version: v14.16.0 Windows 10 Version 20H2

But when I try to run the exact same code on a server, it fails instantly Node Version: v14.16.0 SUSE Linux Enterprise Server 12 (x86_64)

Error: Error: Connection lost: The server closed the connection. at Protocol.end (/root/testmysql/node_modules/mysql/lib/protocol/Protocol.js:112:13) at Socket. (/root/testmysql/node_modules/mysql/lib/Connection.js:94:28) at Socket. (/root/testmysql/node_modules/mysql/lib/Connection.js:526:10) at Socket.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) -------------------- at Protocol._enqueue (/root/testmysql/node_modules/mysql/lib/protocol/Protocol.js:144:48) at Protocol.handshake (/root/testmysql/node_modules/mysql/lib/protocol/Protocol.js:51:23) at PoolConnection.connect (/root/testmysql/node_modules/mysql/lib/Connection.js:116:18) at Pool.getConnection (/root/testmysql/node_modules/mysql/lib/Pool.js:48:16) at Pool.query (/root/testmysql/node_modules/mysql/lib/Pool.js:202:8) at Object. (/root/testmysql/index.js:12:8) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) { fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } Results: undefined

What am I doing wrong?

It shouldn't be a firewall issue:

$ nc -zv 10.1.2.3 3306 Connection to 10.1.2.3 3306 port [tcp/mysql] succeeded!

herdma avatar Apr 14 '21 12:04 herdma

Very strange. I can say from the error, that just like you demonstrated with nc, the TCP connection is established just fine. The error you are seeing from this library is that, after your TCP connectioj has been opened, a packet was received with either FIN or RST set, but you didn't make any API calls to end the connection. This us what it means by the server closed the connection.

Of course the question as to why the server closed your open connection on you is the mystery. Perhaps there is something in the mysql server logs to indicate why? Perhaps if you could include a packet capture of the transaction I can confirm that this module did indeed see the connection close, though that doesn't answer the why, only confirms the issue is outside of this module, somewhere in the environment.

dougwilson avatar Apr 14 '21 15:04 dougwilson

We don't actually see anything in the MySQL logs, but we found the following in /var/log/messages:

mysqld[2960]: refused connect from 10.11.22.33

But the connection is possible from other servers. We set the bind_address to 0.0.0.0 and allowed connections for the user:

grant insert,select,delete,update on database.* to user@'%' identified by '....'

Its a MariaDB server, is that an issue? But then again, it works just fine from my laptop. Do you have any idea if another MySQL setting could be the issue?

herdma avatar Apr 21 '21 11:04 herdma

We fixed it by allowing the host in /etc/hosts.allow :)

herdma avatar Apr 26 '21 09:04 herdma