mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

Node 17.4, MySQL Server 8.0, Mac OS Monterey, ECONNREFUSED with error code -61, ::1:3306 #2547

Closed bhutchins1a closed 2 years ago

bhutchins1a commented 2 years ago

Hi, Guys. Ran into an issue trying to connect with node V 17.4. Plugin is mysql_native_password, user has a password.

JS:

var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'join_us'
});

Get the following error trace:

/Users/david/Projects/mysql_bootcamp/faker/app.js:13
  if (error) throw error;
             ^

Error: connect ECONNREFUSED ::1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
    --------------------
    at Protocol._enqueue (/Users/david/Projects/mysql_bootcamp/faker/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/Users/david/Projects/mysql_bootcamp/faker/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/Users/david/Projects/mysql_bootcamp/faker/node_modules/mysql/lib/Connection.js:116:18)
    at Object.<anonymous> (/Users/david/Projects/mysql_bootcamp/faker/app.js:10:12)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3306,
  fatal: true
}

After agonizing for a while, I couldn't understand the ::1:3306 part of the error message. Changed 'localhost' in the connection object to 127.0.0.1 and it worked. This is with node.js 17.4

Downgraded node.js to 16.X and kept the host in the connection object as 'localhost' and it worked fine.

Any idea what changed between node.js 16.X and 17.4 that would affect the mysql package?

P.S.: There's no binding address in the MySQL server config file.

dougwilson commented 2 years ago

Hi, sorry you had trouble. How TCP connections resolve domain name and the errors is all handled by Node.js, which is why changing the Node.js version changes behavior. This module is not actually involved in that behavior.

bhutchins1a commented 2 years ago

Hi, sorry you had trouble. How TCP connections resolve domain name and the errors is all handled by Node.js, which is why changing the Node.js version changes behavior. This module is not actually involved in that behavior.

Thanks for looking at it so quickly, Doug.

dougwilson commented 2 years ago

No problem. I also checked and in Node.js 17.0 changelog says that the DNS resolution no longer puts IPv4 addresses first, instead they are returned as it comes from the OS. Probably the OS hosts file has ::1 above 127.0.0.1, so Node.js+ will resolve it as IPv6 ::1 instead of 127.0.0.1 on that system.

bhutchins1a commented 2 years ago

Thanks for following up. The Node.js changelog makes it make sense now (awkward sentence structure).

👍👍