Node-mysql2: Error: connect ECONNREFUSED 127.0.0.1:3007

Created on 25 Oct 2019  路  6Comments  路  Source: sidorares/node-mysql2

Hi,

After a lot of tries to connect to my mysql and mariadb local database.
Before open these issue, I read a lot of documentation and issue on mysql / mysql2 / mariadb packages, but all the time same error (I'm new on Node).

I'm on Windows, my Mysql (3306) and Mariadb (3307) servers are running with WAMP. No problem to connect it for all my PHP Projects.

My node version is v10.13.0.

Here is my try :

let mysql      = require('mysql2'),
    connection = mysql.createConnection({
        host     : process.env.DB_HOST,
        port     : process.env.DB_PORT,
        user     : process.env.DB_USER,
        password : process.env.DB_PASSWORD,
        database : process.env.DB_DATABASE,
    });

connection.query(
    'SELECT * FROM consumerlive_respondent WHERE status=20 LIMIT 10',
    function(err, results, fields) {
        console.log(err);
        console.log(results); // results contains rows returned by server
        console.log(fields); // fields contains extra meta data about results, if available
    }
);

Here is my env file :

DB_HOST=127.0.0.1
DB_PORT=3007
DB_USER=root
DB_PASSWORD=
DB_DATABASE=colorado

With all packages (mysql, mysql2, mariadb), I obtain the following error :

{ Error: connect ECONNREFUSED 127.0.0.1:3007
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3007,
  fatal: true }

I understand that maybe the problem is comming from node and not from mysql2, but how can I fix it ?

I also try with by using socketPath like '\\.\pipe\MySQL' or '/tmp/mariadb.sock' as suggested on mariadb documentation, but this is not working to :

{ Error: connect ENOENT \\.\pipe\MySQL
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'connect',
  address: '\\\\.\\pipe\\MySQL',
  fatal: true }

Thank you for your help,

Ben

Most helpful comment

@picbenoit Thank you for taking the time to post this issue. Thanks to the magic of Google search, i found my way to this thread and found that I, too, had incorrectly typed "3007" instead of "3307". I really appreciate the nudge in the right direction!

All 6 comments

No problem to connect it for all my PHP Projects.

Do you know what connection parameters PHP is using? Your server might listen on file socket only and you use incorrect socket name, or it could be on localhost but listen on specific interface only

Hi,

My PHP projects use classic way to connect to my mariadb & mysql databases : url + port + username + password + database. I never use socket file to connect to my db.

I use also a client tool (HeidiSQL), and I connect my databases with the same way.
This is the first time I see to connect with a socket file (by github issues).

If I need to use socket file, is someone can help me to know where I can find it on Wamp folders ?
I found in my.ini and with a query the way : "/tmp/mariadb.sock" (for maria) or "/tmp/mysql.sock" (for mysql) but it is not a Windows path and these two files not exists anywhere actually.

Thank u,

Ben

Your server need to be configured to listen on unix domain socket or windows pipe, I don't think this is very common on windows ( and I don't have experience myself configuring this )

If your other client can connect with url + port + username + password + database then mysql2 should be able as well, can you double check environment variables are set correctly? Maybe to rule out errors here hard code actual host/port values

Hi @sidorares

Thank you for your answer. I loose more than 1/2 day of work on this issue.
After a new try, I create again my env file and finally working. I saw that I made a mistake on the port that is not "3007" but "3307".

Apologies,

Ben.

No problems, glad you solved it @picbenoit !

@picbenoit Thank you for taking the time to post this issue. Thanks to the magic of Google search, i found my way to this thread and found that I, too, had incorrectly typed "3007" instead of "3307". I really appreciate the nudge in the right direction!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sidorares picture sidorares  路  7Comments

abumusamq picture abumusamq  路  5Comments

HulioEglesias picture HulioEglesias  路  4Comments

SiroDiaz picture SiroDiaz  路  6Comments

MHDante picture MHDante  路  4Comments