【问题标题】:Can't connect to MySQL DB with Node.js无法使用 Node.js 连接到 MySQL 数据库
【发布时间】:2016-03-22 00:53:35
【问题描述】:

我不久前设置了我的数据库服务器,并让我的节点 Web 服务器正常连接到它。在我的 dbcommon.js 文件中,我有以下内容。

var credentials = require('./db_access');
var mysql = require('mysql');

//Use this function to grab a thread from the db thread pool and connect to the db.  Then it queries the db with the sql statement passed to it.
exports.executeStatement = function(pool, sql, callback){
    pool.getConnection(function(err, connection){
        if (err) {
            callback(err);
        }
        else {  
            connection.query(sql, function(err, results, fields) {
                if(!err){
                    connection.release();
                    callback(results);

                } else {
                    connection.release();
                    callback(err);
                }
            });
        }
    });
};

//TODO - Own class
exports.pool = mysql.createPool({
    connectionLimit: 100,
    host: "host_url_goes_here",
    user: credentials.user,
    password: credentials.pass,
    database: "bxdb",
    dateStrings: true //Instead of converting to a JS Date object, return as a string
});

在我的 db_access.js 文件中,我有

var user = 'user123';
var pass = 'password123'; //not real

module.exports = user;
module.exports = pass;

奇怪的是,在我决定尝试使用 IAM 密钥来访问数据库之前,它工作得非常好。但是我无法让它工作(访问被拒绝错误)所以我改回我现在的状态,但我仍然收到访问被拒绝错误。然而,我的朋友拥有相同的代码仍然能够访问数据库而不会出现此错误。我很迷茫。 db_access.js 文件与 dbcommon.js 文件位于同一文件夹中。

另外,我目前访问数据库的方式是否相当安全?还是我应该以另一种方式来做? (db_access.js 文件永远不会推送到 git,它只在本地可用)

【问题讨论】:

    标签: mysql node.js amazon-web-services access-denied


    【解决方案1】:

    我最终将我的 mates db_access 文件复制到我的目录中并且它有效。不知道问题出在哪里,因为我检查了大约 10 次拼写错误和语法错误等。可能是一个奇怪的字符搞砸了,比如智能引号之类的东西。

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 2021-01-20
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多