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条回答
Bombasti
2楼-- · 2020-05-20 05:18

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.

查看更多
老娘就宠你
3楼-- · 2020-05-20 05:20

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.

查看更多
\"骚年 ilove
4楼-- · 2020-05-20 05:21

I was having same problem but I solved this by changed

host:  'localhost'

to

host:   '127.0.0.1'
查看更多
在下西门庆
5楼-- · 2020-05-20 05:27

Check your xampp server mysql is running or not

查看更多
霸刀☆藐视天下
6楼-- · 2020-05-20 05:27

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).

查看更多
该账号已被封号
7楼-- · 2020-05-20 05:31

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