DEV Community

Cover image for Node.js Error: connect ECONNREFUSED - how to connect Node with phpMyAdmin
Dan Neamtu
Dan Neamtu

Posted on

Node.js Error: connect ECONNREFUSED - how to connect Node with phpMyAdmin

I spend more than 6 hours searching and testing, I get frustrated, angry and you know... just for 3 minutes job.

Here it's my success story with incredible persistant Node.js error connect ECONNREFUSED .

Day 1.
I run this code, and should work fine.
Node Connection

Oh, but no, some errors
Error: connect ECONNREFUSED

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. No, not that. Oh, yes, hostname: '127.0.0.1:3306'. No. Adding database, remove database. Change every thing I can now change. Nothing.

"You should check if your XAMMP server is running", yes it is green.
Error: connect ECONNREFUSED

Maybe it is the port 8443. node app.js enter, connect ECONNREFUSED. no, it's not the port. No password, no root, no port. What can solve this?

OOhh, yes, I know, I should thinking about that. Maybe I need to put the project in root XAMPP root folder. No.

connect ECONNREFUSED

3 hours later. I need more music.

Day 2
Today, Sunday, with some first snow outside, I discovered something new. There is an IP address on my XAMPP, and maybe this is how I can solve the error.
Error: connect ECONNREFUSED

Unfortunately it was not the case.

Starting to find the real solution in my case
And after I read Yannick Motton's best answer on post host-xxx-xx-xxx-xxx-is-not-allowed-to-connect-to-this-mysql-server I found the solution.

The solution:

1. Create an user with password, and grant all provileges
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON . TO 'username'@'localhost' WITH GRANT OPTION;
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON . TO 'username'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Error: connect ECONNREFUSED
This will set user: username and pass: password

2. Change localhost with your XAMPP IP (and also add username/password for user and password)
Error: connect ECONNREFUSED
Change host: "localhost" to host:"192.168.64.2", and update with user: "username", password: "password".
node app + enter
Error: connect ECONNREFUSED
Connection established It's done. Hooray.

3. Celebrate the victory
Now that your terminal print Connection established, you whould celebrate the victory with dev friends.
Error: connect ECONNREFUSED

Final thoughts

Hope this article will help you to solve the connect ECONNREFUSED error and to connect Node.js with phpMyAdmin in just a few minutes.

โ€œPatience, persistence, and perspiration make an unbeatable combination for success.โ€ - Napoleon Hill

Top comments (12)

Collapse
 
amfsouza profile image
Antonio Marcos Fernandes de Souza

Well, I have the same issue but in my case, I'm getting the Postgresql with Node.js and TypeScript. My app is containerized and I see that I can connect the Dbeaver with my database container. Maybe I have too spend some days... to solve that obstacle. You know in our career every day is not the same the day before. We are warriors my friend.

Collapse
 
luissotoj profile image
Luis Eduardo Sotoj

Hello, I've the same issue, but my db is on RemoteMysql.

Have your a similar scenario?

Collapse
 
samueltdaly profile image
Dale Dobak

I love you for this! 2 Days I search for the solution and you fixed it in 4 min!!! Thank you sir

Collapse
 
upsier profile image
Dan Neamtu • Edited

Glad to help you! Thank you!

Collapse
 
haidotcom profile image
haidotcom

In my case,
change
from
host: "localhost"
to
host: "127.0.0.1"
is work

Collapse
 
dignelsonx profile image
DignelsonX

Thank you so much bro!!

I had this problem on my MacBook and I had no idea how to fix it...

Thank you!

Collapse
 
henryherrington profile image
henryherrington

Wow thank you so much! I was about to give up on this error.

Collapse
 
farukdeste profile image
farukdeste

Grateful for this

Collapse
 
prezine profile image
Precious Tom

How do I get the IP Address from MAMP?

Collapse
 
mahmoud_bebars profile image
m.bebars

it was a quite tricky and needed to give my user lots of privileges but with some more research I got it right

Collapse
 
muhammadfazeel profile image
Muhammad Fazeel

thanks for sharing