无法使用Sequelize(nodejs)连接到localhost数据库 [英] Cant connect to localhost database with Sequelize (nodejs)

查看:345
本文介绍了无法使用Sequelize(nodejs)连接到localhost数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SequelizeJS连接并与我的本地主机(MAMP)上的数据库通信。但是,每当我尝试执行查询时,我都会收到此错误: ECONNREFUSED 。我仔细检查了MySQL的端口号和主机名,即3306和localhost。我几乎遵循他们网站上的指南。以下是我的一些代码:

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
});

在函数中,我有:

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?

推荐答案

所以,谢谢你的答案,但我的同事最终找到了问题所在。它与MAMP中的配置有关。要解决此问题:

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:

在MAMP中,在菜单栏中,转到文件>编辑模板> MySQL my.cnf

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

在第46行,注释掉:

MAMP_skip-networking_MAMP

现在应该可以工作!

这篇关于无法使用Sequelize(nodejs)连接到localhost数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆