84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
人生最曼妙的风景,竟是内心的淡定与从容!
Two ways to solve it 1. You can configure the mysql connection pool
var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); var selectSQL = 'select * from t_user limit 10'; pool.getConnection(function (err, conn) { if (err) console.log("POOL ==> " + err); conn.query(selectSQL,function(err,rows){ if (err) console.log(err); console.log("SELECT ==> "); for (var i in rows) { console.log(rows[i]); } conn.release(); }); });
2. You can solve the problem by disconnecting and reconnecting
var mysql = require('mysql'); var conn; function handleError () { conn = mysql.createConnection({ host: 'localhost', user: 'nodejs', password: 'nodejs', database: 'nodejs', port: 3306 }); //连接错误,2秒重试 conn.connect(function (err) { if (err) { console.log('error when connecting to db:', err); setTimeout(handleError , 2000); } }); conn.on('error', function (err) { console.log('db error', err); // 如果是连接断开,自动重新连接 if (err.code === 'PROTOCOL_CONNECTION_LOST') { handleError(); } else { throw err; } }); } handleError();
Mysql has a connection pool. When concurrency is large, if your previous connection is not closed, the connection pool is full, the request cannot be processed, and the program will block, so it needs to be released when it is used up
Two ways to solve it
1. You can configure the mysql connection pool
2. You can solve the problem by disconnecting and reconnecting
Mysql has a connection pool. When concurrency is large, if your previous connection is not closed, the connection pool is full, the request cannot be processed, and the program will block, so it needs to be released when it is used up