[英]Can't connect to MySQL DB with Node.js
不久前,我设置了数据库服务器,并使其节点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,仅在本地可用)
我最终将我的队友db_access文件复制到我的目录中,并且可以正常工作。 不知道是什么问题,因为我检查了大约10次拼写错误和语法错误等。可能是一个奇怪的字符,将诸如智能引号之类的东西弄乱了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.