0

I am very new to javascript and just installed node.js. I have experience with MySQL on windows and linux and am currently running a MySQL server on my Windows machine. I wanted to connect to the server using javascript but I keep getting an error. Everywhere I look I am supposed to add the socketPath variable to the connection. I can't find the socket path on my device.

Here is the code:

const mysql = require('mysql');
const conn  = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '********',
    database: "our_library",
    port: 3306
});

conn.connect((error) => {
    if(error){
        throw error;
        return;
    }
    console.log("Connection established successfully");
});

conn.end((error) => {

});

This is the error message:

Error: connect ECONNREFUSED ::1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
    --------------------
    at Protocol._enqueue (C:\Users\lawso\Documents\PROGRAMMING\MyApps\Web Library\static\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Users\lawso\Documents\PROGRAMMING\MyApps\Web Library\static\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Users\lawso\Documents\PROGRAMMING\MyApps\Web Library\static\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (C:\Users\lawso\Documents\PROGRAMMING\MyApps\Web Library\static\login.js:97:6)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 3306,
  fatal: true
}
astroguy
  • 45
  • 4
  • 1
    Try 127.0.0.1 instead of localhost https://github.com/nodejs/node/issues/40702 – Nick Aug 10 '23 at 02:38
  • This worked! I got a different error that is attributed to [this question](https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server) unfortunately this problem seems to have yet be fixed. It has to do with the new way that mysql authenticates. – astroguy Aug 10 '23 at 02:45
  • Switching to mysql2 worked I just had to run `npm un mysql && npm i mysql2` – astroguy Aug 10 '23 at 02:46

0 Answers0