How to Install MySQL on Ubuntu 25.04

Updated on 25 April, 2025
How to Install MySQL on Ubuntu 25.04 header image

MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to store and manage data. It is commonly used in web applications, data platforms, and software that requires structured data handling. Installing MySQL on Ubuntu 25.04 provides a stable, consistent and secure environment for database operations across various use cases.

This article explains how to install MySQL on Ubuntu 25.04, from system preparation to securing the MySQL installation. These steps are applied on whether you're setting up a fresh server or preparing a local development environment.

Prerequisites

Before you begin, you need to:

  • Have access to an Ubuntu 25.04 instance as a non-root user with sudo privileges.

Install MySQL on Ubuntu 25.04

Ubuntu 25.04 includes MySQL packages in its default APT repositories. You can install the MySQL server directly without adding any external repositories. Follow the steps below to update your system index and install the MySQL database server on your Ubuntu instance.

  1. Update the APT's server package index.

    console
    $ sudo apt update
    
  2. Install the MySQL server package.

    console
    $ sudo apt install mysql-server -y
    
  3. Verify that MySQL is installed and check its version.

    console
    $ mysql --version
    

    Your output should be similar to the one below:

    mysql  Ver 8.4.4-0ubuntu2 for Linux on x86_64 ((Ubuntu))

Manage the MySQL System Service

MySQL server runs as a systemd service named mysql, which manages its background processes and startup behavior. Follow the steps below to enable the mysql service to start at boot and verify that its status is active and running.

  1. Enable MySQL to start automatically when the system boots.

    console
    $ sudo systemctl enable mysql
    
  2. Start the MySQL database service.

    console
    $ sudo systemctl start mysql
    
  3. View the MySQL service status.

    console
    $ sudo systemctl status mysql
    

    Your output should be similar to the one below:

    ● mysql.service - MySQL Community Server
         Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
         Active: active (running) since Wed 2025-04-23 15:45:51 UTC; 1min 8s ago
     Invocation: e97531cc30e94807a7a293e576cb307d
       Main PID: 6489 (mysqld)
         Status: "Server is operational"
          Tasks: 34 (limit: 8761)
         Memory: 437.1M (peak: 450.4M)
            CPU: 2.069s
         CGroup: /system.slice/mysql.service
                 └─6489 /usr/sbin/mysqld

Secure the MySQL Server

Securing the MySQL server is crucial to prevent unauthorized access and protect sensitive data. By default, the root user has unrestricted access to the MySQL console. Follow the steps below to set a new root password, disable insecure defaults, and improve the overall security of your MySQL server.

  1. Run the following command to start the MySQL secure installation script.

    console
    $ sudo mysql_secure_installation
    

    Follow the prompts below to secure your MySQL server by enabling password validation, enforcing strong password policies, removing anonymous users, disabling remote root access and deleting test databases.

    • VALIDATE PASSWORD COMPONENT: Enter Y and press Enter to enable password validation.
    • Password Validation Policy: Enter 2 to enforce the usage of strong passwords.
    • Remove anonymous users?: Enter Y to remove anonymous MySQL users.
    • Disallow root login remotely?: Enter Y to disable root user remote MySQL access.
    • Remove test database and access to it?: Enter Y to delete the test database.
    • Reload privilege tables now?: Enter Y to apply the changes to the privilege tables.
  2. Open the mysqld.cnf configuration file using any text editor such as nano.

    console
    $ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    
  3. Add the following directive under the [mysqld] section to enable the mysql_native_password authentication plugin.

    ini
    mysql_native_password=ON
    

    Your [mysqld] section should look like below:

    ini
    ...
    [mysqld]
    #
    # * Basic Settings
    #
    mysql_native_password=ON
    user            = mysql
    ...
    
  4. Restart MySQL to ensure the configuration changes takes effect.

    console
    $ sudo systemctl restart mysql
    
  5. Access the MySQL console as the root user.

    console
    $ sudo mysql
    
  6. Set a strong password for the root user.

    sql
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_strong_password';
    

    Replace your_strong_password with a secure password that complies with your password policy.

  7. Execute the following to refresh MySQL’s user permissions.

    sql
    mysql> FLUSH PRIVILEGES;
    
  8. Exit the MySQL database console.

    sql
    mysql> EXIT;
    

Access MySQL

Follow the steps below to access your MySQL server and manage databases and users.

  1. Log in to the MySQL server as the root user.

    console
    $ mysql -u root -p
    

    When prompted, enter the root password you created earlier.

  2. Create a sample MySQL database named app_data.

    sql
    mysql> CREATE DATABASE app_data;
    
  3. Create a new MySQL user app_user with a secure password.

    sql
    mysql> CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'your_secure_password';
    

    Replace your_secure_password with a strong password based on your password policy.

  4. Grant the database user app_user full privileges to the app_data database.

    sql
    mysql> GRANT ALL PRIVILEGES ON app_data.* TO 'app_user'@'localhost';
    
  5. Grant the database user app_user permission to create new databases.

    sql
    mysql> GRANT CREATE ON *.* TO 'app_user'@'localhost';
    
  6. Grant the database user app_user permissions to perform CRUD operations on all databases.

    sql
    mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'app_user'@'localhost';
    
  7. Reload the MySQL privilege tables to apply the new user changes.

    sql
    mysql> FLUSH PRIVILEGES;
    
  8. Exit the MySQL database console.

    sql
    mysql> EXIT;
    

Create a Sample MySQL Database

After creating the app_user database user, you can use it to create and manage databases without root privileges. In this section, you will log in as app_user, create a new database, add a table, and populate it with sample data.

  1. Log in to the MySQL database server as the user app_user.

    console
    $ mysql -u app_user -p
    

    Enter the password you configured for app_user when prompted.

  2. List all databases accessible to the current user.

    sql
    mysql> SHOW DATABASES;
    

    Your output should be similar to the one below:

    +--------------------+
    | Database           |
    +--------------------+
    | app_data           |
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    5 rows in set (0.00 sec)
  3. Create a new database named app_demo.

    sql
    mysql> CREATE DATABASE app_demo;
    
  4. Switch to the new database context.

    sql
    mysql> USE app_demo;
    
  5. Create a table named items to store sample data, including an auto-incrementing ID, name, description, and a timestamp.

    sql
    mysql> CREATE TABLE items (
             id INT AUTO_INCREMENT PRIMARY KEY,
             name VARCHAR(255) NOT NULL,
             description TEXT,
             created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
             );
    
  6. Insert sample records into the items table.

    sql
    mysql> INSERT INTO items (name, description) VALUES 
             ('Item One', 'This is the first item.'),
             ('Item Two', 'This is the second item.'),
             ('Item Three', 'This is the third item.');
    
  7. Query the items table to verify that the data was inserted correctly.

    sql
    mysql> SELECT * FROM items;
    

    Your output should be similar to the one below:

    +----+------------+--------------------------+---------------------+
    | id | name       | description              | created_at          |
    +----+------------+--------------------------+---------------------+
    |  1 | Item One   | This is the first item.  | 2025-04-23 18:02:49 |
    |  2 | Item Two   | This is the second item. | 2025-04-23 18:02:49 |
    |  3 | Item Three | This is the third item.  | 2025-04-23 18:02:49 |
    +----+------------+--------------------------+---------------------+
    3 rows in set (0.00 sec)
  8. Exit the MySQL database console.

    sql
    mysql> EXIT;
    

Conclusion

You have installed and secured MySQL on Ubuntu 25.04, created a dedicated database user with granular privileges, and used that user to create and manage a sample database. MySQL is a robust and scalable open-source relational database management system (RDBMS) that integrates seamlessly with modern application stacks such as LAMP and LEMP. It is also used as the database component for dynamic websites and server-side applications. To learn more about MySQL features, refer to the official MySQL documentation.

Comments

No comments yet.