How to Install MySQL Server on Ubuntu 20.04

How To Install & Secure MySQL Server on Ubuntu 20.04

Last updated on

In this guide we will install, configure and secure MySQL on Ubuntu Server 20.04 (Focal Fossa).

Prerequisites

You should use a non-root user account as explained in the Ubuntu 20.04 Initial Server Setup.

1. Install MySQL Server

Let’s begin by updating the repository and installing the MySQL package for Ubuntu 20.04 using apt.

sudo apt update && sudo apt install mysql-server

Press y and ENTER when prompted to install the MySQL package.

Once the package installer has finished, we can check to see if the MySQL service is running.

sudo service mysql status

If running, you will see a green Active status like below.

 mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-04-02 02:40:59 CEST; 2min 47s ago
   Main PID: 18476 (mysqld)
     Status: "Server is operational"
      Tasks: 27 (limit: 4915)
     CGroup: /system.slice/mysql.service
             └─18476 /usr/sbin/mysqld

Apr 02 02:40:59 ubuntu2004 systemd[1]: Starting MySQL Community Server...
Apr 02 02:40:59 ubuntu2004 systemd[1]: Started MySQL Community Server.

You may need to press q to exit the service status.

2. Configure MySQL Security

You should now run mysql_secure_installation to configure security for your MySQL server.

sudo mysql_secure_installation

2.1. Validate Password Component (Optional)

You will be asked if you want to set up the Validate Password Plugin. It’s not really necessary unless you want to enforce strict password policies for some reason.

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

Press ENTER if you don’t want to set up the validate password plugin.

2.2. Create Root Password

If you didn’t create a root password in Step 1, you must now create one here.

Please set the password for root here.

New password:

Re-enter new password:

Generate a strong password and enter it. Note that when you enter passwords in Linux, nothing will show as you are typing (no stars or dots).

2.3. Remove Anonymous Users

You will next be asked to remove anonymous users.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

Press y and ENTER to remove anonymous users.

2.4. Disable Remote Root Login

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

Press y and ENTER to disallow root login remotely. This will prevent bots and hackers from trying to guess the root password.

2.5. Remove Test Database

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

Press y and ENTER to remove the test database.

2.6. Reload Privilege Tables

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

Press y and ENTER to reload the privilege tables.

You’re all done! 😀

2.7. Test MySQL Service

You can now log into the MySQL server. Because you are running this command as sudo, this will automatically log into MySQL using the MySQL root account. If you are prompted for a password, enter your Linux root account password, not the MySQL one.

sudo mysql

Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.19-0ubuntu5 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

To exit MySQL, type exit and press ENTER.

exit

You have now successfully installed and configured MySQL!

What Next?

Now that your MySQL server is up and running, you might want to install phpMyAdmin for Ubuntu 20.04, which allows you to easily manage your MySQL users and databases through a browser interface.

Before installing phpMyAdmin, you must have Apache/Nginx and PHP installed first. Please refer to the following guides:

Apache

Nginx

1 Star2 Stars3 Stars4 Stars5 Stars 5.00 (4 votes)

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

p.s. I increased my AdSense revenue by 68% using AI 🤖. Read my Ezoic review to find out how.

Leave a Reply

avatar
  Subscribe  
Notify of