This topic describes how to use the MySQL2 driver of Node.js to connect to and use LindormTable.
Prerequisites
The MySQL compatibility feature is enabled for the instance. For more information, see Enable the MySQL compatibility feature.
The IP address of your client is added to the whitelist of your Lindorm instance. For more information, see Configure a whitelist.
Procedure
Install Node.js. You can download the installation package of Node.js from the Node.js official site.
Install the MySQL2 driver. You can download the MySQL2 driver from the NPM MySQL2 page.
Configure connection parameters.
connection = mysql.createConnection({ host: 'ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com', port: 33060, user: 'user', password: 'test', database: 'default', connectTimeout: 10000 });Parameters
Parameter
Description
host
The MySQL-compatible endpoint for LindormTable. For more information about how to obtain the endpoint, see View connection addresses.
ImportantIf your application is deployed on an ECS instance, we recommend that you use a VPC to connect to the Lindorm instance to ensure higher security and lower network latency.
If your application is deployed locally, enable a public endpoint in the console before you connect to the Lindorm instance over the public network. To do this, choose . On the Wide Table Engine tab, click Enable Public Endpoint.
If you access the Lindorm instance over a virtual private cloud (VPC), set host to the MySQL-compatible VPC address. If you access the Lindorm instance over the public network, set host to the MySQL-compatible Internet address.
port
The port used to access LindormTable using MySQL. The value of this parameter is fixed to 33060.
user
If you forget your password, you can change the password in the cluster management system of LindormTable. For more information, see Manage users.
password
database
The name of the database to which you want to connect. By default, your client is connected to a database named default.
connectTimeout
The connection timeout period. Unit: millisecond. Default value: 10000.
Create a connection to LindormTable to run queries using the wide table SQL syntax. For example, you can query all databases.
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."); }); });
Sample code
The following is the complete sample code:
var mysql = require('mysql2');
var connection = mysql.createConnection({
// The MySQL-compatible endpoint for LindormTable
host: 'ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com',
// The port for the MySQL protocol of LindormTable. The value is fixed to 33060.
port: 33060,
// The username for LindormTable.
user: 'user',
// The password for LindormTable.
password: 'test',
// The name of the database to connect to.
database: 'default ',
// The connection timeout. The default value is 10000 milliseconds.
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.");
});
});If the current instance contains only a database named default, the following result is returned:
Connection established.
Connection closed.
[ `DATABASE` VARCHAR(0) ]
[ { DATABASE: 'default' }, { DATABASE: 'information_schema' } ]