Introduction

MySQL is a prominent open-source database management system used to 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.
In Debian 10, MariaDB, a community fork of the MySQL project, is packaged as the default MySQL variant. While MariaDB works well in most cases, if you need features found only in Oracle’s MySQL, you can install and use packages from a repository maintained by the MySQL developers.
In this tutorial, you will install the latest version of MySQL by adding this repository, install the MySQL software itself, secure the install, and test that MySQL is running and responding to commands.

Prerequisites

Before starting this tutorial, you will need one Debian 10 server that has a non-root user with sudo privileges and a firewall configured. You can do this by following our initial server setup guide for Debian 10.

Step 1 — Adding the MySQL Software Repository

The MySQL developers provide a .deb package that handles configuring and installing the official MySQL software repositories. Once the repositories are set up, you’ll be able to use Debian’s standard apt command to install the software.
Before doing this, you need to install the prerequisite GnuPG package, an open-source implementation of the OpenPGP standard.
Begin by updating the local package index to reflect the latest upstream changes:

sudo apt update

Then, install the gnupg package:

sudo apt install gnupg

After confirming the installation, APT will install gnupg and its dependencies.
Next, you’ll download the MySQL .deb package with wget and then install it using the dpkg command.
Load the MySQL download page in your web browser. Find the Download button in the lower-right corner and click through to the next page. This page will prompt you to log in or sign up for an Oracle web account. You can skip that and find 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).
Now you’re going to download the file. On your server, move to a directory you can write to, such as the temporary /tmp directory used in this example:

cd /tmp

Next, download the file using wget, remembering to paste the address you copied in place of the highlighted portion in the following command:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb

The file should now be downloaded in your current directory. List the files to confirm:

ls

The output will return a list of various files, including the one you just downloaded, which is highlighted in the following example:

. . .
mysql-apt-config_0.8.22-1_all.deb
. . .

Now you’re ready for installation. Run the dpkg command which is used to install, remove, and inspect .deb software packages. The -i flag indicates that you’d like to install from the specified file:

sudo dpkg -i mysql-apt-config*

During the installation, you’ll be presented with a configuration screen where you can specify which version of MySQL you’d prefer, along with an option to install repositories for other MySQL-related tools. The defaults will add the repository information for the latest stable version of MySQL and nothing else. This is what we will choose for our purposes, so use the down arrow to navigate to the Ok menu option and hit ENTER.
The package will now finish adding the repository. Refresh your apt package cache to make the new software packages available:

sudo apt update

Now that you’ve added the MySQL repositories, you’re ready to install the actual MySQL server software. If you ever need to update the configuration of these repositories, run sudo dpkg-reconfigure mysql-apt-config, select new options, and then sudo apt-get update to refresh your package cache.

Step 2 — Installing MySQL

Having added the repository and with your package cache freshly updated, now you can use apt to install the latest MySQL server package:

sudo apt install mysql-server

apt will scan all available mysql-server packages and determine that the MySQL provided package is the newest and best candidate. It will then calculate package dependencies and ask you to approve the installation. Write y then ENTER. The software will install.
You will be asked to set a root password during the configuration phase of the installation. Choose and confirm a secure password to continue. Next, a prompt will appear asking you to select a default authentication plugin. Read the display to understand the choices. If you are not sure, choosing Use Strong Password Encryption is safer.
MySQL should be installed and running now. Check by using systemctl:

sudo systemctl status mysql

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Thu 2022-02-24 18:59:22 UTC; 23min ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 3722 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 4915)
   Memory: 371.7M
   CGroup: /system.slice/mysql.service
           └─3722 /usr/sbin/mysqld

Feb 24 18:59:21 sql-debian systemd[1]: Starting MySQL Community Server...
Feb 24 18:59:22 sql-debian systemd[1]: Started MySQL Community Server.

The Active: active (running) line means MySQL is installed and running. In the next step, you’ll make the installation a little more secure.

Step 3 — Securing MySQL

MySQL comes with a command to perform a few security-related updates on your new install. You can run it now:

mysql_secure_installation

This will ask you for the MySQL root password that you set during installation. Write it in and press ENTER. Now you’ll answer a series of yes or no prompts. Let’s review them:
First, you’re asked about 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. Write y and press ENTER to enable it, or press ENTER to skip it. If enabled, you will also be prompted to choose a level from 0–2 for how strict the password validation will be. Choose a number and press ENTER to continue.
Next you’ll be asked if you want to change the root password. Since you recently created the password when you installed MySQL, you can safely skip this. Press ENTER to continue without updating the password.
The rest of the prompts can be answered yes. You will be asked about removing anonymous MySQL users, disallowing remote root logins, removing the test database, and reloading privilege tables to ensure the previous changes take effect properly. These are all good ideas. Write y and press ENTER for each.
The script will exit after all the prompts are answered. Now your MySQL installation is reasonably secured. Next you’ll test it again 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’ll use it to connect to the server and output some version and status information. The -u root portion 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 you want to run:

mysqladmin -u root -p version

The output will result in what version of the MySQL server is running, its uptime, and some other status information as in the following:

mysqladmin  Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2022, 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.28
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 25 min 31 sec

Threads: 2  Questions: 20  Slow queries: 0  Opens: 143  Flush tables: 3  Open tables: 62  Queries per second avg: 0.013

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

Conclusion

You’ve now installed the latest stable version of MySQL, which should work for many popular applications.
For more information about the basics of MySQL, we encourage you to check out the following tutorials:

How To Create a New User and Grant Permissions in MySQL
How To Create and Manage Databases in MySQL and MariaDB on a Cloud Server
How To Migrate a MySQL Database Between Two Servers