Skip to content
Deploying apps4 min read

How to connect your app to the database on hosting

Short answer

Create the database and a user in MySQL Databases and give the user ALL PRIVILEGES, then put five values in your app's environment variables: host localhost (or 127.0.0.1 in Node.js and Python), port 3306, the database and user names in full with the account prefix, and the password. Move your tables by exporting an SQL file locally and importing it in phpMyAdmin.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. The five values
  2. Steps
  3. Troubleshooting
  4. On ArabyHost

Locally, your app probably connects to a database called myapp as root with no password, or to a SQLite file. On cPanel hosting there’s no root, each database has its own user, and database and user names start with your account prefix. So the connection fails after upload unless you change those values. It’s simple once you know the five values and where they go.

The five values

Value On the hosting Common mistake
Host localhost, or 127.0.0.1 in Node.js and Python Using the domain or the server IP
Port 3306
Database name cpuser_shop (prefix, then name) Writing just shop
User cpuser_shop or cpuser_app Using root or the cPanel username
Password The database user’s password Using the cPanel password

Steps

In cPanel, open MySQL Databases: create the database under Create New Database, then the user under Add New User with a strong password, then under Add User To Database pick both and tick ALL PRIVILEGES. The third step is the one people forget. The walkthrough is in create a MySQL database.

2. Move your data from your computer

If you have local tables and data, export them:

mysqldump -u root -p --default-character-set=utf8mb4 myapp > myapp.sql

Then in cPanel open phpMyAdmin, select the new database in the left panel, open the Import tab and upload the file. If the file contains a CREATE DATABASE or USE myapp line, remove it: the database exists under a different name, and your user can’t create databases. If the file is too large, see phpMyAdmin import too large.

If the project builds its own tables with migrations, skip the export and run the migrations on the server after step 3.

3. Put the values in environment variables

Don’t write the password into the code. Put it in .env or the Environment variables screen depending on the project, as in setting .env variables on hosting. Examples per language:

Laravel (.env):

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=cpuser_shop
DB_USERNAME=cpuser_app
DB_PASSWORD=strong-password

PHP with PDO:

$pdo = new PDO(
    'mysql:host=localhost;dbname=cpuser_shop;charset=utf8mb4',
    getenv('DB_USER') ?: 'cpuser_app',
    getenv('DB_PASS'),
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

Node.js with mysql2:

const mysql = require('mysql2/promise');
const pool = mysql.createPool({
  host: '127.0.0.1',
  port: 3306,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  connectionLimit: 5,
  charset: 'utf8mb4',
});

Prisma or any library that takes one URL:

DATABASE_URL="mysql://cpuser_app:[email protected]:3306/cpuser_shop"

If the password contains characters such as @, # or /, they must be URL-encoded (@ becomes %40), or choose a long password of letters and digits. Prisma notes are in Prisma on hosting.

Python with SQLAlchemy and PyMySQL:

import os
from sqlalchemy import create_engine

engine = create_engine(
    "mysql+pymysql://{u}:{p}@127.0.0.1:3306/{d}?charset=utf8mb4".format(
        u=os.environ["DB_USER"], p=os.environ["DB_PASS"], d=os.environ["DB_NAME"]
    ),
    pool_size=3,
    pool_recycle=280,
)

4. Test the connection

The quickest test is a temporary PHP file in public_html with a random name, such as dbtest-8f3k.php:

<?php
try {
    new PDO('mysql:host=localhost;dbname=cpuser_shop', 'cpuser_app', 'strong-password');
    echo 'Connected';
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

Open it in the browser. If it prints Connected, the values are right and the problem is in your app’s configuration. Then delete the file immediately, because it contains the password.

5. Mind the connection limits

On shared hosting, each database user has a limit on simultaneous connections. Node.js and Python apps that open a large pool can hit it quickly. Keep the pool small (3 to 5), and create it once at startup, not on every request. If you see error 1203, see max_user_connections.

Troubleshooting

  • Access denied for user (1045): the name lacks its prefix, the password is wrong, or privileges weren’t granted. See error 1045.
  • ECONNREFUSED or Connection refused: the host is wrong, or localhost resolved to ::1. Use 127.0.0.1. See database connection refused after deploy.
  • Unknown database: the database name lacks its prefix.
  • Table doesn't exist: the tables weren’t imported or migrated, or the table name’s letter case differs.
  • Arabic text shows as question marks: the database or connection isn’t utf8mb4. Add charset=utf8mb4 to the connection.

On ArabyHost

Every ArabyHost plan includes MySQL/MariaDB databases managed from cPanel or the Araby Panel, with phpMyAdmin and daily JetBackup backups that include databases. On the AI/Developer plans your Node.js and Python apps connect from the same account, and you can work on the database over SSH. The database the plans provide is MySQL/MariaDB, so a project that needs PostgreSQL or MongoDB has two honest options: reach a service such as Supabase through its HTTPS library (outgoing connections here are HTTPS only, so a direct connection to an external database port does not work), or run on a provider that offers that database. The panel steps are in databases.

FAQ

What is DB_HOST on cPanel?

localhost when the app and the database are on the same account, which is the normal case. In Node.js and Python use 127.0.0.1 if you see a connection error to ::1. Don't use your domain or the server's public IP.

How do I move my database from my computer to hosting?

Export it as an SQL file with your local phpMyAdmin or mysqldump, then open phpMyAdmin on the hosting, select your empty database and import the file on the Import tab. If the file is large, gzip it or split it.

Can I use PostgreSQL, MongoDB or Supabase?

The plans provide MySQL/MariaDB. On ArabyHost outgoing connections are HTTPS only, so Supabase works through its HTTPS library or REST API, but a direct connection to an external PostgreSQL or MongoDB server (ports 5432 and 27017) does not. If the project needs that, convert it to MySQL or choose a provider that allows it.

Can I connect to the hosting database from an app on my computer?

phpMyAdmin in cPanel is the easiest. To use a client such as DBeaver or TablePlus, use an SSH tunnel if your plan has SSH. On ArabyHost the MySQL port (3306) is not open to the internet, so a client cannot connect to it directly from your computer.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.