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 walks you through how to install MySQL on Ubuntu.
Prerequisites
Before you begin, ensure you have:
- Ubuntu 24.04. We recommend a Digital Ocean VPS server.
- A non-root user with
sudo
privileges.
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.
CONSOLE1. Install the MySQL server.$ sudo apt update
CONSOLE$ sudo apt-get 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_installation
command.CONSOLE$ sudo mysql_secure_installation
-
Reply with the following responses to secure MySQL.
Setup '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] Y
Output:
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_db
MySQLmysql> CREATE DATABASE company_db;
Output:
Query OK, 1 row affected (0.00 sec)
-
Switch to the new
company_db
database.MySQLmysql> USE company_db;
Output;
Database changed
-
Create a sample
customers
database.MySQLmysql> CREATE TABLE customers ( customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50) ); Output: ```mysql Query 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.