6

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?

mins
  • 6,478
  • 12
  • 56
  • 75
fredrikekelund
  • 2,007
  • 2
  • 21
  • 33

1 Answers1

20

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
    }
});
Community
  • 1
  • 1
fredrikekelund
  • 2,007
  • 2
  • 21
  • 33
  • 1
    Adding the dialectOptions key also resolved a slightly different error that I encountered: "SequelizeConnectionError: Connection lost: The server closed the connection.". This error occurred with both Sequelize 3.3.2 and 3.13.0 and was fixed by dialectOptions in both cases. – cfogelberg Nov 17 '15 at 09:51
  • 1
    Is there a scenario under which one would not use socketPath? Does this work equally well across the wire (remote DB server) as it would for a local DB server. – Guy Nov 15 '17 at 20:27
  • This doesn't solve the problem if you have a dedicated MySQL server host. – Andriy Kharchuk Apr 29 '18 at 03:23