컴퓨터/NodeJS

NodeJS npm mysql2

sayyesdoit 2022. 5. 15. 04:34

1. 설치

npm install --save mysql2

 

2. Promise Wrapper 사용

async () => {
  // get the client
  const mysql = require('mysql2');
  // create the pool
  const pool = mysql.createPool({host:'localhost', user: 'root', database: 'test'});
  // now get a Promise wrapped instance of that pool
  const promisePool = pool.promise();
  // query database using promises
  const [rows,fields] = await promisePool.query("SELECT 1");
  pool.end();
}