Can't connect to MySQL with Sequelize

与世无争的帅哥 提交于 2019-12-07 15:12:07

问题


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.

This is my output from 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.

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.

What could be causing this problem?


回答1:


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.

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


来源:https://stackoverflow.com/questions/29866133/cant-connect-to-mysql-with-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!