mysql - 无法使用 Node.js 连接到 MySQL 数据库

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

我刚才设置了我的数据库服务器,并让我的 Node 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 key 访问数据库之前,它工作得非常好。但是我无法让它工作(访问被拒绝错误)所以我改回我现在拥有的,但我仍然收到访问被拒绝错误。然而,我的拥有相同代码的 friend 仍然能够访问数据库而不会出现此错误。我很困扰。 db_access.js 文件与 dbcommon.js 文件位于同一文件夹中。

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

最佳答案

我最终将我的伙伴 db_access 文件复制到我的目录并且它起作用了。不确定问题出在哪里,因为我检查了大约 10 次拼写错误和语法错误等。可能是一个奇怪的字符把事情搞砸了,比如智能引号之类的。

关于mysql - 无法使用 Node.js 连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302668/

相关文章:

php - 如何在多用户网站中使用准备好的语句?

PHP 登录系统 - 当像在数据库中一样登录用户名时,将用户名设置为小

node.js - 处理 Node.js 中未使用的流

amazon-web-services - 云形成 : conditional parameters

python - AWS Lambda 代理引发异常

amazon-web-services - 为 AWS 控制台登录而不是 API 调用强制执行 MFA

mysql - 还需要用 MySQL 5 在 J2EE 6 中编写连接池类吗?

mysql - 我该如何改进这个数据库,表之间的关系是否良好?

node.js - Firebase 的云功能 : admin vs root lookups

javascript - 为什么 Dispatcher 在 Flux 上发起不必要的事件?