Nico
Nico

Reputation: 749

Node.js MySQL - Error: connect ECONNREFUSED

I use Node.js server side. I tried my code on localhost and everything works fine. I bought a server and installed Apache and node.js on it and test my web application there. I correctly changed the MySQL connection configurations from localhost to the server configurations.

I test my web application with this configuration:

var mysql      = require('mysql');
var mysqlConnection;
function new_mysqlConnection() {
    mysqlConnection = mysql.createConnection({
      host     : 'myurl.at',
      user     : 'myusername',
      database : 'mydatabase',
      password : 'mypassword'
    });
}

I start the node.js server with:

$ node server.js

When I load the page, it's correctly displayed, but when Node.js try to connect to the database I always got the following error:

Error: connect ECONNREFUSED
    at errnoException (net.js:905:11)
    at Object.afterConnect [as oncomplete] (net.js:896:19)
    --------------------
    at Protocol._enqueue (/var/www/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/var/www/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/var/www/node_modules/mysql/lib/Connection.js:119:18)
    at reconnectDb (/var/www/server.js:319:18)
    at app.get.email (/var/www/server.js:109:2)
    at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
    at trim_prefix (/var/www/node_modules/express/lib/router/index.js:302:13)
    at /var/www/node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/var/www/node_modules/express/lib/router/index.js:321:12)
    at next (/var/www/node_modules/express/lib/router/index.js:261:10)

Upvotes: 45

Views: 249172

Answers (21)

Pete
Pete

Reputation: 91

Re: the question about localhost vs 127.0.0.1 (I don't have enough reputation to add a comment :-( ) - https://stackoverflow.com/a/53533120/1804408

I had the same problem, I had host: "localhost" and fixed it by using "127.0.0.1".

I was getting the error Error: connect ECONNREFUSED ::1:3306 - I eventually realised that it was reporting the IPv6 local address. Even though I had the MySQL user defined with both @'%' and @'localhost' it looks like MySQL didn't like the IPv6 local address - probably something to do with my MySQL set up.

Upvotes: 7

medium ju
medium ju

Reputation: 9

In my case, I downloaded XAMPP and checked for my username and password, and then it finally worked.

XAMPP > mySQL connected > phpmyadmin > user accounts: to see your account details!

Hope this helps.

Upvotes: 1

In my case it was working locally, but now when deployed. The problem was I hadn't updated the environment variables

Upvotes: 0

Dilshod Sh
Dilshod Sh

Reputation: 301

Maybe it will help someone, In my case I am using strapi app when I encountered this issue, I realized that I did not create database, So I am using Mamp I go to php my admin created db and updated database.js in config folder and now its working fine

Upvotes: 0

Luqm
Luqm

Reputation: 31

if using using express and MySQL. port should be same as your MySQL port.
The default MySQL port is 3306.

let defaultPORT = 3306;
var express = require('express');
var app = express();
var mysql  = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'yourMYSQLUser',
    password : 'yourMYSQLPassword'
});
connection.connect();
 
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
    if (error) throw error;
    console.log('The solution is: ', results[0].solution);
});

var server = app.listen( defaultPORT, function() {
    console.log( defaultPORT)
});

Upvotes: 3

Bala
Bala

Reputation: 1

I created a new user in PhpMyAdmin with a username as "user" and password as "". I set the host as "%" which allows connection from any host.

Use the IP address you find in the XAMPP control panel as the host in the node js code

I have used the IP address in the XAMPP control panel as the host in the database connection

This solved the problem and connected to the database

Upvotes: 0

Williams Ebrechi
Williams Ebrechi

Reputation: 1

I had same issues, and i resolved it by adding my port

Port: YOUR_PORT

had this same issue and I solved it by adding the port

Upvotes: 0

Tech_Sarfu
Tech_Sarfu

Reputation: 11

I am facing the same problem but I have done that:

  1. Turn ON your MySQL using XAMPP
  2. Create a database that will mention in the code. (I have 'node js' named database.

By doing these, my code is worked perfectly.

You can see here

Upvotes: -1

tomyhomie
tomyhomie

Reputation: 372

have experienced a similar issue where with the same configs, could connect from local -> mysql server but not when deployed as cronjob to k8s cluster. Not sure why but workaround was to add sleep 10 && before running application: args: ["-c", 'sleep 10 && node index.js']

Upvotes: 0

toujames
toujames

Reputation: 465

I had the same issue turns out mysql was not running. Make sure it's running by:

$ systemctl status mysqld

If you see this:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since Tue 2020-08-18 12:12:29 CDT; 32min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18668 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS (code=exited, status=2)
  Process: 18650 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18668 (code=exited, status=2)
   Status: "SERVER_BOOTING"

Then the service is not running, to run enter:

$ systemctl start mysqld

Upvotes: 1

janithcooray
janithcooray

Reputation: 308

In most cases, your host should match your bind-address in the MySQL config file. if your bind-address is 0.0.0.0 or 127.0.0.1 then setting host to 'localhost' should work. but if your bind-address is your actual IP address then you should add your IP address instead.

var sql_con = mysql.createConnection({ 
host: "ip address",// should be the same as bind-address
user: "jeffery",
password: "damnthing",
database: "KOKATOOO"
});

Upvotes: 1

Aditya Aggarwal
Aditya Aggarwal

Reputation: 169

I would recommend to add the port in the below table of yours;

const pool=mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    database: 'node-complete',
    port: "3307",
    password: 'Aditya2000#'
});

The port number is to be taken from your MySql Workbench from the homepage table of it which should shows:

Local Instance MySql root localhost: 3307 -> this localhost number has to be written there.

Upvotes: 1

Sri Charan Naga
Sri Charan Naga

Reputation: 1

open services from the search bar.In there search for MySQL80.Right click on it and click on start.Now run the code.It should work.Most of the times the SQL service is inactive when the computer starts up

Upvotes: 0

user12602844
user12602844

Reputation:

I fix it by the following code after a struggle of two days. ( Just giving my solution, you might have another solution as well.)

Follow these steps as well.

  1. Delete all node_modules
  2. Run "npm audit --force"
  3. install npm packages by "npm install"

const sequelize = new Sequelize("test", "root", "root", { host: "127.0.0.1", dialect: "mysql", port: "8889", connectionLimit: 10, socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock" });

Note: I am using sequelize with graphql (NODEJS).

Upvotes: 3

salah
salah

Reputation: 1

I 'm also have same problem but i found the solution by adding: port:"80" I connect with node.js to the server php mysql using this code: var mysql=require('mysql');`

var connection=mysql.createConnection({
   port:"80",
    host:"localhost",
    user:"root",
    password:""
});

Upvotes: 0

Kalpit tandon
Kalpit tandon

Reputation: 129

Check your xampp server mysql is running or not

Upvotes: 6

shrikantbishoye
shrikantbishoye

Reputation: 969

I was having same problem but I solved this by changed

host:  'localhost'

to

host:   '127.0.0.1'

Upvotes: 73

Doeun
Doeun

Reputation: 339

It's probably course from the absence of port and database. first I wrote: host, user, password and no database and port.

Here you need to specified what is your database name, what is your port number. So the total must be: 1. host 2. user 3. password 4. database 5. port

It's works for me, try it if it works for you too.

Upvotes: 0

CLIFFORD P Y
CLIFFORD P Y

Reputation: 17404

I Also have same problem in google cloud with nginx

changed

 host: 'localhost'

to

host : 'cloud_instance_private_ip'

may help some one with same problem

Upvotes: 0

harishannam
harishannam

Reputation: 2796

Check for your MySQL port in your server.

In MAMP, by default it uses 8889.

Add that to your connection config.

Your MySQL config should look like this.

this.pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'todo',
  port: '8889'
});

Upvotes: 23

tdebroc
tdebroc

Reputation: 1526

You need to add the value of socket path to the config object:

socketPath: '/var/run/mysqld/mysqld.sock'

In MAMP, you go to http://localhost:8888/MAMP, and you find:

/Applications/MAMP/tmp/mysql/mysql.sock

At the end you have:

var connection = mysql.createConnection({
  host     : config.host,
  user     : config.user,
  password : config.pass,
  database : config.db,
  socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
});

Upvotes: 57

Related Questions