nodejs 和 mysql 连接

发布时间 2023-06-13 11:41:46作者: 漫漫长路

 

 

原文

https://www.mysqltutorial.org/mysql-nodejs/connect/

 

 

 

let mysql =require('mysql')

let connection = mysql.createConnection({
    host:'119.91.31.144',
    user:'test1',
    password:'',//密码
    database:'test1'
})

// 连接数据库
connection.connect(function(err) {
  if (err) {
    return console.error('error: ' + err.message);
  }

  console.log('Connected to the MySQL server.');
});

// 关闭数据库
connection.end(function(err) {
    if (err) {
      return console.log('error:' + err.message);
    }
    console.log('Close the database connection.');
  });
  


//   连接池
//   var pool = mysql.createPool({
//     connectionLimit: 5,
//     host: 'localhost',
//     user: 'root',
//     password: '', 
//     database: 'todoapp'
// });