Terug naar het artikeloverzicht

How to install MySQL on Ubuntu 20.04

Database code on a computer screen
Photo by Caspar Camille Rubin on Unsplash

MySQL is one of the most widely used database management systems in the world. Since it powers a huge share of the internet, knowing how to install it is useful for all kinds of projects. (Prefer MariaDB? See our guide on how to install MariaDB on Ubuntu 20.04.)

Prerequisites

Before we install MySQL, you'll need a few things:

  • A server running Ubuntu 20.04 or newer. I'm using a dedicated server from Spin Servers.
  • An account with sudo privileges.
  • About 15 minutes and a cup of coffee or hot tea. I prefer vanilla chai with a dash of whole or almond milk, depending on my mood that day.

Step 1: Update your system

First things first: always update your system before installing software or making changes:

sudo apt update
sudo apt upgrade

Step 2: Install MySQL

Install MySQL with the following command:

sudo apt install mysql-server

When asked whether you want to continue, type Y and press Enter. This installs MySQL on your server, and the service starts automatically.

Step 3: Secure MySQL

A fresh MySQL installation includes some default settings that aren't secure, so the next step is to tighten them up. MySQL includes a script for this:

sudo mysql_secure_installation

Password validation

The script first asks whether you want to set up the VALIDATE PASSWORD component, which checks password strength. If you enable it, you'll choose a policy level: 0 (low), 1 (medium) or 2 (strong). Whichever level you choose will apply to every MySQL user password you create afterwards.

The root password

On Ubuntu, the MySQL root user authenticates with the auth_socket plugin by default. That means it doesn't use a password at all: you log in as root by running sudo mysql from an account with sudo privileges. Because of this, the script may skip the root password step.

On some MySQL versions, the script fails here with an error saying that SET PASSWORD has no significance for user 'root'@'localhost'. If that happens, press Ctrl + C to exit, open MySQL with sudo mysql, and give root a password temporarily (replace your_password with a strong password):

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
exit

Then run sudo mysql_secure_installation again. When it's done, you can switch root back to socket authentication so that sudo mysql works again. Log in with mysql -u root -p and run:

ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
exit

Security questions

Next, the script asks a few more questions:

  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

You should normally answer Y to all of them, unless you have a specific reason not to.

Step 4: Verify the MySQL installation

Check that the MySQL server is running properly:

sudo systemctl status mysql

You should see an active (running) status, shown in green.

Step 5: Log in to the MySQL server

To log in to your MySQL server as root, simply run:

sudo mysql

Type exit to leave the MySQL prompt. Congratulations, you now have a fully working MySQL server on your Ubuntu machine!

The server behind this guide

To make these how-to guides possible, I use a dedicated server from Spin Servers. My configuration is:

  • Dual Intel Xeon E5-2630L (12 cores at 2.00 GHz)
  • 128 GB DDR3 RAM
  • 4 × 2 TB HDD in hardware RAID-10
  • 10 TB bandwidth on a 10Gbps port
  • Dallas, TX location

I get this configuration for $89/month with the code DUALE5FOR89, from the Spin Servers promotion on Web Hosting Talk.


If you have questions about any of our services, please contact us via email at [email protected] or by using our live chat at spinservers.com.

Spin Servers is a US-based provider of dedicated servers & affordable private cloud solutions. Our motto is to let you "spin up servers" fast, seamlessly, and affordably.