How to install and configure MySQL on Ubuntu 2025 easy way
Knowing how to install and configure MySQL on Ubuntu isnât just another technical task on a checklist. Itâs a foundational skill. It's the first real step you take in bringing a web application to life. I've spent years wrangling servers, and trust me, Iâve seen countless projects stumble, crash, and burn, all because of a poorly configured database. Itâs almost always the small, overlooked security steps at the beginning that cause the biggest migraines later.
This isn't just about running commands. It's about understanding why you're running them.
In this guide, I'm going to walk you through everything. Every command, every prompt, and every check. Let's get your MySQL server running safely and efficiently. Let's ditch the confusion and get it done right.
The "I Just Need the Commands" Section
For the experienced pros who just need a refresher, here's the "too long; didn't read" version to get you up and running securely.
sudo apt update && sudo apt upgrade -y
sudo apt install mysql-server
sudo mysql_secure_installation
sudo systemctl status mysql.service
sudo mysql
If you want the detailed breakdown of why these commands are essential, especially the security script, continue reading.
Before You Start: The Pre-Flight Check
Before we dive in, let's go through a quick checklist. Getting these things in order first guarantees a smooth ride.
A server running a recent Ubuntu LTS version. This guide is perfect for Ubuntu 24.04, 22.04, or 20.04. "LTS" means Long-Term Support. I always build on an LTS release; it's like building a house on solid rock instead of shifting sand.
A non-root user with sudo privileges. This is a critical security best practice. Running commands as root is like walking around with the master key to an entire city. One mistake, and you can cause irreversible damage.
A working internet connection. Your server needs to connect to Ubuntuâs software repositories to download the MySQL package.
With these three things, you're all set.
Step 1: Getting MySQL onto Your Server
Our first move is installing the software. We'll use Ubuntu's default repositories. Why? Because the versions there have been thoroughly tested to work perfectly with your specific Ubuntu version. Itâs the most stable path.
First, update your package index. It's like checking the menu for the daily specials before ordering.
sudo apt update
Now, install the server package:
sudo apt install mysql-server
The system will show you what's being installed. Just press Y and Enter to continue. In a minute or two, it'll be done.
Step 2: The "Do Not Skip This" Security Lockdown
Congratulations, MySQL is installed! But right now, itâs a new house with default factory locks. This next step is where we harden it, and itâs the most critical part of this entire guide.
Run the security script:
sudo mysql_secure_installation
This script will ask you a series of yes-or-no questions. I'll break down what each one means and what I recommend.
The Password Bouncer (Validate Password Component)
"Do you want to set up a password strength checker?" This forces new MySQL passwords to be strong.
My Recommendation: Press Y. Choose 1 for MEDIUM. It's the Goldilocks choice: secure, but not so strict you'll forget the password. You'll then set your new MySQL root password.
Kicking Out the Strangers (Remove Anonymous Users)
MySQL creates an "anonymous" user for testing. This is a massive security hole.
My Recommendation: Press Y. This is a no-brainer.
Locking the Front Door (Disallow Remote Root Login)
"Should the root user be allowed to log in from other computers?" This is like saying the building's superintendent can only use their master key when they are inside the building. It massively reduces risk.
My Recommendation: Press Y. This is a huge security win.
Cleaning House (Remove the Test Database)
MySQL also creates a sample test database. Itâs useless and insecure on a real server.
My Recommendation: Press Y. Get rid of it.
Reloading the Privilege Tables
"Do you want to apply all these changes right now?" This is the final "Save" button.
My Recommendation: Press Y. This locks in all our new settings.
And you're done! Your MySQL installation is now properly secured.
Step 3: Is it... alive? (Checking Status and Version)
Let's make sure the engine is purring. We can ask systemd for a status report:
sudo systemctl status mysql.service
You should see a line highlighted in green saying active (running). This is our confirmation. (Press Q to exit this view.)
Next, let's check the version:
mysql --version
This will show the specific version number. Seeing both "active" and the version number confirms our success.
Step 4: The "Master Key" Problem (Creating a Dedicated User)
A common mistake I see is connecting an application directly to MySQL using the root user. This is a terrible security practice.
Think of the root user as the building's master key. You would never hand that to a single tenant. Instead, you give them a key that only works for their specific apartment. That's what we're doing here.
First, log in to the MySQL prompt as root:
sudo mysql
Now, run these SQL commands. Be sure to replace the placeholder values with your own.
CREATE DATABASE your_database_name; CREATE USER 'your_new_user'@'localhost' IDENTIFIED BY 'a_very_strong_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_new_user'@'localhost'; FLUSH PRIVILEGES; exit;
Let's break that down:
CREATE DATABASE: Makes a new, empty database.
CREATE USER: Creates the new user. The '@'localhost' part means this user can only connect from the server itself, which is a great security default.
GRANT ALL PRIVILEGES: This is the "giving the key" step. It gives your new user full control over only the new database.
FLUSH PRIVILEGES: This reloads the permissions, telling MySQL to recognize the changes.
Now you have a secure database and a dedicated user. My principle is: One application, one database, one user.
"Help, It Broke!" â Common Troubleshooting
Even with a perfect guide, things can go sideways.
"Access Denied!" (The auth_socket trap)
Problem: You try mysql -u root -p and enter your password, but it's rejected. Cause: This isn't a password problem. On modern Ubuntu, the root user uses a plugin called auth_socket. It checks if you're the system's root user (or using sudo). Solution: The correct way to log in is: sudo mysql.
"How do I connect from my desktop?" (Remote Access)
Problem: You want to connect from another computer, but it times out. Cause: By default, MySQL only listens for connections from localhost (the server itself). Solution (Use with Caution):
Edit the config: sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf.
Find the line bind-address = 127.0.0.1.
Change it to bind-address = 0.0.0.0 (to listen on all IPs).
Restart MySQL: sudo systemctl restart mysql.service.
CRITICAL: You must also configure your firewall (like UFW) to only allow traffic on port 3306 from your specific, trusted IP address.
"It won't start!" (Check your logs)
Problem: You changed a config file, and now MySQL won't start. Cause: 99% of the time, it's a typo in the config file. Solution: Check the logs to see why it failed. journalctl -xeu mysql.service Scroll through the output and look for the error message. The logs will almost always point you to the exact line with the problem.
Unlock all insights here:
Learn how to install and configure MySQL on Ubuntu with our easy guide. Includes commands, screenshots, and best practices for 2025 security










