Subscribe - Thapa Technical

Installing Databases on VPS Server | MYSQL, MONGODB & POSTGRESQL

Installing Databases on Ubuntu Server | Thapa Technical

Complete Guide: Installing Databases on Ubuntu Server

Learn how to install and configure MongoDB, PostgreSQL, and MySQL on your Ubuntu server for your web development projects.

🎁 Hostinger Servers Exclusive Offers 🎁

Click here to Buy India's Best Hosting 👉 www.hostg.xyz/SHGZy

For Thapa Family 💥 Use the discount code THAPA7 to get Extra 10% OFF!

👉 Watch Complete VPS Setup: Watch Tutorial

🌐 Link to VPS Security Setup Before Hosting: Security Guide

Vinod Thapa
Full Stack Developer and Instructor with 10+ years of experience. Passionate about teaching web development and helping students become industry-ready developers.

Installing MongoDB

MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. Let's go through the installation process on Ubuntu.

Search for MongoDB Community Edition

Search on Google "mongodb community edition install", and click the first link.

Choose Linux Installation

On the MongoDB website, choose Install on Linux option.

Select Ubuntu Distribution

Choose your Linux distribution, which for us is Ubuntu.

Install MongoDB Community Edition

Go directly to the "Install MongoDB Community Edition" section and copy-paste the commands into your terminal.

Check Your Ubuntu Version

We are using Ubuntu 24, so we'll follow the instructions for that version.

You can check your Ubuntu version using:

cat /etc/lsb-release

Enable and Start MongoDB Service

After installation, enable and start the MongoDB service:

Test MongoDB Installation

Mongosh is installed by default, so you can test your installation:

Use MongoDB in Your Projects

Now, MongoDB is installed locally in your VPS. To use it in your projects, use the following connection string:

mongodb://127.0.0.1/db_name

Installing PostgreSQL

PostgreSQL is a powerful, open-source object-relational database system. Let's set it up on Ubuntu.

Search for PostgreSQL Installation

Search on Google "PostgreSQL Install" and click on the first link.

Choose Your Distribution

Select your Linux distribution (Ubuntu in our case).

Install PostgreSQL

Ubuntu includes PostgreSQL by default in its package lists, so the installation is straightforward.

For other distributions, you may need to scroll down and follow additional instructions.

Switch to PostgreSQL User

PostgreSQL can only be used by the postgres user initially. Let's switch to that user:

sudo -i -u postgres

psql is a CLI tool to interact with PostgreSQL. You can run PostgreSQL queries and commands from here.

Create a Database User

Create a database user matching your Linux username:

CREATE ROLE your_username WITH LOGIN PASSWORD 'your_password';
Make sure to use SINGLE QUOTES for the password. DOUBLE QUOTES won't work. Also, the semicolon at the end is required.

Make User a SUPERUSER

For ease of use, let's make the new user a superuser:

ALTER ROLE your_username WITH SUPERUSER;

Create a Database

Create a database with the same name as your user:

CREATE DATABASE your_username;

Exit and Test as Your User

Exit out of PSQL and the postgres user, then test with your Linux user:

Get Your Connection String

Now you can connect to PostgreSQL from your projects using:

postgresql://username:password@localhost:5432/mydatabase

PostgreSQL by default runs on port 5432.

Example: postgresql://thapa:1234@localhost:5432/mydatabase

Installing MySQL

MySQL is one of the most popular relational database management systems. Let's install it on Ubuntu.

Update Package Lists

Before installing any packages, refresh your package lists:

sudo apt update

Install MySQL Server

Install MySQL server using apt:

sudo apt install mysql-server
On other distributions, you might need to add MySQL repositories before installing. Make sure to search online for specific instructions for your distribution.

Secure MySQL Installation

It's recommended to run the security script after installing MySQL:

sudo mysql_secure_installation

Check MySQL Status

Verify that MySQL is running:

sudo systemctl status mysql
# If it's not running, or it's not enabled then run these commands sudo systemctl enable mysql sudo systemctl start mysql

Create a New Database User

Create a new database user for your projects:

sudo mysql

To see all the users of MySQL:

SELECT user, host FROM mysql.user;

Create a new user:

CREATE USER user_name IDENTIFIED BY 'password';

Grant full privileges to the new user:

GRANT ALL ON *.* TO thapa WITH GRANT OPTION;

Breakdown of the GRANT command:

1. Privileges

SELECT, INSERT, UPDATE, DELETE

ALL means all privileges

Example: GRANT SELECT, INSERT ON my_database.* TO thapa;

2. ON database.table

*.* means to allow the user permission to all databases and all tables inside them

Example: GRANT SELECT, INSERT ON my_database.* TO thapa;

This allows the user access to all tables of my_database

3. TO 'user'@'host'

As we are connected to a local database, we can just use user_name

4. WITH GRANT OPTION (Optional)

This allows the user to pass their granted privileges to others

Flush privileges to apply changes:

FLUSH PRIVILEGES;

Exit MySQL using the exit command.

Test MySQL with Your New User

Open MySQL CLI using your newly created user:

mysql -u user_name -p

-u flag specifies the username to login with

-p flag prompts for the password

Get Your MySQL Connection String

Now you have your MySQL connection string:

mysql://username:password@host:port/database

Example: mysql://thapa:thapa%401234@localhost:3306/database

3306 is the default MySQL port

Pro Tip: Now that you have multiple databases installed, you can choose the right one for your specific project needs. MongoDB works well for flexible document storage, PostgreSQL for complex relational data with advanced features, and MySQL for traditional relational database needs.