我刚才设置了我的数据库服务器,并让我的 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/