How to Connect a Node.js Application with MySQL/MariaDB/Percona?

How to Connect a Node.js Application with MySQL/MariaDB/Percona?

MySQL, MariaDB, and Percona stand out as some of the most widely used open-source SQL databases, trusted by some of the world’s largest organizations. This guide will provide a straightforward example demonstrating how to establish a connection between a Node.js application and a MySQL server.

Step 1. Access your Platform as a Service (PaaS) account and set up an environment containing a MySQL database server. Additionally, include a Node.js compute node for this tutorial.

Node.js application and a MySQL server.

Step 2. Access your NodeJS server via SSH, e.g. with an embedded Web SSH client.

Web SSH client

Step 3. After establishing the connection, obtain an official MySQL driver for Node.js that is compatible with MariaDB by running the following command:


npm install mysql

Node.js Compile

Step 4. Create a basic Node.js script to validate the connection. Use any text editor to create a file with the .js extension (e.g., vi script.js).


var mysql = require('mysql');
var con = mysql.createConnection({
host: "{host}",
user: "{user}",
password: "{password}",
database: "{database}"
});
con.connect(function(err) {
if (err) throw err;
console.log("You are connected!");
});
con.end();

The placeholders within the provided code should be adjusted with the relevant connection information typically provided within the email for your MySQL/MariaDB container.

  • {user} – username to log into database with
  • {password} – password for the appropriate user
  • {host} – link to your MySQL / MariaDB container
  • {database} – database to be accessed (e.g. the default one – MySQL)

Using this script, you can check the connection to the database from your application server and, if it fails, get an error description.

Connection To The Database

Step 5. Run the code with the appropriate command


node script.js

node script.js code

Upon successful connection, the terminal will display the phrase “You are connected!” If there’s an error, it will provide a description of the issue. Once you’ve confirmed accessibility to your database container, you can expand the code to execute actual operations on your DB server.

Conclusion

Enabling a Node.js application to connect to a MySQL database offers a flexible and scalable solution for managing data within cloud-based environments. Developers can easily deploy and scale their Node.js applications while seamlessly integrating them with MySQL databases. This connection empowers developers to perform essential database operations, including data retrieval, insertion, updates, and transactions, all within the Node.js environment.

Developers can ensure reliable connectivity, optimal performance, and efficient resource utilization, thereby facilitating the development of robust and high-performing applications. With secure and stable connections established, Node.js applications can efficiently interact with MySQL databases, enabling the creation of dynamic and feature-rich web applications tailored to meet evolving business needs.

Save $100 in the next
5:00 minutes?

Register Here