问题描述
尝试连接到我服务器上的 MySQL 数据库时,我总是收到 SequelizeConnectionRefusedError
.
I consistently get a SequelizeConnectionRefusedError
when trying to connect to a MySQL database on my server.
登录凭据正确,端口打开,一切看起来都很好(在开发环境中就像一个魅力).
The login credentials are correct, the port is open, everything seems good (and works like a charm in the dev environment).
抱歉,背景信息稀少,但我在这里傻眼了——我真的不知道是什么导致了这个问题.
Sorry for the scarce background information, but I'm dumbfounded here - I really don't know what could be causing this problem.
这是我的 mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3
这是我用来初始化 Sequelize 的代码.我希望它使用的表还不存在,但我很确定这与这个问题没有任何关系.我也尝试使用 root 用户登录,但没有骰子 - 我仍然遇到同样的错误.
And this is the code I'm using to initialize Sequelize. The table I want it to use doesn't exist yet, but I'm fairly sure that hasn't got anything to do with this problem. I've tried logging in with the root user as well, but no dice - I still get the same error.
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
port: 3306,
define: {
paranoid: true
}
});
var Model = sequelize.define("Model", {
md5: {type: Sequelize.STRING(128)},
ip: {type: Sequelize.STRING(256)},
url: {type: Sequelize.STRING(1024)}
});
sequelize.sync();
这是在 Ubuntu 14.04 上运行的,其中 node 在Passenger 后面运行(尽管如果我也直接使用 node 运行应用程序也会出现错误).我在同一台服务器上运行 nginx 和 PHP,如果有任何相关性,另一个 PHP 应用程序正在连接到数据库.
This is running on Ubuntu 14.04, where node is being run behind Passenger (although the error appears if I run the application with node directly as well). I'm running nginx and PHP on the same server, where another PHP application is connecting to the database, if that's of any relevance.
什么可能导致这个问题?
What could be causing this problem?
推荐答案
我也尝试使用 MySQL 模块直接连接到数据库,但这给了我同样的错误.在寻找相同问题的解决方案,但与 MySQL 模块而不是 Sequelize 相关时,我发现了这个:连接 ECONNREFUSED - 节点 js、sql.
I tried to connect to the database directly with the MySQL module as well, but that gave me the same error. When looking for solutions to the same problem, but related to the MySQL module rather than Sequelize, I found this: connect ECONNREFUSED - node js , sql.
我需要为 mysql
模块提供 socketPath
键.以下是我更改代码以使其工作的方式:
What I needed was to supply the mysql
module with a socketPath
key. Here's how I changed my code to make it work:
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
logging: function () {},
pool: {
max: 5,
min: 0,
idle: 10000
},
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
},
define: {
paranoid: true
}
});
这篇关于无法使用 Sequelize 连接到 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!