i've been trying to connect to to the xampp database with the following code:
async function connect(){
if (global.connection && global.connection.state !== 'disconnected')
return global.connection;
const mysql = require("mysql2/promise");
const connection = await mysql.createConnection("mysql://root:root@localhost:3307/crud-almir-lp");
console.log("Connected in mysql!");
global.connection = connection;
return connection;
}
connect();
module.exports = {}
But every time I try to connect to the database, it returns the error:
/mnt/c/xampp/htdocs/crud-almir-lp/node_modules/mysql2/promise.js:242
const createConnectionErr = new Error();
^
Error: connect ECONNREFUSED 127.0.0.1:3307
at Object.createConnection (/mnt/c/xampp/htdocs/crud-almir-lp/node_modules/mysql2/promise.js:242:31)
at connect (/mnt/c/xampp/htdocs/crud-almir-lp/js/db.js:6:36)
at Object.<anonymous> (/mnt/c/xampp/htdocs/crud-almir-lp/js/db.js:11:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/mnt/c/xampp/htdocs/crud-almir-lp/js/index.js:1:12) {
code: 'ECONNREFUSED',
errno: -111,
sqlState: undefined
I tried to add a new password at the database, use express, sequelize, but the error continues. I've also tried to change the defaul port of xampp to 3307, as i show below, but im still having the same error. How to proceed?
Im calling the function in another archive like this:
const db = require('./db');
console.log('Start!');