Reproduce MySQL error: The server closed the connection (node.js)

后端 未结 5 1802
你的背包
你的背包 2020-12-25 14:38

I\'m trying to reproduce a MySQL error I\'m seeing in my node.js app on EC2 with the node mysql library:

Connection lost: The server closed the connec

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-25 15:14

    The solution is use pooling connection !

    You can wrote code to handle connection manually, it works. However pooling is design for this, use pooling connection solved connection drop error.

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

    pooling mysql connection

提交回复
热议问题