Cant connect to localhost database with Sequelize (nodejs)

蹲街弑〆低调 提交于 2020-01-13 05:53:49

问题


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?


回答1:


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!




回答2:


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.




回答3:


Can you try with host 127.0.0.1?



来源:https://stackoverflow.com/questions/8392239/cant-connect-to-localhost-database-with-sequelize-nodejs

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