Connecting a Hostinger MySQL database to a Node.js application

Connect a Node.js application on Hostinger to a MySQL database using environment variables.

Updated 21 hours ago

This guide explains how to connect a Node.js application to a MySQL database hosted on Hostinger.

Before proceeding:

  • You already have a built Node.js website or application

  • Your app requires a database connection to store data

  • You are ready to configure the database and environment variables for production

Hostinger currently offers MySQL databases only. Other database engines (PostgreSQL, MongoDB, etc.) are not supported on shared or managed hosting plans. If your application needs a different engine, you can connect to an external provider instead — see connecting a Supabase database to a Hostinger Node.js application.

Step 1 – Create a MySQL database in hPanel

  1. Log in to Hostinger hPanel

  2. Navigate to Websites → Dashboard→ Databases → Management

  3. Fill in:

    • Database name

    • Database username

    • Password (save this securely)

  4. Click Create

Hostinger hPanel MySQL Management page with the Create button highlighted

Once created, note down:

  • Database name

  • Database username

  • Database password

  • Database host (usually localhost on Hostinger)

Step 2 – Configure environment variables

Your Node.js app should never hardcode database credentials.

Instead, use environment variables.

Option A – Separate environment variables (recommended for clarity)

Create or update your .env file:

DB_HOST=localhost
DB_PORT=3306
DB_USER=databaseuser
DB_PASSWORD=databasepassword
DB_NAME=databasename
Hostinger deployment settings with environment variables section highlighted, including MySQL_HOST and MySQL_PASSWORD fields

Option B – Single database connection string

You can also define one database connection variable, depending on your application setup:

DATABASE_URL=mysql://databaseuser:databasepassword@localhost:3306/databasename
The exact format may vary depending on the library or framework you use. Hostinger supports MySQL only, so the prefix must be mysql://.

In this article, we’ll be using Option A.

Step 3 – Rebuild or restart the application

After updating environment variables, make sure to restart your application so it loads the new values.

Restart from hPanel (Recommended)

For server-side Node.js apps (e.g., Express.js, Next.js, NestJS), you can restart directly from the dashboard:

  1. Open your Website Dashboard in hPanel
  2. Click Restart
    Hostinger website dashboard with the Running status highlighted in the deployment row
The Restart option is only available for server-side Node.js applications. Static websites (React, Vue, Angular) do not run a persistent server process and do not have this option.

Alternative: Restart via SSH

If you prefer to restart via the command line:

  • Connect to SSH
  • Run: cd domains/xx/public_html (replace xx with your domain name)
  • Run: touch tmp/restart.txt

Step 4 – Ensure your website contains database connection files

Before your application can store or retrieve data, it must include a dedicated database connection file.

This file is responsible for:

  • Reading database credentials from environment variables

  • Establishing a connection to the MySQL database

  • Exporting the connection so it can be reused across the application

Keeping database logic in a single file makes the application easier to maintain and debug.

Typical database connection file structure

In most Node.js projects, the database connection file is located in a folder such as:

  • config/database.js

  • db.js

  • database/connection.js

The exact name does not matter, as long as it is imported where needed.

Default MySQL connection file example (node.js)

Below is a simple and commonly used example using the mysql2 package.

import mysql from "mysql2/promise";
const db = mysql.createPool({
  host: process.env.DB_HOST,
  port: process.env.DB_PORT || 3306,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});
export default db;

This file:

  • Reads credentials from environment variables

  • Creates a reusable connection pool

  • Prevents opening a new connection for every query

Example using a single connection string

If your application uses one database connection variable, the connection file may look like this:

import mysql from "mysql2/promise";

const db = mysql.createPool(process.env.DATABASE_URL);

export default db;
The connection string format depends on the database driver. On Hostinger, only MySQL is supported, so the prefix must be mysql://.

Example connection string:

DATABASE_URL=mysql://databaseuser:databasepassword@localhost:3306/databasename

Using the database connection in your application

Once the connection file exists, it can be imported and used anywhere in your project.

Example:

import db from "./config/database.js";

const [rows] = await db.query("SELECT * FROM users");

This confirms that:

  • The database connection file is correctly set up

  • Your application can communicate with the MySQL database

Step 5 – Create required database tables

Before storing data, your database must contain the required tables and columns.

Example SQL table

CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

You can execute this SQL:

  • Via phpMyAdmin in hPanel

  • Or programmatically from your application

phpMyAdmin SQL editor with the Go button highlighted at the bottom right

Step 6 – Test data insertion in production

  1. Open your live Node.js application

  2. Perform an action that stores data (e.g., submit a form)

  3. Check the database using phpMyAdmin

  4. Confirm:

    • A new row is created

    • Values are stored correctly

Restaurant Staff Manager dashboard with Add New Worker form and current staff card for Test Testing phpMyAdmin table row selected for restaurant_workers, showing the host record

Common issues & troubleshooting

❌ Access denied for user

  • Verify database username and password

  • Ensure the user is assigned to the database

❌ Cannot connect to MySQL server

  • Confirm host is set to localhost

  • Ensure port 3306 is used

❌ Environment variables not loading

  • Restart the Node.js application

  • Confirm .env is included in the runtime environment