適用於 Node.js 的 Azure MySQL 模組Azure MySQL modules for Node.js

用於存取 Azure Database for MySQL 的建議用戶端程式庫是開放原始碼之 Azure Database for MySQL 的 Node.js 連線程式庫The recommended client library for accessing Azure Database for MySQL is the open-source Node.js connection library for Azure Database for MySQL.

深入了解 Azure Database for MySQLLearn more about Azure Database for MySQL

用戶端套件Client Package

安裝 npm 模組Install the npm module

使用 npm 來安裝 MySQL 用戶端模組。Use npm to install the MySQL client module.

npm install mysql2

範例Example

此範例會連線到 MySQL 資料庫,並執行簡單查詢來擷取所有客戶。This example connects to a MySQL database and performs a simple query to retrieve all customers.

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: 'mysqldemo.mysql.database.azure.com',
  user: 'myadmin@mysqldemo',
  password: 'your_password',
  database: 'my_db',
  port: 3306,
  ssl: true
});

connection.connect();
const query = 'SELECT * FROM customers';
connection.query(query, (err, res) =>
  console.log(`We have ${res.length} customers`)
);

connection.end();

範例Samples

深入探索可在應用程式中使用的 Node.js 程式碼範例Explore more sample Node.js code you can use in your apps.