nodejs + mysql: how can i reopen mysql after end()?

code as follows:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host      : 'localhost',
  port      :"3306",
  database  :"mydb",
  user      : 'root',
  password  : '007007',
});
var isCon = true;
connection.connect(
    function  (err) {
        console.log(isCon);
        if (err) {
           isCon=false;
           console.error("error connecting :"+err);
           return;
       };
   }
   );
if(isCon){
    connection.query('select * from tb_items', function(err, result) {
        if (err) throw err;
        console.log('The solution is: ', result);
        console.log('The typeof solution is ',typeof(result));
        debugger;
    });
    connection.end();
}

connection.connect(
    function  (err) {
        console.log(isCon);
        if (err) {
           isCon=false;
           console.error("error connecting :"+err);
           return;
       };
   }
   );
if(isCon){
    connection.query('select * from tb_items', function(err, result) {
        if (err) throw err;
        console.log('The solution is: ', result);
        console.log('The typeof solution is ',typeof(result));
        debugger;
    });
    connection.end();
}

i just open()-->connect()-->query()-->end(),then did it again,but second time , there is a error : Error:Could not enqueue Handshake after invoking quiting . question : maybe i can't reopen it after end().but i just wanna kwon ,if i end(),how can i reopen it?

no, you can't, just create another one. All state in the connection class only relevant for current connection, so having "reopenWithSameConfig" method is possible, but you should really do this from outside.

Also, you don't need to close connection on each query - just continue to reuse it without calling .end()

If you want multiple connections and automatic disposal of dead connections you should use Pool class.

One more note: your isCon check is incorrect, connection.query is called before connect() callback so it's always true. It's safe to just check error in query callback itself. If connection was not successful error is propagated to a queued command