Node连接mySQL:Cannot enqueue Handshake after already enqueuing a Handshake.

解决办法:使用池连接

Pooling connections

https://www.npmjs.com/package/mysql

Rather than creating and managing connections one-by-one, this module also provides built-in connection pooling using mysql.createPool(config). Read more about connection pooling.

Create a pool and use it directly:

 var mysql = require('mysql');
 var pool  = mysql.createPool({
   connectionLimit : 10,
   host            : 'example.org',
   user            : 'bob',
   password        : 'secret',
   database        : 'my_db'
 });
  
 pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
   if (error) throw error;
   console.log('The solution is: ', results[0].solution);
 });

This is a shortcut for the pool.getConnection() -> connection.query() -> connection.release() code flow. Using pool.getConnection() is useful to share connection state for subsequent queries. This is because two calls to pool.query() may use two different connections and run in parallel. This is the basic structure:

 var mysql = require('mysql');
 var pool  = mysql.createPool(...);
  
 pool.getConnection(function(err, connection) {
   if (err) throw err; // not connected!
  
   // Use the connection
   connection.query('SELECT something FROM sometable', function (error, results, fields) {
     // When done with the connection, release it.
     connection.release();
  
     // Handle error after the release.
     if (error) throw error;
  
     // Don't use the connection here, it has been returned to the pool.
   });
 });

If you would like to close the connection and remove it from the pool, use connection.destroy() instead. The pool will create a new connection the next time one is needed.

Connections are lazily created by the pool. If you configure the pool to allow up to 100 connections, but only ever use 5 simultaneously, only 5 connections will be made. Connections are also cycled round-robin style, with connections being taken from the top of the pool and returning to the bottom.

When a previous connection is retrieved from the pool, a ping packet is sent to the server to check if the connection is still good.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常表示在MySQL连接上发生了严重的错误,导致连接不再可用。在这种情况下,你需要关闭现有的MySQL连接,然后重新创建一个新的连接。 以下是一个示例代码,演示如何关闭现有连接并创建新连接: ``` const mysql = require('mysql'); let connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'database_name' }); connection.connect((error) => { if (error) { console.error('Error connecting to MySQL database: ' + error.stack); return; } console.log('Connected to MySQL database with ID ' + connection.threadId); // Perform some MySQL queries here... // When you're done, close the connection connection.end((error) => { if (error) { console.error('Error closing MySQL connection: ' + error.stack); return; } console.log('MySQL connection closed'); // Create a new connection connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'database_name' }); connection.connect((error) => { if (error) { console.error('Error connecting to MySQL database: ' + error.stack); return; } console.log('Connected to MySQL database with ID ' + connection.threadId); // Perform some more MySQL queries here... }); }); }); ``` 请注意,以上代码中的“localhost”、“root”、“password”和“database_name”应替换为你的MySQL数据库的实际信息。此外,你应该在每个查询之后关闭连接,并在需要时重新创建新连接,以避免出现此错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值