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
}