Tutorial

How To Install the Latest MySQL on Ubuntu 20.04

How To Install the Latest MySQL on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

MySQL is a prominent open-source relational database management system (RDBMS) used to create, store and retrieve data for a wide variety of popular applications. MySQL is the M in the LAMP stack, a commonly used set of open-source software that also includes Linux, the Apache web server, and the PHP programming language.

When MySQL releases new features, you may need to install a more up-to-date version than what your Linux distributor provides. For this reason, MySQL developers share their own software repository to install the latest version.

In this tutorial, you will install the latest version of MySQL. Before doing so, you will need to add the MySQL software repository. Then, you will install the MySQL software itself, secure the install, and verify that MySQL is running and responding to commands.

Prerequisites

Before starting this tutorial, you will need:

Step 1 — Adding the MySQL Software Repository

The MySQL developers provide a .deb package that configures and installs the official MySQL software repositories. Once the repositories are set up, you will run Ubuntu’s standard apt command to install the software. To do this, you will download the .deb file with curl and then install it with the dpkg command.

First, load the MySQL download page in your web browser. Find the Download button in the right corner and press it to get to the next page. This page will prompt you to log in or sign up for an Oracle web account. Ignore this and locate the link that says No thanks, just start my download. Right-click the link and select Copy Link Address (this option may be worded differently, depending on your browser).

Next, you will download the file. On your server, move to a directory you can write to:

  1. cd /tmp

Download the file using curl and replace the highlighted URL with the address you copied from the MySQL Download page:

  1. curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.18-1_all.deb

This command uses two flags with curl. -O instructs curl to output to a file instead of standard output. The L flag makes curl follow HTTP redirects, because the address you copied actually redirects to another location before the file downloads.

The file is now downloaded in your current directory. List the files to make sure:

  1. ls

You will see the filename listed:

Output
mysql-apt-config_0.8.18-1_all.deb . . .

Now you can use dpkg to begin the install:

  1. sudo dpkg -i mysql-apt-config*

dpkg is used to install, remove, and inspect .deb software packages. The -i flag requests to install from the specified file.

During installation, you’ll be prompted with a configuration screen where you can choose which version of MySQL you prefer, and an option to install repositories for other MySQL-related tools.

Alt text for screen readers

Since you only want to install the latest version of MySQL, you can choose Ok from the menu options and press ENTER. The repository information you need is already included with the defaults.

The package will finish adding the repository. Then you can refresh your apt package cache to make the new software packages available:

  1. sudo apt update

Tidy everything up by deleting the file download with the rm command:

  1. rm mysql-apt-config*

Now that the MySQL repositories are added, you’re ready to install the actual MySQL server software. You can update the configuration of these repositories in the future by running sudo dpkg-reconfigure mysql-apt-config, selecting new options, and then sudo apt update to refresh your package cache.

Step 2 — Installing MySQL

With your new repository added and package cache freshly updated, you will use apt to install the latest MySQL server package:

  1. sudo apt install mysql-server

apt identifies all available mysql-server packages and confirms that the MySQL package is the newest version. Then, it will calculate package dependencies and prompt you to approve the installation. Type y then ENTER to proceed with the installation.

You will be asked to create a root password during the configuration phase of the installation. Be sure to choose a secure password. After you enter the password twice and hit ENTER, you will be asked to configure an authentication plugin. The default of Use Strong Password Encryption is recommended, so press ENTER to choose it. The installation process will continue until finished.

MySQL is now installed and running. You can check with systemctl:

  1. systemctl status mysql
Output
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: > Active: active (running) since Thu 2021-08-05 22:11:42 UTC; 1min 23s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 5268 ExecStartPre=/usr/share/mysql-8.0/mysql-systemd-start pre (cod> Main PID: 5322 (mysqld) Status: "Server is operational" Tasks: 37 (limit: 9513) Memory: 353.3M CGroup: /system.slice/mysql.service └─5322 /usr/sbin/mysqld

MySQL is installed and running if you see Active: active (running). Next, you will secure your installation.

Step 3 — Securing MySQL

MySQL comes with a command we can use to perform a few security-related updates on our new install. Run the following command:

  1. mysql_secure_installation

This command will bring up the configuration screen from earlier and ask for the MySQL root password credentials you created upon installation. Type it in and press ENTER. Then, you will answer a series of yes or no prompts. This is what they’ll ask:

  • First, you will be asked whether you want to enable the validate password plugin, a plugin that can automatically enforce certain password strength rules for your MySQL users. Enabling this is a decision you’ll need to make based on your individual security needs. Type y and ENTER to enable it, or press ENTER to skip it. If enabled, you will also be prompted to choose a level from 0–2 on how strict the password validation will be. Choose a number and hit ENTER to continue.

  • Second, you’ll be asked if you want to change the root password. Since you created the password during installation, you can safely skip this. Hit ENTER to continue without updating the password.

  • Lastly, the other prompts can be answered yes. You will be asked about removing the anonymous MySQL user, disallowing remote root login, removing the test database, and reloading privilege tables to ensure the previous changes take effect properly. Type y and hit ENTER for each.

The configuration screen will disappear after all the prompts are answered. This indicates your MySQL installation is reasonably secured. You can confirm by running a client that connects to the server and returns some information.

Step 4 – Testing MySQL

mysqladmin is a command line administrative client for MySQL. You will use it connect to the server and output some version and status information:

  1. mysqladmin -u root -p version

The -u root portion of this command tells mysqladmin to log in as the MySQL root user, -p instructs the client to ask for a password, and version is the actual command we want to run.

The output will let us know what version of the MySQL server is running, its uptime, and some other status information:

Output
mysqladmin Ver 8.0.26 for Linux on x86_64 (MySQL Community Server - GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Server version 8.0.26 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 31 min 1 sec Threads: 2 Questions: 12 Slow queries: 0 Opens: 130 Flush tables: 3 Open tables: 49 Queries per second avg: 0.006

This output indicates that you’ve successfully installed and secured the latest MySQL server.

Conclusion

You’ve now completed a basic install of the latest version of MySQL, which will work for many popular applications. If you have more advanced needs you can apply some other configuration tasks:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

did not work for me, I got this error:

dpkg: error processing package mysql-server-8.0 (–configure): installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1.

var log file: 2022-03-16T00:18:29.001131Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.28-0ubuntu0.20.04.3) initializing of server in progress as process 130666 2022-03-16T00:18:29.023259Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2022-03-16T00:18:29.693693Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.

I know it’s not recommended, but if I don’t set up any password… I just run sudo mysql or sudo mysqladmin right?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.