NodeJS 不会使用 node-mysql 连接到本地数据库

NodeJS won't connect to local DB with node-mysql

我遇到了 NodeJS 和 node-mysql 的问题。出于某种原因,在我的本地环境中,我能够连接到数据库并访问端点以更新和插入信息。我正在使用 mac os x Sierra 进行开发。我在我的开发服务器上使用 Ubuntu 16.04,我只是 运行 端口 80 上的节点。

我的 connection.js 文件如下所示。

var mysql = require('mysql');

function Connection() {
  this.pool = null;

  this.init = function() {
    this.pool = mysql.createPool({
      connectionLimit: 100,
      host: 'localhost',
      user: 'Miguel',
      password: 'thepassword',
      database: 'lavishwebLogin',
     port:'3306'
    });
  };

  this.acquire = function(callback) {
    this.pool.getConnection(function(err, connection) {
      callback(err, connection);
    });
  };
}

module.exports = new Connection();

我的端点看起来像这样...

this.login = function(req, res){
        connection.acquire(function(err, con) {
            con.query('SELECT * FROM users WHERE Email = "' + req.Email + '";', function(err, result) {
                if(!err){
                    if(bcrypt.compareSync(req.Password, result[0].Pass) == true){
                        result[0].Pass = "";
                        res.send(result[0])
                        con.release();
                    } 
                } else {
                    res.send(err)
                    con.release()
                }
            });
        });
    } 

错误我在控制台中收到这样的错误响应

我在终端中也遇到这样的错误。

build.js:1275 POST http://lavishweb.com/api/login net::ERR_CONNECTION_REFUSED

无法读取未定义的 属性 'query' 在 /root/syn-tacticsolutions/api/users/users.js:38:13 在 /root/syn-tacticsolutions/connection.js:18:7

在你说这与权限有关之前... 我在 v-server

中调用了它
mysql -u Miguel -p

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

如有任何帮助,我们将不胜感激。我到处寻找它可能无法正常工作的原因。在这一点上我真的很困惑。

好的伙计们,我发现了问题。因此,由于某种原因,当连接到同一系统上的数据库服务器时,localhost 不正确。我最终要做的是为我的远程服务器的静态外部 IP 地址(不是 127.0.0.1)创建一个用户,然后使用该用户访问它。

mysql -u root -p
password: thepassword

>create user 'user'@'ip' identified by 'thepassword';
>grant all on *.* to 'user'@'ip';
>flush privileges;
>exit;

 this.init = function() {
   this.pool = mysql.createPool({
     host: 'ip',
     user: 'user',
     password: 'thepassword',
     database: 'table'
   });
 };

一切正常。耶!!