main logo

How to Install MySQL on Ubuntu 24.04 LTS

How to Install MySQL on Ubuntu 24.04 LTS

Ubuntu 24.04 LTS (Noble Numbat), the latest Long-Term Support release, offers a cutting-edge yet stable environment for deploying server applications, including the powerful MySQL database server. This tutorial will comprehensively guide you through installing MySQL Community Server on an Ubuntu 24.04 LTS system. We will cover updating your system’s package index, installing the MySQL server packages, executing the crucial security script for hardening your installation, and managing the MySQL service effectively. For users seeking advanced server management, optimization, or troubleshooting, exploring Linux Server Support can provide significant benefits.

MySQL stands as a globally recognized open-source relational database management system (RDBMS), forming the backbone of countless dynamic websites and applications. By meticulously following this guide, you will establish a secure, functional, and up-to-date MySQL installation on your Ubuntu 24.04 server.

Prerequisites

  • An Ubuntu 24.04 LTS server.
  • A non-root user account configured with sudo privileges.
  • Access to a terminal or command-line interface (CLI).

Before commencing with the installation, ensure your Ubuntu 24.04 system is fully updated and that you have a sudo-enabled user. Standard Ubuntu initial server setup tutorials can provide guidance on these preliminary configurations if needed.

Step 1 — Updating Package Index

Maintaining an updated package list is a fundamental best practice before installing any new software on a Linux system. This ensures that your system is aware of the latest available package versions and their dependencies, preventing potential conflicts and ensuring you install the most recent security patches.

sudo apt update

Optionally, you can also upgrade existing packages, though this is not strictly required before installing MySQL if you have just updated the index:

sudo apt upgrade

Step 2 — Installing MySQL

MySQL server packages are readily available within Ubuntu 24.04’s default APT repositories. You can install the mysql-server package, which typically includes mysql-community-server and all necessary dependencies for a standard installation.

sudo apt install mysql-server

During the installation process on Ubuntu 24.04, MySQL (typically version 8.0 or a newer stable release available in the repositories at the time) is usually configured to use the auth_socket authentication plugin for the root MySQL user by default. This plugin enhances security by allowing authentication based on the operating system user connecting via a local Unix socket. This means you can initially connect as the MySQL root user by prefixing your command with sudo (e.g., sudo mysql) without needing a separate MySQL root password.

Once the installation is successfully completed, the MySQL service should start automatically. You can verify its operational status using systemctl:

sudo systemctl status mysql

The output should clearly indicate that the service is active (running).

Step 3 — Securing MySQL

MySQL provides an indispensable security script named mysql_secure_installation. Running this script immediately after installation is crucial for hardening your MySQL server. It guides you through several important security configurations, including setting up the VALIDATE PASSWORD component (to enforce strong password policies), changing the root password (particularly if you plan to switch from auth_socket to traditional password-based authentication for the root user), removing anonymous user accounts, disallowing remote root logins, and removing the default test database.

Execute the script with sudo privileges:

sudo mysql_secure_installation

The script will interactively prompt you for the following configurations:

  1. VALIDATE PASSWORD COMPONENT: The script will first ask if you wish to enable the VALIDATE PASSWORD component. Enabling this feature is highly recommended for production environments as it enforces password complexity rules, making your database more resilient against brute-force attacks. If you choose to enable it, you will be prompted to select a password validation policy level: 0 (LOW), 1 (MEDIUM), or 2 (STRONG).
  2. Set Root Password: You will then be prompted to set a password for the MySQL root user.
    • Important Note on auth_socket: If you intend to continue using the auth_socket plugin for the root user (which allows passwordless access for the system root user via sudo mysql), setting a password here might seem unnecessary for local sudo access. However, it is still a good security practice to set a strong password. If you later decide to enable traditional password authentication for the MySQL root user (e.g., for remote access or specific application requirements), you would need to alter the user within the MySQL monitor: ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'your_new_strong_password'; FLUSH PRIVILEGES;
  3. Remove anonymous users? (Strongly Recommended: Yes) Anonymous users can pose a security risk and should be removed from production servers.
  4. Disallow root login remotely? (Strongly Recommended: Yes) This is a critical security measure that restricts the MySQL root user from connecting from any host other than localhost, significantly reducing the attack surface.
  5. Remove test database and access to it? (Strongly Recommended: Yes) The test database is provided for development and testing purposes only and should be removed from any production MySQL server.
  6. Reload privilege tables now? (Strongly Recommended: Yes) This action ensures that all the security changes you have made are applied immediately and take effect.

Successfully completing these steps is fundamental for establishing a secure MySQL deployment. For organizations with stringent security requirements or those needing ongoing database administration, consider engaging with professional MySQL support services for expert guidance.

Step 4 — Testing MySQL

After executing mysql_secure_installation, it is essential to test your ability to connect to the MySQL server. If the auth_socket plugin is still active for the root user (which is the default behavior on Ubuntu 24.04), you can connect to MySQL using the following command:

sudo mysql

If you have reconfigured the root user to use password-based authentication (e.g., with mysql_native_password), you would connect using:

mysql -u root -p

You will then be prompted to enter the root password you set.

Once successfully connected, you will be greeted by the MySQL monitor prompt (mysql>). You can execute a simple test query, such as checking the server version or listing the available databases:

SELECT VERSION();
SHOW DATABASES;

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

The MySQL service on Ubuntu 24.04 LTS is managed using the systemctl command-line utility.

  • Start MySQL Service:
    bash
    sudo systemctl start mysql
  • Stop MySQL Service:
    bash
    sudo systemctl stop mysql
  • Restart MySQL Service:
    bash
    sudo systemctl restart mysql
  • Check MySQL Service Status:
    bash
    sudo systemctl status mysql
  • Enable MySQL Service on Boot: (This is typically configured automatically during the installation process)
    bash
    sudo systemctl enable mysql
  • Disable MySQL Service on Boot:
    bash
    sudo systemctl disable mysql

Conclusion

You have now successfully installed and secured MySQL on your Ubuntu 24.04 LTS server. Your database system is primed and ready to support your applications with a reliable and robust data storage solution. It is crucial to remember the importance of implementing regular data backup strategies and consistently keeping your operating system and MySQL packages updated to maintain optimal security and performance.

For further information, advanced configurations, and related guides, you may find the following resources particularly helpful:

Effective and proactive database administration is paramount for ensuring application reliability, data integrity, and robust security. If your organization requires specialized assistance with database management, performance tuning, or is considering comprehensive Managed IT Services, Medha Cloud offers a diverse range of solutions tailored to support your infrastructure needs. Additionally, for Managed Service Providers (MSPs) seeking to expand their service portfolio, Medha Cloud provides white-label managed IT services, enabling you to enhance your offerings under your own distinct brand.

Medha Cloud
Medha Cloud
Medha Cloud, a leading multi-cloud service provider, focuses on cloud computing and digital transformation. Every article published under the Medha Cloud brand is a collaborative effort by our team. We are committed to sharing insights, best practices, how-tos, reviews, and real-world examples to help organizations stay ahead of the curve and thrive in the digital age
Share
Contents

Related Articles

medhacloud logo
USA:
Medha Cloud Solutions LLC
30 N Gould St Ste R, Sheridan, WY 82801,
Phone: +1 646 775 2855

India:
Medha Cloud Solutions Private Limited
#74, 7th Cross, Krishna Garden InCity Layout. Chikka Kammanahalli, Banneraghatta Road, Bangalore 560083
Phone:+91 93536 44646

E-Mail: sales@medhahosting.com
©Medha Cloud 2024. All rights reserved.