How to Install MySQL on Ubuntu
How to Install MySQL on Ubuntu: Complete Guide

Introduction
MySQL is one of the most popular open-source relational database management systems, widely used by web hosting providers and developers worldwide. Whether you’re setting up a dedicated server, VPS hosting, or cloud hosting environment, MySQL serves as the backbone for countless web applications and websites. In this comprehensive guide by HostingHome, we’ll walk you through the complete process of installing MySQL on Ubuntu.
Why Choose MySQL for Your Hosting Environment?
MySQL has become the go-to database solution for shared hosting, dedicated hosting, and managed hosting services due to its reliability, performance, and ease of use. Major hosting providers rely on MySQL to power everything from simple blogs to complex e-commerce platforms. When combined with Ubuntu’s stability, you get a robust foundation for any web hosting infrastructure.
Prerequisites
Before beginning the MySQL installation on your Ubuntu server hosting environment, ensure you have:
- Ubuntu 18.04, 20.04, 22.04, or newer
- Root or sudo privileges
- Basic command-line knowledge
- Stable internet connection
Step 1: Update Your Ubuntu System
Start by updating your package repository to ensure you have access to the latest MySQL packages available for Ubuntu hosting:
sudo apt update && sudo apt upgrade -y
Step 2: Install MySQL Server
Ubuntu’s package repository includes MySQL server packages optimized for hosting environments. Install MySQL using the following command:
sudo apt install mysql-server -y
This command will download and install the MySQL server package along with all necessary dependencies. The installation process typically takes a few minutes depending on your server’s specifications and hosting provider’s network speed.
sudo systemctl start mysql
sudo systemctl enable mysql
Verify that MySQL is running properly:
sudo systemctl status mysql
Step 3: Secure Your MySQL Installation
Security is paramount in any hosting environment. MySQL includes a security script that helps you secure your database server:
sudo mysql_secure_installation
Follow the prompts to set a root password and remove insecure defaults. This is crucial for secure hosting environments.
This script will prompt you to configure the Validate Password Plugin
, which helps enforce strong password policies for MySQL users.
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?Press y|Y for Yes, any other key for No:
If you enable validation, the script will prompt you to choose a password validation level. Selecting level 2—the strongest level
—will enforce strict password requirements. This means you’ll encounter errors when setting passwords that don’t include a mix of uppercase and lowercase letters, numbers, special characters, or if the password is based on common dictionary words.
There are three levels of password validation policy:LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary filePlease enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:1
After installing MySQL, you’ll be prompted to submit and confirm a root password:
Please set the password for root here.
New password:
Re-enter new password:
Y
, to all security questions to maintain the highest security standards.- Set root password: Create a strong password for the MySQL root user
- Remove anonymous users: Eliminate security risks from unnamed user accounts
- Disable root remote login: Prevent remote access to the root account
- Remove test database: Delete the default test database
- Reload privilege tables: Apply all security changes
Step 4: Switching Authentication Method for Root User
By default, MySQL may use the auth_socket
plugin for root authentication. If you prefer to use a password for connecting as root, you’ll need to switch to mysql_native_password
open the MySQL prompt:
sudo mysql
Check current authentication methods:
SELECT user,authentication_string,plugin,host FROM mysql.user;
You may see something like:
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | | auth_socket | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)
This shows the root user is using auth_socket
Update root authentication method:
Replace your_strong_password
with a secure password of your choice.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
Apply the changes:
FLUSH PRIVILEGES;
Verify the changes:
SELECT user,authentication_string,plugin,host FROM mysql.user;
You should now see:
+------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | | mysql_native_password | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)
Exit the MySQL shell:
exit;
-p
flag and your new password:mysql -u root -p
Step 5: Configure MySQL for Hosting Environments
Log into the MySQL console to begin configuration:
mysql -u root -p
Create a Database User for Web Hosting
For hosting applications, create dedicated database users rather than using the root account:
CREATE USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
⚠️ Important: Some versions of PHP have compatibility issues with the caching_sha2_password
authentication plugin, which may lead to connection problems—especially with applications like phpMyAdmin.
If you’re planning to use MySQL with a PHP-based application, it is recommended to create a user that uses the older (yet secure) mysql_native_password
plugin instead.
Once you’ve created the new user, the next step is to assign the necessary privileges. You can use the following general syntax to grant permissions to the user:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
Apply the changes:
FLUSH PRIVILEGES;
Integrating MySQL with Popular Hosting Control Panels
HostingHome recommends integrating MySQL with popular control panels used in web hosting:
1. cPanel Integration
Most shared hosting providers use cPanel, which seamlessly integrates with MySQL for database management.
2. Plesk Configuration
For VPS hosting and dedicated server environments, Plesk offers excellent MySQL management capabilities.
3. DirectAdmin Setup
Budget hosting providers often use DirectAdmin, which provides straightforward MySQL database management.
Performance Optimization for Hosting Providers
Monitor MySQL Performance
Use these commands to monitor MySQL performance in your hosting environment:
mysql -u root -p -e "SHOW PROCESSLIST;"
mysql -u root -p -e "SHOW STATUS LIKE 'Threads_connected';"
Backup Strategies for Hosting
Implement regular backups for your hosting databases—sample command to dump a database backup.
mysqldump -u root -p database_name > backup_$(date +%Y%m%d).sql
Security Best Practices for Hosting Environments
- Regular Updates: Keep MySQL updated on your hosting servers
- Strong Passwords: Enforce complex passwords for all database users
- Limited Privileges: Grant only necessary permissions to hosting accounts
- Network Security: Configure firewalls for your hosting infrastructure
- Regular Audits: Monitor database access logs in hosting environments
Scaling MySQL for Growing Hosting Needs
As your hosting business grows, consider these scaling strategies:
Master-Slave Replication
For high-traffic hosting environments, implement MySQL replication for load distribution.
Database Clustering
Large hosting providers benefit from MySQL cluster configurations for high availability.
Cloud Database Solutions
Consider managed database services for enterprise hosting solutions.
Conclusion
Installing MySQL on Ubuntu provides a solid foundation for any web hosting environment. Whether you’re running a small shared hosting service or managing enterprise-level dedicated servers, MySQL’s reliability and performance make it an excellent choice for hosting providers.
HostingHome’s step-by-step guide ensures you have a secure, optimized MySQL installation ready for production hosting environments. Remember to regularly update your MySQL installation, implement proper backup strategies, and monitor performance to maintain optimal hosting services.