Node.js MySQL - 错误:连接 ECONNREFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30266221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:01:12  来源:igfitidea点击:

Node.js MySQL - Error: connect ECONNREFUSED

mysqlnode.jsapache

提问by Nico

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.

我使用 Node.js 服务器端。我在本地主机上尝试了我的代码,一切正常。我购买了一台服务器并在其上安装了 Apache 和 node.js 并在那里测试了我的 Web 应用程序。我正确地将 MySQL 连接配置从 localhost 更改为服务器配置。

I test my web application with this configuration:

我使用以下配置测试我的 Web 应用程序:

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.js 服务器:

$ 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:

当我加载页面时,它正确显示,但是当 Node.js 尝试连接到数据库时,我总是收到以下错误:

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)

回答by tdebroc

You need to add the value of socket pathto the config object:

您需要将 的值添加socket path到配置对象中:

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

In MAMP, you go to http://localhost:8888/MAMP, and you find:

在 MAMP 中,您转到http://localhost:8888/MAMP,您会发现:

/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'
});

回答by harishannam

Check for your MySQL port in your server.

检查服务器中的 MySQL 端口。

In MAMP, by default it uses 8889.

在 MAMP 中,默认情况下它使用 8889。

Add that to your connection config.

将其添加到您的连接配置中。

Your MySQL config should look like this.

您的 MySQL 配置应如下所示。

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

回答by Kalpit tandon

Check your xampp server mysql is running or not

检查您的 xampp 服务器 mysql 是否正在运行

回答by shrikantbishoye

I was having same problem but I solved this by changed

我遇到了同样的问题,但我通过更改解决了这个问题

host:  'localhost'

to

host:   '127.0.0.1'

回答by salah

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');`

我也有同样的问题,但我通过添加找到了解决方案: 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:""
});

回答by salah

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"
  1. 删除所有 node_modules
  2. 运行“npm audit --force”
  3. 通过“npm install”安装 npm 包

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" });

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

注意:我正在使用带有 graphql (NODEJS) 的 sequelize。

回答by Sri Charan Naga

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

从搜索栏中打开服务。在那里搜索 MySQL80。右键单击它并单击开始。现在运行代码。它应该可以工作。大多数情况下,计算机启动时 SQL 服务处于非活动状态

回答by Aditya Aggarwal

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:

端口号将从 MySql Workbench 的主页表中获取,该表应显示:

Local Instance MySql root localhost: 3307 -> this localhost number has to be written there.

本地实例 MySql 根 localhost: 3307 -> 这个 localhost 号必须写在那里。

回答by janithcooray

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.

在大多数情况下,您的主机应与 MySQL 配置文件中的绑定地址匹配。如果您的绑定地址是 0.0.0.0 或 127.0.0.1,那么将主机设置为 'localhost' 应该可以工作。但是如果您的绑定地址是您的实际 IP 地址,那么您应该添加您的 IP 地址。

var sql_con = mysql.createConnection({ 
host: "ip address",// should be the same as bind-address
user: "jeffery",
password: "damnthing",
database: "KOKATOOO"
});

回答by CLIFFORD P Y

I Also have same problem in google cloud with nginx

我在使用 nginx 的谷歌云中也有同样的问题

changed

改变了

 host: 'localhost'

to

host : 'cloud_instance_private_ip'

host : 'cloud_instance_private_ip'

may help some one with same problem

可能会帮助一些有同样问题的人