The Complete Guide to Install MySQL on Ubuntu

Filed Under: Ubuntu
Install Mysql On Ubuntu

Let’s learn how to install MySQL on Ubuntu today. MySQL is a free, open-source database management system. This database management system uses Structured Query Language (SQL) to manage data saved in a relational database. Developed by the Oracle Corporation in 1995, this efficient and scalable program finds applications in both small and big tech companies.

In this tutorial, we will go through the process to install the latest version of MySQL (ver 8.0.20 at the time of writing) on an Ubuntu system.

Pre-requisites 

For following this tutorial to install MySQL your system, you need to fulfil the following requirements.

  • A non-root user with Sudo privileges
  • A system running Ubuntu
  • An active firewall

Steps to Install MySQL on Ubuntu

With the prerequisites out of the way, let’s jump right into installing the database system on Ubuntu.

Updating the Repositories

Before we begin the tutorial to install MySQL on Ubuntu, we might wish to make sure that we have the latest version of the MySQL package. To do this, we need to update our default repositories using the apt package management service. 

To do this, you need to open the terminal on your Ubuntu system and type the following. 

sudo apt update
Apt Update
Apt Update

You should see an output similar to this on your screen. Next, we will use the upgrade command in our terminal.

Apt Upgrade
Apt Upgrade

This is what you will see on the terminal screen. Enter ‘Y’ in the command line to proceed with the operation. These commands have replaced the default Ubuntu repositories with the latest available repositories for your system. Now, this ensures that we will get the latest version of the MySQL package when we commence with the installation on our system.

Installing MySQL Server Package on Ubuntu

Now that we have the latest available packages, we use the apt package manager to install the MySQL server on our system. This can be done using the following command in our terminal window.

sudo apt install mysql-server
Apt Install Mysql
Apt Install Mysql

This will install the MySQL on our Ubuntu system. However, we cannot use it yet as the service is currently insecure. Let us now configure our service and make it ready for work.

Steps to Configure MySQL on Ubuntu

Now that we’re done with the installation, it’s time to set things up for our system. Let’s begin with opening up the firewall ports.

Allowing MySQL through the firewall

Note that this step is not required when MySQL is being used locally and does not require any remote client-server interaction.

Usually, MySQL is used in a setting where multiple systems can send queries for data. Hence we need to allow remote access through the firewall. The default port at which the MySQL service listens to is 3306. So we use the following command to allow it through the firewall. 

sudo ufw allow mysql

UFW has default settings for multiple setups like Apache, LAMP, MySQL, HTTP, and others.

Running the security script

Now, we will launch the secure installation utility by entering the following command in our terminal. 

sudo mysql_secure_installation

This script will make changes to the security options of our freshly installed MySQL service such as remote root logins and sample test users. First, we will be prompted to choose whether we wish to set up the Validate Password Plugin.

Irrespective of the choice we make, we will next get a prompt to choose the password for the root user. 

It will be followed by a series of less important options, so you can choose to modify the settings or enter ‘Y’ to confirm the default settings.

When you are done, the MySQL service will load the modified rules and update itself to match your configurations immediately.

Starting the MySQL service

Now that the installation is complete, we should start the MySQL service on our system. To do so, we use the systemctl command as shown below.

sudo systemctl start mysql

As we will use it in other sessions in the future and starting the process every single time is an unnecessary chore, we will set it to launch at startup. We can do this using the command given below.

sudo systemctl enable mysql

Now every time we log into our system, MySQL process is started automatically.

Setting up the login credentials

Remember you set up a root password earlier? We have yet to configure the password for logging into the MySQL server.

Right now, our MySQL service is authenticating the root user using the auth_socket plugin. To fix this, we need to first launch the MySQL server on our system. we use the following command to do so.

sudo mysql

Next, we need to update the authentication. Now we choose a strong password and use it to update the authentication method with the following SQL command. 

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'

Replace ‘newpassword’ with a password of your choice. Note that this will overwrite the password you chose earlier with the security script.

Now, we will reload all the grant tables to update the changes we just made. This can be done with the following command.

FLUSH PRIVILEGES

Now that we have configured our MySQL server, it’s time to test it out by logging into the server using the root user. Let us exit the service using the ‘exit’ command and log in again using our newly updated password.

Testing MySQL on our system

We have installed the MySQL server on our system and set up some basic configurations. But we just cannot be done without being sure that our freshly installed MySQL server is working properly.

We will test the MySQL server with a basic query. First, we log on to the MySQL server using our root account and its associated password. 

Login to MySQL Server
Login to MySQL Server

You should see a screen similar to this. Notice that this time we used a different method to log on to our root user. This is because without specifying the user and entering the correct password we cannot get administrative privileges of the root user.

Now we will ask our MySQL server to display the list of users, hosts, and plugins on our system. This can be done using the following SQL command.

SELECT User, Host, Plugin FROM mysql.user;
Mysql Display Table
MySQL – Display table

The above output confirms that our MySQL server is working properly.

Wrapping up

MySQL is one of the leading relational database management systems available for users. This tutorial aimed to help you install a MySQL server on Ubuntu and set some basic configurations. If you have any feedback, queries, or suggestions, feel free to reach out to us in the comments below.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages