MySQL is one of the most widely used relational database management systems for websites and applications. It powers platforms such as WordPress, Laravel, Magento, WHMCS, and thousands of other web applications. This guide explains how to install and secure MySQL 8 on an Ubuntu 24.04 VPS.
Requirements
- Ubuntu 24.04 VPS
- Root or sudo access
- SSH connection to your server
Step 1 — Update Your VPS
sudo apt update
sudo apt upgrade -y
Step 2 — Install MySQL Server
sudo apt install mysql-server -y
Step 3 — Verify Installation
mysql --version
Example output:
mysql Ver 8.x.x for Linux on x86_64
Step 4 — Start and Enable MySQL
sudo systemctl enable mysql
sudo systemctl start mysql
Step 5 — Check Service Status
sudo systemctl status mysql
The service should display active (running).
Step 6 — Secure Your MySQL Installation
sudo mysql_secure_installation
During the setup, it is recommended to:
- Enable password validation.
- Set a strong password.
- Remove anonymous users.
- Disallow remote root login.
- Remove the test database.
- Reload privilege tables.
Step 7 — Log Into MySQL
sudo mysql
You should see the MySQL prompt:
mysql>
Step 8 — Create a Database
CREATE DATABASE mydatabase;
Step 9 — Create a New Database User
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
Step 10 — Grant Permissions
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
Step 11 — Exit MySQL
EXIT;
Useful MySQL Commands
sudo systemctl restart mysql
sudo systemctl stop mysql
sudo systemctl status mysql
mysql --version
Backup a Database
mysqldump -u root -p mydatabase > backup.sql
Restore a Database
mysql -u root -p mydatabase < backup.sql
Best Practices
- Always run
mysql_secure_installationafter installation. - Create separate users for each application.
- Use strong passwords.
- Perform regular database backups.
- Keep MySQL updated with the latest security patches.
Conclusion
MySQL 8 provides a secure, reliable, and high-performance database platform for modern VPS hosting. Proper configuration and regular maintenance will help ensure your applications remain stable and secure.
Related Articles
- How to Install PHP 8.4 on Ubuntu 24.04 VPS
- How to Install Nginx on Ubuntu 24.04 VPS
- How to Secure Your Linux VPS
- How to Create a New User with Sudo Access
- How to Connect to Your VPS via SSH
