超出了"max_user_connections"资源

问题描述:

我有一个MySQL,Express,Angular,NodeJS应用程序,有时登录时在节点控制台中出现以下错误:

I have a MySQL, Express, Angular, NodeJS application and sometimes when I log in I get the following error in my node console:

TypeError:无法读取未定义的属性查询"

TypeError: Cannot read property 'query' of undefined

该错误发生在我的passport.local.js文件中,这是一行:

The error occurs in my passport.local.js file, this is the line:

connection.query('SELECT * FROM users WHERE username LIKE ?', [username], function (err, user) {

这是护照功能

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();
        });
    }
));

我的文件顶部需要我的pool

I require my pool in the top of my file

var pool = require('../../config/connection');

发生错误时:

console.log(connection);  

获取:

未定义

我还记录了错误:

console.log('err: ' + err);

显示:

 err: Error: ER_USER_LIMIT_REACHED: User 'bfe4a8980ede74' has exceeded the 'max_user_connections' resource (current value: 10)

您收到的错误说明了问题:您的MySQL服务器仅允许每个用户10个连接,并且已达到该限制.

The error you're getting is stating the issue: your MySQL server is only allowing 10 connection per user, and that limit has been reached.

mysql连接池的默认值也恰好是10,即正在将它切得很近.如果除Express应用程序以外的其他MySQL客户端使用相同的用户凭据连接到数据库,则可能会遇到该特定错误.我建议在MySQL配置中增加max_user_connections.

The default for the mysql connection pool also happens to be 10, which is cutting it really close. If any other MySQL client besides your Express app is connected to the database with the same user credentials, you may run into that particular error. I would suggest increasing max_user_connections in the MySQL configuration.

除此之外,您的代码还有另一个问题:它在查询完成之前释放了连接,这可能导致意外的行为.将呼叫移至connection.release()到回调内部:

Aside from that, there's another issue with your code: it's releasing the connection before the query has finished, which may lead to unexpected behaviour. Move the call to connection.release() to inside the callback:

pool.getConnection(function(err, connection) {
  ...
  connection.query('SELECT * FROM users WHERE username LIKE ?', [username], function (err, user) {
    connection.release();
    ...
  });
});

如果这是使用MySQL的一种常见方式(获取连接,执行查询,释放连接),则可以使用pool.query()来简化生活.参见此示例.

If this is a common way you're using MySQL (get a connection, perform a query, release the connection), you can make life a bit easier by using pool.query() instead. See this example.

最后,如果您使用的是异步代码,请不要抛出错误,而是将它们传递给回调(并确保您确实处理了这些错误,因为您现在不处理pool.getConnection中的任何错误,除了记录它们):

And finally, if you're working with async code, don't throw errors but pass them to the callback (and make sure that you actually handle them, because you're not handling any errors from pool.getConnection now, besides logging them):

pool.getConnection(function(err, connection) {
  if (err) return done(err);
  ...
});