How to connect an app to a remote database
Updated 11/29/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 pricingRelated tutorials
How to install and secure PostgreSQL on Ubuntu
Install the most advanced open-source database and configure it safely: password, scram-sha-256 authentication and controlled access.
Updated 11/29/2026DatabasesHow to install and secure MySQL on Ubuntu
Install MySQL or MariaDB on your VPS and secure it properly: root password, removing test accounts and controlled access.
Updated 11/29/2026DatabasesHow to create a MySQL user and grant privileges
Create dedicated MySQL users per application and grant exactly the privileges needed, step by step.
Updated 11/29/2026