Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to MySQL with Sequelize

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?

like image 372
fredrikekelund Avatar asked Apr 25 '15 14:04

fredrikekelund


1 Answers

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
    }
});
like image 194
fredrikekelund Avatar answered Oct 29 '22 08:10

fredrikekelund