What You'll Need
Everything below assumes a fresh Ubuntu VPS (22.04 or 24.04 LTS). The commands are copy-paste ready — just change the database name, username and password to your own.
- A VPS running Ubuntu and its public IP address
- SSH access — usually
rootor a sudo-enabled user - 5–10 minutes per database
THAPA7), then come straight back to this step.
Grab a Powerful VPS for up to 80% OFF
+ an extra 10% OFF with my code 🔥
Most providers charge ₹1,500–₹2,000+ a month for a real VPS. Through my link you get the same horsepower — dedicated CPU & RAM, full root access, NVMe SSD — for a fraction of that. Then stack code THAPA7 on top for an extra 10% off the already-discounted price. That's a discount on a discount — cheaper, never weaker.
- Full root / SSH access — install any DB on this page
- Dedicated RAM & CPU + lightning NVMe SSD storage
- Free domain, dedicated IP, weekly backups & AI assistant
- 30-day money-back guarantee — zero risk
THAPA7 at checkout
Affiliate link — you pay the same (or less with code THAPA7), and it supports these free tutorials. 💚
SSH In & Update the Server
Log into your VPS over SSH using its IP address (or domain, if you've pointed one):
ssh root@your_server_ip
Always refresh the package list before installing anything so you pull the latest versions:
sudo apt update && sudo apt upgrade -y
A couple of steps below depend on your release. Run lsb_release -a — jammy means Ubuntu 22.04 and noble means 24.04. You'll need that codename for MongoDB.
Install & Secure MySQL
MySQL is the classic relational database — perfect for PHP, Laravel, Node and most CRUD apps. Install the server package:
sudo apt install -y mysql-server
Make sure the service is running and starts on boot:
sudo systemctl start mysql
sudo systemctl enable mysql
sudo systemctl status mysql
Now lock it down. This script lets you set password rules, remove the anonymous user, and disable remote root login — answer Y to the safe defaults:
sudo mysql_secure_installation
Open the MySQL shell, then create a dedicated user and a database for your app:
sudo mysql
CREATE DATABASE myapp_db;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Your app connects with this string:
mysql://myapp_user:strong_password_here@localhost:3306/myapp_db
myapp_db.* — just that one database — not *.*. Never give an app account access to every database on the server.
Install PostgreSQL & Create a Role
PostgreSQL is the go-to for modern stacks — Prisma, Drizzle, Django, Rails. Install the server and its contrib extensions:
sudo apt install -y postgresql postgresql-contrib
sudo systemctl enable --now postgresql
PostgreSQL creates a system user called postgres. Switch to it and open the psql shell:
sudo -i -u postgres
psql
Create a login role and a database that role owns. Run these at the postgres=# prompt:
CREATE ROLE myapp_user WITH LOGIN PASSWORD 'strong_password_here';
CREATE DATABASE myapp_db OWNER myapp_user;
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp_user;
\q
Type exit to leave the postgres user. Your connection string:
postgresql://myapp_user:strong_password_here@localhost:5432/myapp_db
Giving the role ownership of its own database is safer than handing it SUPERUSER. It's everything your app needs and keeps the rest of the server out of reach. The default port is 5432.
Install MongoDB Community Edition
MongoDB isn't in Ubuntu's default repositories, so you add MongoDB's official one. First install the tools needed to import its signing key:
sudo apt install -y gnupg curl
Import the public GPG key for the current release (8.0):
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
Add the repository. Match the codename to your Ubuntu version — use noble for 24.04 or jammy for 22.04:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
Refresh the package list and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Start the service, enable it on boot, and confirm it's up:
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
Test it by opening the Mongo shell:
mongosh
By default MongoDB listens only on localhost (port 27017), so your app connects with:
mongodb://127.0.0.1:27017/myapp_db
security.authorization: enabled in /etc/mongod.conf.
The Three at a Glance
| Database | Default port | Best for |
|---|---|---|
| MySQL | 3306 | PHP / Laravel, classic relational CRUD apps |
| PostgreSQL | 5432 | Prisma / Drizzle, Django, advanced SQL & JSON |
| MongoDB | 27017 | Document data, flexible schemas, MERN apps |
Quick Troubleshooting
| Symptom | Likely fix |
|---|---|
| Service won't start | Check logs with sudo journalctl -u mysql (or mongod) for the exact error. |
| MongoDB install fails | Wrong codename in the repo line — it must match your Ubuntu version (noble / jammy). |
| App can't connect | Double-check the username, password and database name in your connection string. |
| Access denied (MySQL) | Re-run the GRANT and FLUSH PRIVILEGES for the right host ('localhost'). |
Frequently Asked Questions
How do I install MySQL on a VPS?
Run sudo apt install -y mysql-server on Ubuntu, start and enable it with systemctl, secure it with sudo mysql_secure_installation, then create a user and database from the sudo mysql shell. MySQL listens on port 3306.
How do I install PostgreSQL on a VPS?
Install with sudo apt install -y postgresql postgresql-contrib, switch to the postgres user, open psql, and create a login role plus a database it owns. PostgreSQL listens on port 5432.
How do I install MongoDB on a VPS?
MongoDB isn't in Ubuntu's default repositories — add the official MongoDB 8.0 repo and GPG key (matching noble for 24.04 or jammy for 22.04), then sudo apt install -y mongodb-org and start mongod. It listens on port 27017.
Which database should I use — MySQL, PostgreSQL or MongoDB?
MySQL suits PHP/Laravel and classic relational apps, PostgreSQL fits modern stacks like Prisma, Drizzle and Django, and MongoDB is best for flexible document data such as MERN apps. You can run all three on the same VPS.
What are the default database ports?
MySQL 3306, PostgreSQL 5432, MongoDB 27017. By default each listens only on localhost — the safest setup for an app on the same server.
Wrapping Up
You now know how to install and secure the three databases you'll actually use in production — MySQL, PostgreSQL and MongoDB — on your own VPS, complete with users, databases and connection strings ready to drop into a .env file. From here, point your backend at the right string and you're live.
Next step: wire one of these into a real deployment. Our guide on hosting a full-stack Next.js app with PostgreSQL on a VPS picks up exactly where this leaves off.
Discussion