I can’t connect to my local MySQL Server through Javascript.
I tried to connect to my mySQL instance with following code:
const mysql = require("mysql2");
// create a new MySQL connection
const connection = mysql.createConnection({
host: "localhost",
user: "js",
password: "js_sql",
});
// connect to the MySQL database
connection.connect((error) => {
if (error) {
console.error("Error connecting to MySQL database:", error);
} else {
console.log("Connected to MySQL database!");
}
});
// close the MySQL connection
connection.end();
Output:
Error connecting to MySQL database: Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
If i try to connect with the same credentials in my workbench, it works.
PS: My project folder is in my WSL!
New contributor
fr4xx is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.