Application Connection to MySQL MariaDB

Node.js Application Connection to MySQL MariaDB

MySQL and MariaDB are among of the most popular open source SQL databases, used by world’s largest organizations. In this guide we’ll overview a simple example of Node.js application connection to MySQL or MariaDB server.

1. Log into your Apiqcloud account and create an environment with MySQL (or MariaDB) database server, we’ll also add a NodeJS compute node for this tutorial.

     

For this tutorial, we’ll use a single environment with both instances inside.

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

 

3. Once connected, get an official MySQL driver for Node.js (compatible with MariaDB) by executing the following command:

npm install mysql

Note: MySQL driver for NodeJS 10 is currently in testing, so if the deprecation warnings are shown while operating this server version, you may need to install the testing version:

npm install mysqljs/mysql​

Installation will be finished in a moment.

4. Prepare a simple Node.js script to verify connection. Create a file with the .js extension, using any text editor of your choice (e.g. vim 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 highlighted placeholders in the code above should be adjusted using the appropriate connection information (is provided within the email for your MySQL container):  
  • {user} - username to log into the database with
  • {password} - password for the appropriate user
  • {host} - link to your MySQL 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.

5. Run code with the appropriate command:
node script.js


For successful connection a “You are connected!” phrase will be displayed in terminal, otherwise error description will be provided. Now, when you are sure your database container is accessible, expand the code to execute some real actions on your DB server.

  • Node.js Application Connection to MySQL/MariaDB
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Deploy Node.js Project > via Archive / URL

Upload and Deploy Your Node.js Application With Apiqcloud you can easily upload and deploy...

Deploy Node.js Project Via GIT SVN

Deploy Node.js Project via GIT/SVN With Apiqcloudyou can host any public or your private...

MongoDB Node.js Connection

Node.js Application Connection to MongoDB MongoDB is a popular NoSQL databases, which is...

How to Connect PostgreSQL with Node.js Application

PostgreSQL is one of the most popular and advanced open-source database solutions. Within...

PHP Accelerators

A PHP accelerator is a PHP extension designed to improve the performance of software applications...