main logo

How to Install MySQL on Ubuntu 22.04 LTS

How to Install MySQL on Ubuntu 22.04 LTS

Ubuntu 22.04 LTS (Jammy Jellyfish) continues the tradition of providing a stable and reliable platform for server deployments, making it an excellent choice for hosting MySQL databases. This tutorial will walk you through installing MySQL Community Server on an Ubuntu 22.04 LTS system. We will cover updating your package sources, installing the MySQL server, running the essential security script, and managing the MySQL service. For comprehensive server administration and optimization, including Linux Server Support, consider exploring professional services.

MySQL is a leading open-source relational database management system (RDBMS), integral to countless web applications and data-driven services. By following this guide, you will establish a secure and functional MySQL installation on your server.

Prerequisites

  • An Ubuntu 22.04 LTS server.
  • A non-root user with sudo privileges.
  • Access to a terminal or command-line interface.

Before starting, ensure your system is fully updated and you have a sudo-enabled user. Standard Ubuntu initial server setup guides can assist with these preliminary steps.

Step 1 — Updating Package Index

It is always a best practice to update your server’s package list before installing new software. This ensures that you are installing the latest available versions and that all dependencies are correctly resolved.

sudo apt update

Step 2 — Installing MySQL

MySQL packages are available in Ubuntu 22.04’s default APT repositories. You can install the mysql-server package, which typically provides mysql-community-server and its necessary dependencies.

sudo apt install mysql-server

During the installation on Ubuntu 22.04, MySQL 8.0 (or a newer stable version available in the repositories) is typically configured to use the auth_socket plugin for the root user by default. This plugin allows authentication by verifying the operating system user connecting via a Unix socket, meaning you can connect as root using sudo mysql without a separate MySQL root password initially.

Once the installation completes, the MySQL service should start automatically. You can verify its operational status:

sudo systemctl status mysql

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

Step 3 — Securing MySQL

MySQL provides a security script, mysql_secure_installation, which should be executed post-installation to enhance the security of your MySQL server. This script helps you configure options such as the VALIDATE PASSWORD component (to enforce password strength), set or change the root password (especially if you intend to switch from auth_socket to password-based authentication for root), remove anonymous users, restrict remote root login, and remove the test database.

Run the script using sudo:

sudo mysql_secure_installation

The script will guide you through several prompts:

  1. VALIDATE PASSWORD COMPONENT: The script will first ask if you want to enable the VALIDATE PASSWORD component. Enabling this is recommended for production environments as it enforces password complexity. If you choose to enable it, you will select a password validation level (0 for LOW, 1 for MEDIUM, 2 for STRONG).
  2. Set Root Password: You will be prompted to set a password for the MySQL root user.
    • Note on auth_socket: If you plan to continue using auth_socket for root (allowing sudo mysql access without a password), this step might seem redundant for local sudo access. However, setting a strong password here is still good practice, especially if you later decide to enable password authentication for root. To switch root to use password authentication, you would execute: ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'your_new_password'; FLUSH PRIVILEGES; inside the MySQL monitor.
  3. Remove anonymous users? (Recommended: Yes) This prevents unauthenticated access.
  4. Disallow root login remotely? (Recommended: Yes) This is a critical security measure, restricting root access to the local machine only.
  5. Remove test database and access to it? (Recommended: Yes) The test database is for development and testing, not production.
  6. Reload privilege tables now? (Recommended: Yes) This applies all the changes made immediately.

Completing these configurations is vital for a secure MySQL deployment. For advanced security measures or ongoing database administration, consider engaging with professional MySQL support services.

Step 4 — Testing MySQL

After running mysql_secure_installation, test your MySQL access. If auth_socket is still active for root (the default), connect using:

sudo mysql

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

mysql -u root -p

Then, enter the root password you set.

Once connected, you will be at the MySQL monitor prompt (mysql>). You can run a test query, such as checking the server version or listing databases:

SELECT VERSION();
SHOW DATABASES;

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

The MySQL service on Ubuntu 22.04 is managed with systemctl.

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

Conclusion

You have successfully installed and secured MySQL on your Ubuntu 22.04 LTS server. Your database system is now ready for your applications. Remember the importance of regular backups and keeping your system and MySQL packages updated.

For further information and related guides, explore these resources:

Robust database administration is essential for application reliability and data security. If your organization needs expert assistance with database management or is considering comprehensive Managed IT Services, Medha Cloud offers a variety of solutions. Additionally, for Managed Service Providers (MSPs), Medha Cloud provides white-label managed IT services, enabling you to enhance your service offerings under your own 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.