CloudLinux.ro
Databases

How to connect an app to a remote database

Updated 7/20/2026

When the app and the database run on different servers, you must allow remote access, but carefully. Here are the steps for MySQL and PostgreSQL.

MySQL

1. In mysqld.cnf, allow external connections:

bind-address = 0.0.0.0

2. Create a user that connects from the app's IP:

CREATE USER 'app'@'APP_IP' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON app_db.* TO 'app'@'APP_IP';
FLUSH PRIVILEGES;

PostgreSQL

1. In postgresql.conf: listen_addresses = '*'

2. In pg_hba.conf, allow the app IP:

host app_db app APP_IP/32 scram-sha-256

Security: mandatory

  • Firewall: allow the database port (3306 MySQL, 5432 PostgreSQL) only from the app's IP, not from everyone.
  • Dedicated user: do not use root; bind the user to the exact app IP.
  • Ideally, a private network: if the servers are in the same location, use a private link instead of the public internet.

Exposing a database to the internet without these measures is one of the most common causes of breaches. See also the server hardening guide.

Put this guide into practice on your own VPS

EU cloud servers, billed hourly, ready in minutes, with one-click installs for dozens of apps.

See pricing

Related tutorials