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 whitelists.
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 LindormTable endpoint for MySQL. For more information about how to obtain the LindormTable endpoint for MySQL, see View endpoints.
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 on a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the public endpoint for the instance in the Lindorm console: In the left-side navigation pane, select
. On the Wide Table Engine tab, click Enable Public Endpoint.If you use a VPC to access the Lindorm instance, specify the LindormTable VPC endpoint for MySQL in the value of host. If you use the Internet to access the Lindorm instance, specify the LindormTable Internet endpoint for MySQL in the value of host.
port
The port used to access LindormTable by 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.
Establish a connection and use LindormTable SQL to perform operations in LindormTable. The following code block provides an example on how to use LindormTable SQL to 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 code provides a complete example on how to connect to and use LindormTable by using the MySQL2 driver of Node.js:
var mysql = require('mysql2');
var connection = mysql.createConnection({
// Specify the LindormTable endpoint for MySQL.
host: 'ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com',
// Specify the port used to access LindormTable by using MySQL.
port: 33060,
// Specify the username used to connect to LindormTable.
user: 'user',
// Specify the password used to connect to LindormTable.
password: 'test',
// Specify the database to which you want to connect.
database: 'default ',
// Specify the connection timeout period. The default timeout period is 10,000 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' } ]