Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "connect ECONNREFUSED 127.0.0.1:3306" while connecting to mysql

Tags:

node.js

mysql

I'm new to nodeJs and trying to develop my first application. I installed mysql package through npm, It got installed and I can see a successful entry of it in package.json. However, when I'm trying to connect to mysql server, it gives me this error connect ECONNREFUSED 127.0.0.1:3306. I searched for this issue on StackOverflow and the other users who had the very same issue got it running by adding an entry socketPath: '/var/run/mysqld/mysqld.sock' to the configuration object. But when I added this to my configuration object, I got a new error connect ENOENT /var/run/mysqld/mysqld.sock.

Here's the code by which I'm trying to connect to the server

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'blog',
    socketPath: '/var/run/mysqld/mysqld.sock'
});

connection.connect();
module.exports = router;

I'm using a Windows machine and had mysql installed before via XAMPP package. Can this be the reason behind the connection failure?

like image 772
Kartik Chauhan Avatar asked Mar 20 '17 09:03

Kartik Chauhan


1 Answers

Have you started your XAMPP Server ?.

connect ECONNREFUSED 127.0.0.1:3306 -- this error came, when you not started your XAMPP server.

Please remove socketPath & try once.

I have faced same kind of issue, when I not started the XAMPP server.

like image 142
Bhaurao Birajdar Avatar answered Nov 08 '22 02:11

Bhaurao Birajdar