无法使用Sequelize连接到MySQL [英] Can't connect to MySQL with Sequelize

查看:334
本文介绍了无法使用Sequelize连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试连接到服务器上的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上运行的,其中节点正在Passenger后面运行(尽管如果我出现错误)也可以直接运行带有节点的应用程序)。我在同一台服务器上运行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.

我需要的是提供带有 socketPath mysql 模块$ c>键。以下是我更改代码以使其工作的方式:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆