Install MySQL on Ubuntu

Table of Contents

Introduction

Do you need to start saving your data in a MySQL database but don’t know how to install it on your Ubuntu machine?
Don’t worry, It’s very easy to install MySQL on Ubuntu if you know how to do it.
With this post I will show you how to install MySQL on Ubuntu and how to go through all the steps to have a working installation.

Prerequisites

In this post we will see how to install MySQL on an Ubuntu machine.
The tutorial is very simple, but there are some basic prerequisites.
I list them here for clarity:

  • Internet connection
  • Ubuntu OS
  • Access to a terminal
  • User with root privileges

If you have everything, don’t waste any more time and go straight to the next chapter to install MySQL!

How to install MySQL on Ubuntu

To install MySQL on Ubuntu we will use the mysql-server package from the APT package repository.
The installation steps are simple and fast, namely:

  • Update the indexes
  • Install the package
  • Verify that everything is installed correctly

As we said, it’s a good practice to update the indexes with the following command:

sudo apt update

Then we go to install the package with this command:

sudo apt install mysql-server

After running this command you should get a screen similar to this showing what is about to be installed.

How to install MySQL on Ubuntu
How to install MySQL on Ubuntu

Accept by pressing Y and wait until the installation is finished.
If the installation finished without errors, you should be able to see the MySQL version with this command:

mysql --version

If the output looks like this, the installation was successful.

mysql  Ver 8.0.32-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

Like all the others Linux packages, mysql also has a parameter to print general help on screen.
The parameter I’m talking about is the help and you can invoke it like this:

mysql --help

The output of this command is a short description of what mysql is and all available parameters. You will also find the variables with their default values.

MySQL configuration

After finishing the installation you need to do some configuration.
You may have read in some other tutorial to use the mysql_secure_installation command, but as of July 2022 if you try you may get the following error:

Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. 
Please consider using ALTER USER instead if you want to change authentication parameters.

Don’t worry, let’s see how to solve this problem right away.
First let’s open the MySQL prompt:

sudo mysql

Then, run this query in order to change the root user’s authentication method to mysql_native_password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '<my_password>';

My suggestion is to use a strong password with lower and upper case characters, symbols and numbers e.g. ‘Password123!’.
After this, if there are no error, please exit from MySQL prompt typing exit:

mysql> exit

At this point you can run the mysql_secure_installation without problems:

mysql_secure_installation

Please answer to the questions based on your preferences.
Here my suggestions:

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

At this point you should be able to use MySQL from the command line using this line:

sudo mysql -p

The option -p is present because the password is required (use the password set before).

Add MySQL user

In the previous section we saw how you can install MySQL on an Ubuntu machine.
At the end of the described procedure, however, we were able to access the mysql console only with the root user.
This is not good practice as the root user has full access to the system.
For daily use it is better to create and use a user with less privileges.
For this reason, in this chapter we will see how to create a new user.

First we enter the mysql console with the following command:

sudo mysql -p

At this point you will be prompted to enter your password.

To create a new user we must use the CREATE USER command:

CREATE USER '<my_new_user>'@'localhost' IDENTIFIED BY '<my_password>';

Remember to replace the placeholders with the correct username and password.
Once the user has been created, you can connect to the mysql console using the following command:

mysql -u <my_user> -p

Enter the correct password and you’re done!

Created users have no particular privileges.
If you are interested in providing specific privileges to your users, check out this post!

Run MySQL as service

You will probably want to run MYSQL as a service and therefore automatically.
After installing MySQL on Ubuntu, everything should already be set up correctly, but you can check it with this command:

systemctl status mysql.service

If MySQL is not running, start it with this command:

sudo systemctl start mysql

Conclusion

Here we are at the end of this post where we have seen how it is possible to install MySQL on an Ubuntu machine and create a new user.
We’ve also seen how you can test and run MySQL as a service on your machine.

As usual, I hope this post was helpful for you to successfully install MySQL on Ubuntu.
If you have found any errors or you simply have doubts, do not hesitate to contact me or leave a comment below.

If, on the other hand, everything is clear to you, I invite you to continue with the next article!

Leave a Comment

Your email address will not be published. Required fields are marked *