Can't connect to MySQL with Sequelize

26,282

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
    }
});
Share:
26,282
fredrikekelund
Author by

fredrikekelund

Updated on July 09, 2022

Comments

  • fredrikekelund
    fredrikekelund almost 2 years

    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?

  • cfogelberg
    cfogelberg over 8 years
    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.
  • Guy
    Guy over 6 years
    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.
  • Andriy Kharchuk
    Andriy Kharchuk about 6 years
    This doesn't solve the problem if you have a dedicated MySQL server host.