errno: 1203, code: 'ER_TOO_MANY_USER_CONNECTIONS' in nodejs mysql code?



大家好,我是Node.js Mysql的初学者。我已经用mysql连接到Node.js。在启动Node.js服务器时,我收到了类似"代码:'ER_TOO_MANY_USER_CONNECTIONS'"的错误,我将在下面附加mysql-db连接代码。任何形式的帮助都将不胜感激。提前感谢。。。

var db = mysql.createPool({
host: 'xxxxxxxxxxxxxxxxx',
port: 'xxx',
user: 'xxxx',
password: 'xxx',
database: 'xxx'
});
db.getConnection((err, tempConn) => {
if (err) {
console.log(err);
}
else {
tempConn.release();
console.log('Mysql Connected');
}
});
module.exports={db};

如果要创建池,则不需要使用getConnection。有一个快捷方式可以让你直接使用它。如果确实使用getConnection,则必须在其后执行查询,然后才能释放连接。您的示例缺少一个查询。

以下是使用池配置的有用模板:

// in your application initialization file such as app.js
// 
// other require items here as well like express maybe?
// 
const mysql = require('mysql');
const connection = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST || '127.0.0.1',
user: process.env.DB_USER || 'local_user',
password: process.env.DB_PASSWORD || 'local_password',
database: process.env.DB_NAME || 'local_database',
multipleStatements: true, 
charset: 'utf8mb4' // necessary if you might need support for emoji characters
});
connection.on('connection', function (connection) {
// handy for testing
console.log('Pool id %d connected', connection.threadId);
});
connection.on('enqueue', function () {
// handy for testing
console.log('Waiting for available connection slot');
});
global.db = connection;
// 
// other app setup stuff here like app.set, app.engine, app.use, module.exports = app and all that good stuff
// 

// later… 
// everywhere else in your app, use the global db variable when running queries
// ../new_users.js or similar maybe?
const _create_user = (user_payload) => {
db.query(
'INSERT INTO users SET ?', user_payload, function(error, results, fields) {
if (error) throw error;
console.log(results);
});
}

// maybe we are in a module that has access to 
// the request object so we can use something
// that has come via POST
// 
// here is a manual object as a placeholder…
let new_user = {
first_name: 'John',
last_name: 'Smith',
email: 'j.smith@example.com',
password: 'keyboard_cat'
}
_create_user(new_user);

最新更新