Node.js MySQL - Error: ECONNREFUSED connection

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

I am testing my web application using 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 using

 $ node server.js 

When I load the page, it displays correctly, but when Node.js tries to connect to the database, I always get 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) 
+12
source share
7 answers

You need to add the socket path value to the configuration object:

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

In MAMP, you will go to http: // localhost: 8888 / MAMP , and you will 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' }); 
+26
source

Check your MySQL port on your server.

MAMP uses 8889 by default.

Add this to your connection configuration.

MySQL configuration should look like this.

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

I had the same problem, but I solved it by changing

 host: 'localhost' 

in

 host: '127.0.0.1' 
+1
source

I have the same problem in google cloud with nginx too

changed

  host: 'localhost' 

to

host : 'cloud_instance_private_ip'

might help someone with the same problem

0
source

This is probably the course due to the lack of port and database. First I wrote: host, user, password and no database and port.

Here you need to indicate what your database name is, what port number you have. Thus, the total should be: 1. host 2. user 3. password 4. database 5. port

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

0
source

Make sure your xampp mysql server is running or not

0
source

I also have the same problem, but I found a solution by adding: port:"80" 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:"" }); 
0
source

Source: https://habr.com/ru/post/987294/


All Articles