MySQL is a widely used open-source relational database. In this guide, you’ll learn how to install the latest MySQL version (currently MySQL 8.0) on Ubuntu.
✅ Step 1: Update Your System
Start by updating your system packages to ensure everything is up to date:
sudo apt update && sudo apt upgrade -y
✅ Step 2: Add MySQL APT Repository (Optional but Recommended)
Ubuntu’s default repo may not have the latest MySQL version. You can add the official MySQL APT repository to ensure you’re installing the latest release:
wget https://dev.mysql.com/get/mysql-apt-config_0.8.29-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
During the setup, select the MySQL version (default is 8.0), then choose “OK” to proceed.
Update package info again after adding the repo:
sudo apt update
✅ Step 3: Install MySQL Server
Now install MySQL server:
sudo apt install mysql-server -y
The installer will automatically configure and start MySQL.
✅ Step 4: Secure MySQL Installation
Run the included security script to harden your MySQL server:
sudo mysql_secure_installation
You’ll be prompted to:
- Set root password
- Remove anonymous users
- Disallow root login remotely
- Remove test database
- Reload privilege tables
Recommended: Answer Y to all prompts for maximum security.
✅ Step 5: Check MySQL Status
Verify that MySQL is active and running:
sudo systemctl status mysql
You should see: active (running). Press q to exit.
✅ Step 6: Log In to MySQL
To access MySQL shell:
sudo mysql
This logs in as the root user using socket authentication.
🔄 Optional: Enable Password Authentication for Root
If you want to use mysql -u root -p with a password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongPassword';
FLUSH PRIVILEGES;
🔧 Useful Commands
| Task | Command |
|---|---|
| Restart MySQL | sudo systemctl restart mysql |
| Enable on boot | sudo systemctl enable mysql |
| MySQL version | mysql --version |
| MySQL shell | sudo mysql |
🚀 MySQL is Ready!
Your MySQL server is now fully installed and secured on Ubuntu. You can start creating databases and users for your applications.