How to Install MySQL 8.0 on Ubuntu 22.04 or 20.04

If you’re looking to install MySQL 8.0 on Ubuntu 22.04 Jammy Jellyfish LTS or its older stable release of Ubuntu 20.04 Focal Fossa LTS, you’ve come to the right place. MySQL 8.0 Community Edition is a leading open-source relational database management system (RDBMS) that offers a range of features designed for optimal data management.

Key Features and Improvements in MySQL 8.0

  • Enhanced Performance: MySQL 8.0 introduces performance optimizations like invisible indexes and improved hash joins, enabling faster data processing and retrieval.
  • Improved Security: With features like advanced password encryption and SQL roles, MySQL 8.0 strongly emphasizes data security.
  • GIS Support: Full support for Geographic Information Systems (GIS), including spatial indexes on InnoDB, opens up new possibilities for geographic data management.
  • Data Dictionary: A transactional data dictionary provides a unified system for handling metadata, making data management more coherent.
  • Enhanced JSON Support: Improved JSON functions simplify the management of complex JSON data structures.

MySQL 8.0 Community Edition is a robust, reliable, and secure RDBMS solution, offering significant improvements over its predecessors. Whether you’re a database administrator, developer, or someone interested in database management, MySQL 8.0 offers features catering to various data management needs.

The following guide will provide step-by-step instructions on how to install MySQL 8.0 on Ubuntu 22.04 Jammy Jellyfish LTS or Ubuntu 20.04 Focal Fossa LTS. We’ll cover two installation methods using the APT package manager: one with Ubuntu’s default repository and another using the official APT repository from MySQL. Stay tuned for the detailed installation steps.

Section 1: Install MySQL 8.0 on Ubuntu 22.04 or 20.04

Refreshing System Packages Before MySQL 8.0 Installation

Before diving into the installation process, updating your system is always a good practice. This process ensures all existing packages are up to date, minimizing the chance of conflicts during the MySQL 8.0 installation.

Run the following commands to update and upgrade your system:

sudo apt update
sudo apt upgrade

Choosing the MySQL 8.0 APT Installation Method on Ubuntu 22.04 or 20.04

There are two ways to install MySQL 8.0 on your Ubuntu system. You can either leverage the Ubuntu’s default repository or import the MySQL’s official APT repository.

Method 1: Installing MySQL 8.0 with Ubuntu’s Repository

This is the recommended option if you are looking for a method that prioritizes system stability. Installing MySQL from Ubuntu’s default repository ensures a stable, thoroughly tested version of MySQL 8.0.

To install MySQL with this method, execute the following command:

sudo apt install mysql-server

Method 2: Installing MySQL 8.0 with MySQL APT Repository on Ubuntu 22.04 or 20.04

This method suits those who want the most recent stable version of MySQL 8.0 directly from MySQL’s repository. This approach can offer quick improvements, especially beneficial depending on your system requirements.

Installing Required Packages for MySQL on Ubuntu:

Before proceeding with this method, you must install a few necessary packages. These packages are typically present in most Ubuntu systems. However, to be on the safe side, run the following command to ensure they’re installed:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl lsb-release -y

Importing the MySQL 8.0 Community Server Repository on Ubuntu 22.04 or 20.04:

To install MySQL from its official repository, import its GPG key and repository into your system. Here’s how you do it:

  • Import the GPG key:
curl -fsSL | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mysql.gpg > /dev/null
  • Then, import the MySQL 8.0 repository:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] $(lsb_release -cs) mysql-8.0" | sudo tee -a /etc/apt/sources.list.d/mysql.list

If you’re a developer or have a specific requirement, you can optionally import the MySQL source repository:

echo "deb-src [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] $(lsb_release -cs) mysql-8.0" | sudo tee -a /etc/apt/sources.list.d/mysql.list 

Developers can also import the MySQL tools repository as follows:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] $(lsb_release -cs) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list
echo "deb-src [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/mysql.gpg] $(lsb_release -cs) mysql-tools" | sudo tee -a /etc/apt/sources.list.d/mysql.list

After adding the repositories, you can verify the changes by executing this command:

grep mysql /etc/apt/sources.list.d/mysql.list

Example output:

joshua@ubuntu-linux:~$ grep mysql /etc/apt/sources.list.d/mysql.list 
deb [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] jammy mysql-8.0
deb-src [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] jammy mysql-8.0
deb [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] jammy mysql-tools
deb-src [arch=amd64 signed-by=/usr/share/keyrings/mysql.gpg] jammy mysql-tools

Updating APT Cache after Importing MySQL 8.0:

Now that you’ve successfully imported the MySQL APT repository, it’s time to refresh the Advanced Package Tool (APT) cache to recognize the newly added repository.

Run the following command to update the APT cache:

sudo apt update

Installing MySQL 8.0 Community Edition Server on Ubuntu 22.04 or 20.04:

With all the prerequisites, we are now ready to install MySQL 8.0 Community Edition Server. Run the following command to start the installation:

sudo apt install mysql-community-server

During installation, you’ll be prompted to set a password for the MySQL root user. Please ensure this password is secure and note it for future reference. After confirming the password, you’ll be asked to select the default authentication plugin. If in doubt, opt for the “Use Strong Password Encryption” recommended by MySQL.

Screenshot showing how to configure the root password for MySQL 8.0 Community Release on Ubuntu 22.04 or 20.04.Pin
Step-by-step screenshot for configuring the root password in MySQL 8.0 on Ubuntu.
Screenshot illustrating the selection of authentication options for MySQL 8.0 Community on Ubuntu 22.04 or 20.04.Pin
Screenshot showing the authentication options available during MySQL 8.0 installation on Ubuntu.

To confirm the installation, check the installed MySQL version with the following command:

apt policy mysql-community-server
Screenshot of the 'apt policy' command output for checking MySQL 8.0 version on Ubuntu 22.04 or 20.04.Pin
The first screenshot demonstrates how to use the ‘apt policy’ command to check the MySQL 8.0 version on Ubuntu.

Section 2: Service Commands for MySQL 8.0 Community Server

With the MySQL 8.0 Community Edition Server installed, the system automatically activates the MySQL service and sets it to auto-start upon system boot. Understanding some basic service management commands is essential for the efficient operation of your database server.

After installation, one of the first tasks is to confirm that your MySQL service is running as expected. Using the systemctl command, specifically tailored for interacting with systemd services, you can view the current status of your MySQL service. Issue the following command:

systemctl status mysql
Second screenshot of the 'apt policy' command output for verifying MySQL 8.0 version on Ubuntu 22.04 or 20.04.Pin
The second screenshot shows additional details from the ‘apt policy’ command for MySQL 8.0 on Ubuntu.

On successful operation, the status message should indicate that the service is active and running without issues.

Understanding how to manage the MySQL service is critical to maintaining your database server. Various systemctl commands enable you to interact with the MySQL service directly. Here’s an overview of some essential commands:

Stopping the MySQL Service

To halt the MySQL service at any given time, use the following command:

sudo systemctl stop mysqld

Starting the MySQL Service

If your MySQL service is not running, or after you’ve stopped it, restart it with the following command:

sudo systemctl start mysqld

Disabling the MySQL Service at System Startup

There might be situations where you’d prefer the MySQL service not to start automatically when your system boots. To disable auto-start, use the following command:

sudo systemctl disable mysqld

Enabling the MySQL Service at System Startup

To revert the above action and set the MySQL service to start automatically upon system boot, use the following command:

sudo systemctl enable mysqld

Restarting the MySQL Service

If you need to restart the MySQL service—for instance, after making configuration changes—use the following command:

sudo systemctl restart mysqld

Section 3: Secure MySQL 8.0 on Ubuntu 22.04 or 20.04

Upon installing MySQL, specific default settings may not meet all security standards, potentially opening your database to intrusions or exploitations. A crucial step after installation is to bolster the security of your MySQL instance. MySQL provides a security script mysql_secure_installation that helps to tighten the security around your database.

Running the MySQL Security Script on Ubuntu

The mysql_secure_installation script is a part of the MySQL package that enhances security. Launch the script by issuing the following command:

sudo mysql_secure_installation

Interacting with the Security Script on Ubuntu

After running the command, the script prompts you to input your root password, initially set during the MySQL installation.

You will encounter a prompt regarding the VALIDATE PASSWORD COMPONENT, which helps enforce password strength policies. Generally, the default settings suffice.

The script then takes you through a series of steps:

1. Password Settings for Root Accounts

Here, you have the chance to reset the root password. However, if you already set it during the initial installation, you may choose to skip this step. The script requires you to enter the new password twice for validation.

New password: <---- SET NEW PASSWORD
Re-enter new password: <---- RE-ENTER NEW PASSWORD

2. Removal of Anonymous Users

MySQL, by default, includes an anonymous user account. This account allows anyone to log into MySQL without having an authenticated user account, which can be a significant security risk. The script offers you the chance to remove these anonymous user accounts:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY.

3. Disallowing Root Login Remotely

In a standard security protocol, the root user should only be allowed to connect from ‘localhost’. This protocol ensures that remote attempts to guess the root password are mitigated. The script asks you to confirm the deactivation of remote root logins:

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY.

4. Removal of Test Database

By default, MySQL installation includes a ‘test’ database. This database is accessible to anyone and is only meant for testing purposes. Before moving your setup to a production environment, it’s crucial to remove the test database and access it:

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY.

5. Reloading Privilege Tables

Reloading the privilege tables allows all changes you’ve made so far to take effect immediately:

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y <---- Type Y then press the ENTER KEY.

By following these steps and confirming the security changes, you enhance the security of your MySQL server installation, mitigating the risk of potential exploits.

Section 4: Managing MySQL 8.0 on Ubuntu 22.04 or 20.04

Managing your MySQL server involves several routine tasks such as updates, upgrades, and, when necessary, uninstallation. Ubuntu Linux provides efficient command-line utilities for these tasks, ensuring your MySQL server stays up-to-date and secure.

Updating MySQL 8.0 Community Server

Regular updates are essential to maintain the smooth running of your MySQL server. These updates often bring bug fixes, security patches, and new features, helping enhance your server’s overall security and functionality.

Since you’ve added the official MySQL APT repository to your system, updating MySQL is easy. The following command is a combination of two distinct commands, sudo apt update and sudo apt upgrade.

sudo apt update && sudo apt upgrade

The sudo apt update command retrieves information about what packages can be upgraded, including MySQL, if an update is available. On the other hand, sudo apt upgrade upgrades the actual packages.

Uninstalling MySQL 8.0 from Ubuntu 22.04 or 20.04

There might be instances when you no longer require the MySQL server on your Ubuntu system. The uninstallation process involves stopping MySQL service and removing the software and associated dependencies.

Stop the MySQL Service on Ubuntu

Before uninstallation, ensure that the MySQL service isn’t running. Use the following command to stop the service immediately:

sudo systemctl stop mysql --now

Remove MySQL and Its Dependencies from Ubuntu

Now, you can remove the MySQL server and any associated packages no longer needed. The sudo apt autoremove command is quite efficient at this:

sudo apt autoremove mysql-community-server

Alternatively, use the following command if you used Ubuntu’s version:

sudo apt autoremove mysql-server

This command removes the MySQL server and cleans up unused packages or dependencies, freeing up system resources.

Remove MySQL 8.0 Repository from Ubuntu 22.04 or 20.04

If you no longer intend to use MySQL and had previously imported the MySQL 8.0 Community Repository, removing the repository from your system is a good practice. Use the following command to delete the MySQL list file from the sources.list.d directory:

sudo rm /etc/apt/sources.list.d/mysql.list

This step ensures that your system won’t attempt to check the MySQL repository for updates during a system-wide upgrade, thus keeping your APT sources streamlined.


The journey of installing, configuring, and managing MySQL 8.0 on Ubuntu Linux is an essential skill for any database administrator, developer, or tech enthusiast. We’ve covered a broad range of topics, from setting up and starting the service to ensuring the security of your MySQL installation and updating and uninstalling as needed. Each step is crucial in maintaining an efficient, secure, and up-to-date MySQL server on your Ubuntu system.

Share to...