How to Install MySQL On Ubuntu 24.04
31 Mar, 2026
MySQL is a widely used database system that leverages Structured Query Language (SQL). It's often paired with web servers like Apache, LightSpeed, and Nginx, and scripting languages such as PHP, Perl, and Python. Known for its stability, MySQL is popular among developers for production use and powers content management systems like WordPress, Joomla, and Drupal. The database management system offers high security, scalability, and performance, especially in transactional environments. MySQL's complete workflow and reduced total cost of ownership give developers the flexibility they need.
This guide shows you how to install MySQL on Ubuntu 24.04.
Prerequisites
Before you begin:
- Purchase an Ubuntu 24.04 VPS server. If you don't have a VPS server, sign up with Vultr and get upto $300 worth of free credit to test the Vultr platform.
-
SSH to your VPS server using PuTTY for Windows or run the following command if you're using Linux or Mac.
console$ ssh username@vps_server_public_ip_address -
Create a non-root user with sudo privileges. Read our guide on How to Create a Non-Root Sudo User on Ubuntu 24.04. You'll use this user's account to run the commands in this guide.
Install MySQL Using APT
The MySQL package is maintained in the Ubuntu central repositories. Install the package by following the steps below.
-
SSH to your Ubuntu server and update the package information index.
console$ sudo apt update -
Install the MySQL server using
apt.console$ sudo apt install mysql-server -y
Secure the MySQL Server
A default MySQL installation isn't secure because it often comes with default settings that can hackers can exploit. For example, it may have a default root user with no password, anonymous user accounts, and accessible test databases. Follow the steps below to secure MySQL
-
Run the
mysql_secure_installationcommand.console$ sudo mysql_secure_installation -
Reply with the following responses to secure MySQL.
consoleSetup 'validate password' plugin? [Y/N]Y Password Validation Policy Level: 2 Remove anonymous users: Y Disallow root login remotely? [Y/N] Y Remove test database and access to it? [Y/N] Y Reload privilege tables now? [Y/N] YOutput:
All done! -
Log in to the database server.
console$ sudo mysql -u root -p -
Press Enter without providing any password. MySQL should log you in using
auth_socket, a method that allows users to log in to MySQL without a password, using their system user credentials.
Create Test Database
After installing logging in to your MySQL server, create a test database, a table, and insert some records to ensure the database is working by following the steps below;
-
Create a sample
company_dbMySQLmysql> CREATE DATABASE company_db;Output:
Query OK, 1 row affected (0.00 sec) -
Switch to the new
company_dbdatabase.MySQLmysql> USE company_db;Output;
Database changed -
Create a sample
customersdatabase.MySQLmysql> CREATE TABLE customers ( customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50) );Output:
MySQLQuery OK, 0 rows affected (0.01 sec) -
Insert sample records into the customers table.
MySQLmysql> INSERT INTO customers (first_name, last_name) VALUES ('JOHN', 'DOE'); INSERT INTO customers (first_name, last_name) VALUES ('MARY', 'SMITH'); INSERT INTO customers (first_name, last_name) VALUES ('PETER', 'HENRY');Output:
MySQLQuery OK, 1 row affected (0.00 sec) -
Query the customers table to ensure the records are in place.
MySQLmysql> SELECT customer_id, first_name, last_name FROM customers;Output:
MySQL+-------------+------------+-----------+ | customer_id | first_name | last_name | +-------------+------------+-----------+ | 1 | JOHN | DOE | | 2 | MARY | SMITH | | 3 | PETER | HENRY | +-------------+------------+-----------+ 3 rows in set (0.00 sec)The MySQL database is working as expected.
-
Log out from the MySQL database server.
MySQLmysql> EXIT;Output:
Bye
Conclusion
In this guide, you have installed MySQL Server on Ubuntu 24.04. With your MySQL database now set up, you can explore various applications to enhance your server capabilities. Consider installing phpMyAdmin for a web-based database management interface, or WordPress for content management. You can deploy applications with MySQL by integrating it with Laravel, Django, or Ruby on Rails. These applications will help you make the most of your MySQL database, providing robust and dynamic solutions for your projects.