Have you just gotten a VPS (Virtual Private Server) and are wondering how to install MySQL on it? Don’t worry, I’ve been there too. When I got my first Ubuntu server, I had no idea where to begin. MySQL sounded like a big, complicated thing. But the truth is—it’s not that hard.
In this guide, I’ll show you how to install MySQL on your Ubuntu VPS, step by step. I’ll also explain the words you may not know, give examples, and walk you through the process like we’re sitting side by side.
What is MySQL?
Let’s start with the basics. MySQL is a database management system. That means it’s a tool that helps you store, organize, and retrieve information.
Think of it like a filing cabinet, but digital. When you create a blog, an online store, or a user login system, you need a place to store data—like posts, products, or passwords. MySQL is one of the most popular tools for that.
It works with many programs, like WordPress, Magento, and even custom apps.
What is Ubuntu?
Ubuntu is a kind of Linux operating system. It’s free and widely used on servers. If you bought a VPS, chances are it’s running Ubuntu.
A VPS, or Virtual Private Server, is like renting a computer in the cloud. You can log into it from anywhere and use it to host websites, databases, or apps.
Why Use MySQL?
There are many reasons people choose MySQL:
- It’s free and open source.
- It works fast and handles big data.
- It’s supported by many web hosting companies.
I like MySQL because it’s simple to use once you learn the basics, and it runs smoothly even on a small VPS.
What You Need First
Before installing MySQL, make sure you have:
- A VPS with Ubuntu (version 20.04 or 22.04 is great)
- Root access or a user with
sudo
privileges - A stable internet connection
- Terminal access (via SSH)
Don’t know what SSH is? It’s a way to connect to your server securely from your local machine (your laptop or desktop).
Here’s a command I use to connect to my VPS from my computer:
ssh your-username@your-vps-ip
Replace your-username
and your-vps-ip
with your actual username and IP address.
Step 1: Update Your Server
First things first. Always update your server. Why? Because it gets the latest security fixes and tools.
Type this:
sudo apt update && sudo apt upgrade -y
This tells Ubuntu to check for updates and install them. It may take a few minutes.
Step 2: Install MySQL
Now it’s time to install MySQL.
Just run this command:
sudo apt install mysql-server -y
That’s it. Ubuntu will download and install MySQL for you. The -y
just means “yes” to all prompts.
After that, MySQL is installed. But we’re not done yet.
Step 3: Check If MySQL Is Running
Let’s make sure everything is working.
Type:
sudo systemctl status mysql
You should see something like:
Active: active (running)
If it’s not running, start it with:
sudo systemctl start mysql
And if you want it to start every time your server boots, use:
sudo systemctl enable mysql
Step 4: Secure MySQL
This is an important step. MySQL comes with a basic setup, but we want it to be more secure.
Run the following command:
sudo mysql_secure_installation
You’ll get a series of questions. Here’s how I usually answer them:
- Set up VALIDATE PASSWORD plugin? (optional)
You can say No, unless you want strong password checks. - Change root password?
Say Yes, and set a strong password. - Remove anonymous users?
Yes - Disallow root login remotely?
Yes - Remove test database?
Yes - Reload privilege tables?
Yes
These steps help block hackers and keep your database safe.
Step 5: Log In to MySQL
Now let’s test it. You can log in to MySQL like this:
sudo mysql
You’ll see a welcome message and the mysql>
prompt.
To see which databases exist, type:
SHOW DATABASES;
To exit, type:
exit;
That’s it—you’re inside the database system.
Step 6: Create a New Database and User
This part is optional, but useful if you want to build a website.
Let’s say you want to create a database for your blog. You’d type:
sudo mysql
Then inside the prompt:
CREATE DATABASE blog;
CREATE USER 'bloguser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON blog.* TO 'bloguser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
This creates a new database called blog
, a user named bloguser
, and gives that user full access to the blog
database.
I always create separate users for each project. That way, if one part gets hacked, the others stay safe.
Three Quick Checks (Before You Continue)
Let’s make sure everything is set correctly.
- Is MySQL installed and running?
Check withsudo systemctl status mysql
. - Can you log in?
Trysudo mysql
. - Can you see your new database?
UseSHOW DATABASES;
once logged in.
If all three work, you’re ready to use MySQL with your website or app.
What to Do Next?
Here are some things you might want to do after this:
- Connect MySQL to WordPress or another CMS
- Use phpMyAdmin for a web interface (a tool to manage MySQL in your browser)
- Set up backups to keep your data safe
I personally use a mix of command line and phpMyAdmin. It depends on what I’m working on.
Why This Setup Works Well
I’ve used this setup on multiple projects. Here’s why I think it’s solid:
- It runs well even on a small 1 GB VPS.
- MySQL is easy to learn but powerful.
- You can control everything from the terminal.
If you ever switch to something bigger like MariaDB or PostgreSQL, the ideas stay the same.
Tips for Beginners
If you’re just starting, here are a few tips I’ve learned the hard way:
- Write down passwords you create. It’s easy to forget them.
- Always back up your database before making big changes.
- Don’t use the root user for apps—create new users with limited access.
These small habits saved me from big mistakes later.
Summary
Let’s go over what you did today:
- Logged into your Ubuntu VPS
- Updated your server
- Installed MySQL
- Secured the installation
- Logged in and created your first database
Now your server is ready for websites, apps, or anything that needs a database.
You Learned:
- What MySQL is and why it’s useful
- How to install and test it
- How to keep it secure
- How to create users and databases
It may seem like a lot, but you’ve done a great job. Once you do it a few times, it becomes second nature.