节点js无法连接到本地数据库

时间:2018-04-23 09:40:37

标签: mysql node.js localhost mamp

我有一个节点js App,当我尝试使用本地数据库(mysql)运行节点js时,我使用mamp并显示错误Localhost refuse to connect并显示日志TypeError: Cannot read property 'query' of undefined。但是当我尝试在服务器中使用db运行节点js时没问题。

app.js

var mysql = require("mysql");

function Connection() {

this.pool = null;

var konek = {
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'wmc_wahana'
};

this.init = function() {
    this.pool = mysql.createPool(konek);
}

this.acquire = function(callback) {
    this.init();
    this.pool.getConnection(function(err, connection) {
        callback(err, connection);
    });
};
}
module.exports = new Connection();

server.js

var app = require('./app');
var http = require('http').Server(app);
var port = '8080';

http.listen(port, function() {
   console.log('Server listening on port ' + port);
});

谢谢

1 个答案:

答案 0 :(得分:0)

请将“localhost”更改为“127.0.0.1”

相关问题