javascript 无法使用 Sequelize 连接到 MySQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29866133/
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
Can't connect to MySQL with Sequelize
提问by fredrikekelund
I consistently get a SequelizeConnectionRefusedError
when trying to connect to a MySQL database on my server.
SequelizeConnectionRefusedError
尝试连接到我的服务器上的 MySQL 数据库时,我始终得到一个。
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.
很抱歉提供了稀缺的背景信息,但我在这里目瞪口呆 - 我真的不知道是什么导致了这个问题。
This is my output from mysql --version
这是我的输出 mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3
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.
这是我用来初始化 Sequelize 的代码。我想要它使用的表尚不存在,但我很确定这与这个问题没有任何关系。我也试过用 root 用户登录,但没有骰子 - 我仍然遇到同样的错误。
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();
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.
这是在 Ubuntu 14.04 上运行的,其中节点在乘客后面运行(尽管如果我直接使用节点运行应用程序也会出现错误)。我在同一台服务器上运行 nginx 和 PHP,其中另一个 PHP 应用程序正在连接到数据库,如果这有任何相关性的话。
What could be causing this problem?
什么可能导致这个问题?
回答by fredrikekelund
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 模块直接连接到数据库,但这给了我同样的错误。在寻找相同问题的解决方案时,但与 MySQL 模块而不是 Sequelize 相关,我发现了这一点:connect ECONNREFUSED - node js , sql。
What I needed was to supply the mysql
module with a socketPath
key. Here's how I changed my code to make it work:
我需要的是为mysql
模块提供一把socketPath
钥匙。以下是我更改代码以使其正常工作的方法:
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
}
});