This topic describes how to use the MySQL2 driver of Node.js to connect to and use LindormTable.
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
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({
host: 'ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com',
port: 33060,
user: 'user',
password: 'test',
database: 'default ',
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' } ]