Node.js MySQL - Error: connect ECONNREFUSED

2020-05-20 04:48发布

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)

11条回答
唯我独甜
2楼-- · 2020-05-20 05:31

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'
});
查看更多
女痞
3楼-- · 2020-05-20 05:31

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

查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-20 05:31

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:""
});
查看更多
混吃等死
5楼-- · 2020-05-20 05:41

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

查看更多
狗以群分
6楼-- · 2020-05-20 05:42

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"
});
查看更多
登录 后发表回答