Pet*_*sma 5 mysql node.js express
我有一个 MySQL、Express、Angular、NodeJS 应用程序,有时当我登录时,我的节点控制台中出现以下错误:
类型错误:无法读取未定义的属性“查询”
错误发生在我的passport.local.js文件中,这是一行:
connection.query('SELECT * FROM users WHERE username LIKE ?', [username], function (err, user) {
Run Code Online (Sandbox Code Playgroud)
这是护照功能
passport.use(new LocalStrategy(
function(username, password, done) {
console.log('app.js');
pool.getConnection(function(err, connection) {
console.log('err: ' + err);
console.log(connection);
connection.query('SELECT * FROM users WHERE username LIKE ?', [username], function (err, user) {
if (err) throw err;
for (var i = user.length - 1; i >= 0; i--) {
var current = user[i];
}
if(current){
if(bcrypt.compareSync(password, current.password)){
return done(null, user);
} else {
return done(null, false);
}
} else {
console.log('no user');
return done(null, false);
}
});
connection.release();
});
}
));
Run Code Online (Sandbox Code Playgroud)
我需要pool在我的文件顶部
var pool = require('../../config/connection');
Run Code Online (Sandbox Code Playgroud)
当错误发生时:
console.log(connection);
Run Code Online (Sandbox Code Playgroud)
获取:
不明确的
我还记录了错误:
console.log('err: ' + err);
Run Code Online (Sandbox Code Playgroud)
节目:
err: Error: ER_USER_LIMIT_REACHED: User 'bfe4a8980ede74' has exceeded the 'max_user_connections' resource (current value: 10)
Run Code Online (Sandbox Code Playgroud)
小智 8
我假设您max_user_connections的设置为 10。请增加该max_user_connection值。
show global variables like '%connections%';
Run Code Online (Sandbox Code Playgroud)
将帮助您提供已设置的连接数。增加连接数如果它小于 25 或 50。最大连接数可以超过 16000 我猜,这完全取决于你的 CPU,没有线程它可以处理等。
SET GLOBAL max_user_connections=100;
Run Code Online (Sandbox Code Playgroud)
max_user_connections是一个动态变量,这意味着您可以直接运行此查询。您不必关闭 mysql。
您收到的错误说明了问题:您的 MySQL 服务器只允许每个用户有 10 个连接,并且已达到该限制。
连接池的默认值mysql也恰好是 10,这与它非常接近。如果除您的 Express 应用程序之外的任何其他 MySQL 客户端使用相同的用户凭据连接到数据库,您可能会遇到该特定错误。我建议增加max_user_connectionsMySQL 配置。
除此之外,您的代码还有另一个问题:它在查询完成之前释放连接,这可能会导致意外行为。将调用移动到connection.release()回调内部:
pool.getConnection(function(err, connection) {
...
connection.query('SELECT * FROM users WHERE username LIKE ?', [username], function (err, user) {
connection.release();
...
});
});
Run Code Online (Sandbox Code Playgroud)
如果这是您使用 MySQL 的一种常见方式(获取连接、执行查询、释放连接),那么您可以通过使用pool.query()来让生活更轻松一些。请参阅此示例。
最后,如果您正在使用异步代码,请不要抛出错误,而是将它们传递给回调(并确保您确实处理了它们,因为pool.getConnection除了记录它们之外,从现在开始您不会处理任何错误):
pool.getConnection(function(err, connection) {
if (err) return done(err);
...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32068 次 |
| 最近记录: |