main logo

How to Install MySQL on Ubuntu 20.04 LTS

How to Install MySQL on Ubuntu 20.04 LTS

Ubuntu 20.04 LTS (Focal Fossa) is a popular choice for servers, offering long-term support and a stable environment for applications like MySQL. This tutorial will guide you through the process of installing MySQL Community Server on an Ubuntu 20.04 LTS system. We will cover updating your package index, installing the MySQL packages, running the security script, and managing the MySQL service. For comprehensive server management and optimization, consider exploring Linux Server Support options.

MySQL is a widely-used open-source relational database management system (RDBMS), crucial for many web applications and data storage needs. Following this guide will result in a functional and secured MySQL installation.

Prerequisites

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

Ensure your system is up-to-date and you have a sudo user configured before proceeding. Standard Ubuntu initial server setup guides can help with this.

Step 1 — Updating Package Index

Before installing any new software, it’s good practice to update your server’s package list to ensure you get the latest versions of packages and their dependencies.

sudo apt update

Step 2 — Installing MySQL

Ubuntu 20.04 includes MySQL in its default APT repositories. You can install the mysql-server package, which will pull in mysql-community-server and related dependencies.

sudo apt install mysql-server

During the installation, you will likely not be prompted to set a root password or make other configuration choices. MySQL 8.0 on Ubuntu initializes with the auth_socket plugin for the root user by default, which allows authentication by validating the operating system user connecting via a Unix socket.

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

sudo systemctl status mysql

The output should show the service as active (running).

Step 3 — Securing MySQL

MySQL comes with a security script that should be run after installation to improve the security of your MySQL server. This script, mysql_secure_installation, will help you configure options like the VALIDATE PASSWORD component, set or change the root password (if not using auth_socket or if you wish to change authentication method), remove anonymous users, disallow remote root login, and remove the test database.

Run the script with sudo:

sudo mysql_secure_installation

This script will guide you through the following prompts:

  1. VALIDATE PASSWORD COMPONENT: The script will first ask if you want to use the VALIDATE PASSWORD component. If you enable it, MySQL will enforce password complexity rules. It’s generally recommended for production. You’ll choose a level of password validation (0 for LOW, 1 for MEDIUM, 2 for STRONG).
  2. Set Root Password: If you enabled the VALIDATE PASSWORD component, or if you choose to set a password for root authentication via traditional password methods, you will be prompted to set and confirm the root password.
    • Note on auth_socket: If you plan to continue using auth_socket for the root user (allowing sudo mysql access without a password), you might not need to set a password here for that specific purpose. However, mysql_secure_installation might still prompt for it. If you wish to enable password authentication for root, you would later need to alter the root user: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password'; FLUSH PRIVILEGES;
  3. Remove anonymous users? (Recommended: Yes) This prevents users from logging in without an account.
  4. Disallow root login remotely? (Recommended: Yes) This restricts the root user from connecting from anywhere other than localhost, enhancing security.
  5. Remove test database and access to it? (Recommended: Yes) The test database is for testing and should be removed on production servers.
  6. Reload privilege tables now? (Recommended: Yes) This ensures that all changes made so far take effect immediately.

Completing these steps is crucial for a secure MySQL deployment. For advanced security hardening or ongoing database management, consider seeking professional MySQL support.

Step 4 — Testing MySQL

After running the security script, you can test your MySQL installation. If you are using auth_socket for the root user (the default on Ubuntu 20.04), you can access MySQL as root using sudo:

sudo mysql

If you configured root to use password authentication (e.g., mysql_native_password), you would log in with:

mysql -u root -p

And then enter the root password you set.

Once logged in, you will be at the MySQL monitor prompt (mysql>). You can run a simple query, like checking the server version:

SELECT VERSION();

Or show databases:

SHOW DATABASES;

To exit the MySQL monitor, type:

EXIT;

Step 5 — Managing the MySQL Service

The MySQL service is managed using systemctl on Ubuntu 20.04.

  • 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: (Usually enabled by default after 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 20.04 LTS server. Your database is now ready to be used by your applications. Remember to perform regular backups and keep your system updated.

For further reading and related guides, you might find these useful:

Effective database administration is key for performance and security. If your organization requires expert assistance with database management or is looking for comprehensive Managed IT Services, Medha Cloud offers a range of solutions. For Managed Service Providers (MSPs), Medha Cloud also provides white-label managed IT services, allowing you to expand 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.