Node.js Connection
Node.js Connection to MySQL, MariaDB and Percona
Connect a Node.js application hosted on the platform to MySQL, MariaDB, or Percona by installing a compatible Node.js database driver and using the database hostname, username, password, and database name supplied for the database node.
Create the Environment
Create a Node.js and database environment
Create an environment containing a Node.js application server together with a MySQL, MariaDB, or Percona database node.
Prepare the Database
Open the database administration panel
Use the Open in Browser action for the database node and sign in with the credentials supplied by the platform.
Create or select a database
Create the database that will be used by the Node.js application, or use an existing database.
Database host
Use the database node hostname or internal IP address for the application connection. Do not use localhost when the application and database run in separate containers.
Install the Node.js MySQL Driver
Connect to the Node.js application-server node and install the MySQL package:
npm install mysql
The package provides the connection, query, and connection-closing methods used by the example below.
Create the Database Connection
Create a JavaScript file, for example connection.js, and add the database connection settings:
var mysql = require('mysql');
var connection = mysql.createConnection({
host: '{host}',
user: '{user}',
password: '{password}',
database: '{database}'
});
connection.connect(function(err) {
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
console.log('Connected to the database.');
});
Execute a Test Query
Use the query() method to verify database access:
connection.query(
'SELECT 1 + 1 AS solution',
function(err, rows) {
if (err) {
throw err;
}
console.log('Result: ', rows[0].solution);
}
);
connection.end(function(err) {
if (err) {
return console.log(
'Error while closing connection: ' + err.message
);
}
console.log('Database connection closed.');
});
Run the script from the Node.js server:
node connection.js
A successful connection and query should return a result of 2.
Connection Pooling
For applications handling many concurrent requests, use a connection pool instead of creating a new database connection for every request.
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit: 10,
host: '{host}',
user: '{user}',
password: '{password}',
database: '{database}'
});
pool.query(
'SELECT 1 + 1 AS solution',
function(err, rows) {
if (err) {
throw err;
}
console.log(rows[0].solution);
}
);
Production recommendation
Store database credentials in environment variables instead of hardcoding them directly into the application source.
Expected Result
The Node.js application establishes a database connection using the configured hostname and credentials, executes a test SQL query successfully, returns the result, and closes or releases the connection correctly.
Important Notes
- Do not use
localhostwhen the database is running in another container. - Install a compatible MySQL driver in the Node.js application.
- Use the database hostname without
http://orhttps://. - Use a dedicated database user with only the required privileges.
- Use connection pooling for applications with concurrent database requests.
- Store credentials in environment variables for production deployments.
Common Issues and Solutions
npm install mysql in the application project and redeploy or restart as required.