Use the MySQL2 driver to connect a Node.js application to LindormTable over the MySQL compatibility protocol. This guide walks you through installing the driver, configuring your connection, and running your first query.
Prerequisites
Before you begin, ensure that you have:
MySQL compatibility enabled for your Lindorm instance. See Enable the MySQL compatibility feature.
Your client IP address added to the Lindorm instance whitelist. See Configure a whitelist.
Node.js installed. Download it from the Node.js official site.
Install the MySQL2 driver
Run the following command to install the MySQL2 driver:
npm install mysql2Connect to LindormTable
Step 1: Set connection parameters
Store your credentials in environment variables to avoid hardcoding sensitive values in your code:
export LINDORM_HOST=<your-mysql-endpoint>
export LINDORM_USER=<your-username>
export LINDORM_PASSWORD=<your-password>Replace the placeholders with your actual values:
| Placeholder | Description |
|---|---|
<your-mysql-endpoint> | The MySQL-compatible endpoint for LindormTable. See View connection addresses. |
<your-username> | Your LindormTable username. |
<your-password> | Your LindormTable password. To reset a forgotten password, see Manage users. |
Choose the right endpoint for your deployment:
ECS instance (recommended): Use the virtual private cloud (VPC) endpoint for higher security and lower network latency. Set
LINDORM_HOSTto the MySQL-compatible VPC address.Local machine: Enable a public endpoint first. In the Lindorm console, go to Database Connections > Wide Table Engine, then click Enable Public Endpoint. Set
LINDORM_HOSTto the MySQL-compatible Internet address.
Step 2: Create a connection and run a query
The following example creates a connection to LindormTable and runs a SHOW DATABASES query:
var mysql = require('mysql2');
var connection = mysql.createConnection({
// The MySQL-compatible endpoint for LindormTable
host: process.env.LINDORM_HOST,
// The port for the MySQL protocol. Fixed value: 33060.
port: 33060,
// Your LindormTable username
user: process.env.LINDORM_USER,
// Your LindormTable password
password: process.env.LINDORM_PASSWORD,
// The database to connect to. Default: 'default'
database: 'default',
// Connection timeout in milliseconds. Default: 10000.
connectTimeout: 10000
});
connection.connect(function(err) {
if (err) {
throw err;
}
console.log("Connection established.");
connection.query('show databases', function(err1, results, fields) {
if (err1) {
throw err1;
}
console.log(fields);
console.log(results);
});
connection.end(function(err2) {
if (err2) {
throw err2;
}
console.log("Connection closed.");
});
});Connection parameters:
| Parameter | Description | Default |
|---|---|---|
host | The MySQL-compatible endpoint for LindormTable. | — |
port | The port for the MySQL protocol. Fixed value: 33060. | 33060 |
user | Your LindormTable username. | — |
password | Your LindormTable password. | — |
database | The database to connect to. | default |
connectTimeout | The connection timeout period, in milliseconds. | 10000 |
Step 3: Verify the connection
If the connection succeeds and your instance contains only the default database, the output is:
Connection established.
Connection closed.
[ `DATABASE` VARCHAR(0) ]
[ { DATABASE: 'default' }, { DATABASE: 'information_schema' } ]Even when only the default database exists, the query returns two rows: default and information_schema.