home tools books contact

How to Install MySQL on Linux (Ubuntu 20.04)

MySQL is a popular open-source database used by WordPress and Drupal etc to store data. This tutorial will show you how to install and configure it on Ubuntu Linux.

Advertisement

Step 1: Install MySQL

As of writing, MySQL Version 8.0 is the latest stable release and it’s included in the main repository of Ubuntu 20.04. It can be installed by running the following command:

sudo apt install mysql-server

When the command finishes, the MySQL service will start and you should be able to connect as the root user by running sudo mysql.

Advertisement

Step 2: Make Sure MySQL is running

After installing MySQL it should already be running and configured to start when Linux boots. You can check the status by running:

sudo systemctl status mysql

Output

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-10-15 03:51:46 PDT; 6h ago
   Main PID: 894 (mysqld)
     Status: "Server is operational"
      Tasks: 41 (limit: 4624)
     Memory: 402.7M
     CGroup: /system.slice/mysql.service
             └─894 /usr/sbin/mysqld

Oct 15 03:51:38 ubuntu systemd[1]: Starting MySQL Community Server...
Oct 15 03:51:46 ubuntu systemd[1]: Started MySQL Community Server.

If it’s not started, you can start it with:

sudo systemctl enable mysql
sudo systemctl start mysql

Now that we know MySQL is enabled and running, let’s configure a superuser.

Advertisement

Step 3: Create a Superuser

Unless you plan on creating all users and databases from the mysql command line, you might want to create a superuser that can be used to access MySQL using tools like DBeaver.

Check out this tutorial on how to create a user with full privileges that can be accessed with remote tools.

Optional: Harden MySQL

Another thing you might want to is run the secure installation tool. This will allow you to assign a password to the root user and perform some other steps to improve security.

Run the following command:

sudo mysql_secure_installation

When asked to setup the VALIDATE PASSWORD component type y then 0, 1 or 2 depending on how complex you want to make passwords.

After enabling the validate password component, type in a new root password then re-type the password to confirm.

Say yes to removing anonymous users by pressing y.

Say yes to Disallow root login remotely by pressing y.

Remove the test database by pressing y.

Complete the setup by saying yes to reload privileges.

You’re now ready to start using MySQL.

Advertisement

Conclusion

That’s it!

In this tutorial we installed MySQL 8 on Ubuntu 20.04 and configured a test user and database that you should now be able to access from your web app or a client like SQL Workbench or DBeaver.

Written by: Tony Mackay

MySQL Linux Ubuntu
Advertisement