Asked  4 Months ago    Answers:  5   Viewed   2.5k times

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 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'

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?



Have you started your XAMPP Server ?.

connect ECONNREFUSED -- 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.

Tuesday, August 17, 2021
Giorgi Peikrishvili
answered 4 Months ago

Had the same issue. Here's what I did.

  1. Open Terminal (/Applications/Utilities/

  2. Use your favorite text editor to create/edit ~/.bash_profile. For instance, if you're using vim, type vim ~/.bash_profile. I use TextMate, so I typed mate ~/.bash_profile.

  3. Add these lines:

    export MAMP_SQL=/Applications/MAMP/Library/bin 
    export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin

    These lines ensure that the first versions of MySQL and PHP that are found (and therefore used) are the versions used by MAMP. NB: Make sure that php5.5.10 matches the version MAMP is using. You can check this against http://localhost:8888/MAMP/index.php?page=phpinfo&language=English by default. SAVE THE FILE (I shouldn't have to save this, but invariably someone complains that it doesn't work).

  4. Close your terminal window and reopen. This restarts the shell, which will load your .bash_profile script.

  5. Type which php. You should get something akin to /Applications/MAMP/bin/php/php5.5.10/bin/php

  6. Type which mysql. You should get something akin to /Applications/MAMP/Library/bin/mysql

If for any reason you get something different than the responses from 5 and 6, go back and check your .bash_profile; chances are you just neglected to save it properly.

Saturday, May 29, 2021
answered 7 Months ago

I have added timezone in index.js when initializing mysql connection

var db_config = {
  host     : 'localhost',
  user     : 'xxx',
  password : '',
  database : 'xxx',
  timezone: 'utc'  //<-here this line was missing
Monday, June 14, 2021
answered 6 Months ago

This worked after changing the string to SQLString for all the connection params like server, username and password.

This worked out like a charm.

Sunday, August 15, 2021
answered 4 Months ago

try something like this , I think you need login and password for sure

some sample code

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',


connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);

Friday, August 20, 2021
Matt Bullock
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :