Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

554
Views
sql not connecting in node js

I'm trying to connect my database with node js but it is giving me this error . here is my code

//to connect to sql
var mysql=require('mysql');
var conn=mysql.createConnection({
   
    host:"localhost",
    user:"root",
    password:"",
    database:"bns"
    
});
conn.connect(function (err) {
    if (err) {
        console.log(err);
        return;
    }
    console.log("connected");
   conn.query("SELECT * FROM employee", function (err, recordset) {
        if (err) {
            console.log(err);
        }
        else { 
            console.log(recordset);
        }
        conn.close();
    });
});

and I'm getting this error

Error: connect ECONNREFUSED ::1:3306 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) -------------------- at Protocol._enqueue (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\protocol\Protocol.js:144:48) at Protocol.handshake (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\protocol\Protocol.js:51:23) at Connection.connect (C:\Users\DELL\OneDrive\Desktop\node-demo\node_modules\mysql\lib\Connection.js:116:18) at Object. (C:\Users\DELL\OneDrive\Desktop\node-demo\other.js:11:6) 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:81:12) at node:internal/main/run_main_module:17:47 { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '::1', port: 3306, fatal: true } [nodemon] clean exit - waiting for changes before restart

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I've had an exactly same issue.

var conn=mysql.createConnection({

host:"127.0.0.1",
user:"root",
password:"",
database:"bns"
});

try this code. I think it will work.

Now only problem i hav is why it works.. lol

about 4 years ago · Juan Pablo Isaza Report

0

You should specify the port number by which the server has to establish a connection. In your case, you had missed the port number. The code below must eliminate the problem.

var conn=mysql.createConnection({
   
    host:"localhost",
    user:"root",
    password:"",
    port: 3306,
    database:"bns"
    
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!