How to Install MySQL On Ubuntu 24.04

  • Francis Ndungu

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:

Install MySQL Using APT

The MySQL package is maintained in the Ubuntu central repositories. Install the package by following the steps below.

  1. SSH to your Ubuntu server and update the package information index.

    CONSOLE
    $ sudo apt update
    
    1. Install the MySQL server.

    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

  1. Run the mysql_secure_installation command.

    CONSOLE
    $ sudo mysql_secure_installation
    
  2. 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!
    
  3. Log in to the database server.

    CONSOLE
    $ sudo mysql -u root -p
    
  4. 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;

  1. Create a sample company_db

    MySQL
    mysql> CREATE DATABASE company_db;
    

    Output:

    Query OK, 1 row affected (0.00 sec)
    
  2. Switch to the new company_db database.

    MySQL
    mysql> USE company_db;
    

    Output;

    Database changed
    
  3. Create a sample customers database.

    MySQL
    mysql> 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)
    
  4. Insert sample records into the customers table.

    MySQL
    mysql> 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:

    MySQL
    Query OK, 1 row affected (0.00 sec)
    
  5. Query the customers table to ensure the records are in place.

    MySQL
    mysql> 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.

  6. Log out from the MySQL database server.

    MySQL
    mysql> 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.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI