How to Install MySQL 8 on Ubuntu 20.04

MySQL is one of the most popular open source database management systems. In this tutorial, we will show you how to install MySQL 8 on Ubuntu 20.04.

Posted on

MySQL 8 is the latest major version upgrade of MySQL. It comes with many new features and improvements such as better performance, new SQL functions, new JSON data type support, atomic DDL statements, invisible indexes, etc.

In this tutorial, we will show you how to install MySQL 8 on an Ubuntu 20.04 server from the MySQL APT repository.

Prerequisites

Before you install MySQL, you need to have the following prerequisites:

  • Ubuntu 20.04 server with a non-root user with sudo privileges
  • An internet connection

Step 1 - Update Package Repository

First, update your Ubuntu package repository:

sudo apt update

Step 2 - Import MySQL GPG Key

Next, you need to import the MySQL GPG key into APT keyring to ensure the downloaded packages are authenticated:

wget https://mysql.he.net/Downloads/MySQL-8.0/mysql-apt-config_0.8.13-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb

During the installation, select the MySQL 8.0 version.

Step 3 - Install MySQL Server

Now install MySQL server:

sudo apt update
sudo apt install mysql-server

This will install MySQL 8.0.

Step 4 - Start MySQL Service

Once installed, the MySQL service will start automatically. You can check it with:

sudo systemctl status mysql

Step 5 - Secure MySQL

It’s recommended that you run the mysql_secure_installation script to improve MySQL security:

sudo mysql_secure_installation

This will set the root password, remove anonymous user accounts, disable root logins outside localhost, etc.

Step 6 - Create a Dedicated MySQL User

It’s best practice to create a dedicated MySQL user for your applications instead of using the root account:

sudo mysql
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost' WITH GRANT OPTION;

Replace “myuser” and “mypass” with your desired username and password.

Step 7 - Test MySQL

To test if MySQL is working properly, connect to the mysql server with the new user created:

mysql -u myuser -p

Enter the password when prompted. Then you’ll enter into the MySQL shell.

Run a simple command to test connectivity:

SHOW DATABASES;

This will show the available databases.

Step 8 - Create a Sample Database and Table

Let’s create a sample database and table to validate everything is working:

CREATE DATABASE mytestdb;

USE mytestdb;

CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50),
  email VARCHAR(50)
);

You now have MySQL 8 installed and can start using it!

Conclusion

That’s it! We’ve shown you how to install the latest MySQL 8 on Ubuntu 20.04 from the official MySQL APT repository. You can now start developing applications using MySQL or connect it to your web apps.

If you want to learn more about MySQL, please use our MySQL tutorials and MySQL Reference.