Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get error connect ECONNREFUSED 127.0.0.1:3306 when start my node.js app

When I start my node.js app I get this error:

SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

What I was doing:

 - create a user:
CREATE USER 'main'@'localhost' IDENTIFIED BY 'myPass';

 - give this user all privileges
GRANT ALL PRIVILEGES ON *.* TO 'main'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

I then try to connect through my code on production and it gives me an error: connect ECONNREFUSED 127.0.0.1:3306.

But in my localhost environment it connects well. And when I try to connect to DB using command line on VPS it works too: mysql -umain -p

I can't connect with code. But on my localhost it connects. Double checked my loggin and pass for DB user in .env.

like image 520
Nastro Avatar asked Feb 24 '19 23:02

Nastro


People also ask

What is connect Econnrefused?

ECONNREFUSED means no process is listening at the given address and port.

Can we connect mysql with node js?

Once you have MySQL up and running on your computer, you can access it by using Node. js. To access a MySQL database with Node. js, you need a MySQL driver.

What does the error econnrefused 127 3306 mean?

The error ECONNREFUSED 127.0.0.1:3306 is being raised by Node.js itself, and not this module. This module passes the error through, though, from Node.js. Basically, this module is asking Node.js to create a TCP connection to 127.0.0.1 , port 3306 , but for whatever reason, Node.js cannot do this and raises the ECONNREFUSED error.

How long does it take for nodejs error connect econnrefused to work?

Here it's my success story with incredible persistant Node.js error connect ECONNREFUSED . Day 1. I run this code, and should work fine. Usual errors, maybe 3-4 minutes. I changed host: 'localhost' to host : '127.0.0.1', port: 8080, or maybe 8000, oh yes, 3306 this is.

How to resolve econnrefused error 7668 in MySQL?

In this case, the process will be the one with the pid 7668. To kill it and restart the server, run kill -9 7668. Show activity on this post. Check with starting mysql in terminal. Use below command Show activity on this post. ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080 ).

What is unhandled error in Node JS?

The Unhandled 'error' event is referring not providing a function to the request to pass errors. Without this event the node process ends with the error instead of failing gracefully and providing actual feedback. You can set the event just before the request.write line to catch any issues: Show activity on this post.


3 Answers

Try restarting the MySQL service.

like image 165
Leandro Rocha Avatar answered Oct 17 '22 22:10

Leandro Rocha


I found a problem! In my mysql config file (etc/my.cnf) was bind-address = my VPS ip So, when I remove this line it starts to listen al ips.

Thanks every one!

like image 2
Nastro Avatar answered Oct 17 '22 20:10

Nastro


I had the same problem and solved it by adding "socketPath" to my sequelize deceleration like this:

const sequelize = new Sequelize('db-name', 'user', 'password', {
  host: 'localhost',
  dialect: 'mysql',
  dialectOptions: {
    socketPath: 'your socket path',
    supportBigNumbers: true,
    bigNumberStrings: true
  },
});

find your socket path by entering /etc/mysql/mysqld.cnf: then you have their your socket path:

user        = mysql
# pid-file  = /var/run/mysqld/mysqld.pid
# socket    = /var/run/mysqld/mysqld.sock
# port      = 3306
# datadir   = /var/lib/mysql
like image 1
Noga-z Avatar answered Oct 17 '22 21:10

Noga-z