安裝mysql2

npm install mysql2 --save

設定網站接口:

const mysql = require('mysql2');
const connection = mysql.createConnection({
  socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock', // connect mamp mysql sock
  host: 'localhost',
  user: 'MYSQL_USERNAME',
  password: 'MYSQL_PASSWORD',
  database: 'MYSQL_DB_NAME',
  port: MYSQL_PORT
});

連接測試:

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

connection.end();

存取資料範例:

const sql = 'SELECT * FROM example';

connection.query(sql, function(err, result) {
  if (err) {
    console.log('[SELECT ERROR] - ', err.message);
    return;
  }

  console.log(result);
});

connection.end();

進階存取範例:

connection.connect();

function performQuery(sql) {
  return new Promise((resolve, reject) => {
    connection.query(sql, (error, results) => {
      if (error) {
        reject(error);
      } else {
        resolve(results);
      }
    });
  });
}

try {
  const results = await performQuery('SELECT * FROM example');
  console.log(results);
}
catch (error) {
  console.error('Error executing query:', error);
}

connection.end();

參考資料