John
John

Reputation: 474

Cant connect to localhost database with Sequelize (nodejs)

I am using SequelizeJS to connect and talk to my database which is on my localhost (MAMP). However, whenever I try to execute a query, I keep getting this error: ECONNREFUSED. I double checked the port number of MySQL and the host name, which are 3306 and localhost. I pretty much followed the guide that is on their site. Here is some of my code:

var sequelize = new mysql('partythump', 'root', 'mysqlroot', {
    host: 'localhost',
    port: 3306,
    timestamps: false
}); 

var parties = sequelize.define('parties', {
    id: mysql.INTEGER,
    party_name: mysql.STRING,
    party_id: mysql.STRING,
    created_on: mysql.DATE,
    is_active: mysql.BOOLEAN,
    party_code: mysql.STRING,
    geo_location: mysql.STRING
});

In a function, I have this:

var party = parties.build({
    id: null,
    party_name: 'test',
    party_id: '',
    is_active: 1,
    party_code: '',
    geo_location: '',
    created_on: new Date()
});

party
.save()
.on('success', function() {
    console.log('Inserted new party into db');
})
.on('failure', function(error) {
    console.log('Could not execute insert query');
    console.log(error);
});

Can anyone help me out?

Upvotes: 2

Views: 8908

Answers (4)

Nazar D.
Nazar D.

Reputation: 1

You should add the parameter 'dialectOptions' with 'socketPath' attribute when you call new mysql

Upvotes: 0

John
John

Reputation: 474

So, thanks for the answers, but my co-worker eventually figured out the problem. It has to do with a configuration in MAMP. To fix this:

In MAMP, in the menu bar, go to File > Edit Template > MySQL my.cnf

On line 46, comment out:

MAMP_skip-networking_MAMP

Should work now!

Upvotes: 7

Monolo
Monolo

Reputation: 18253

MySQL client libraries normally try to connect through Unix sockets (a special kind of file) when you specify localhost as the host name.

This only works if the client library and the MySQL server process look for the Unix socket under the same name, and set it in both the server's and client's configuration files.

For development work, you don't need the speed of the Unix sockets, so sdepold's suggestion of using 127.0.0.1 should get you going.

Then you can go through the documentation for your server and client installation later and find the settings to fiddle with when you feel in the mood for that.

Upvotes: 1

sdepold
sdepold

Reputation: 6241

Can you try with host 127.0.0.1?

Upvotes: 0

Related Questions