How to install MySQL on Ubuntu 18.04

Youtube, Facebook, twitter and most web hosting providers are using MySQL. As an open source freely available (RDBMS) Relational Database Management System, MySQL is an essential component of almost every open PHP application. Joomla, Magento, WordPress and Drupal are are all relying on the reliability and robustness of MySQL to handle and process large numbers of complex SQL statements. Your website is most likely managed in the background by MySQL.
In this article, we will describe the installation steps of MySQL in Ubuntu 18.04 using both the apt command and the Ubuntu official repos.

MySQL installation and security settings configuration

In the following sections, you will learn how to install MySQL using the Ubuntu terminal and how to configure its related security settings.

Update repository index

To install the most recent version of a software from the available repositories, you you would need to update your local repository index. To do so , run the following command:

sudo apt-get update

Install MySQL Server using apt

To install MySQL from the APT repositories, run the following command:

sudo apt-get install mysql-server

Once the installation is finished, MySQL service will launch automatically. In order to check that MySQL server is running, type in the command:

sudo systemctl status mysql

To verify the version of MySQL installation, execute the following command :

mysql –version

Read: How to use the APT command on Ubuntu/Debian Linux systems

Securing MySQL server deployment

Some default settings would have to be changed each time a fresh copy of MySQL is installed so that the security of your MySQL installation is enhanced. This includes the suppression of test users, test databases and permissions.

To run the security script, run the following command :

sudo mysql_secure_installation

After you run this command, you will be asked first to setup the Validate Password plugin. This is to set a strong password for root user. Once you hit Y to run the Validate Password Plugin, you will obtain the following prompt:

Go ahead and provide your selection for password strength and press Enter. You will then be asked for the new root password.

The system will then show the password strength that you entered. Hit Y for the next prompt and then press Enter.

You will now be presented with a series of questions, one at a time.

if you want to remove anonymous test users, press Y.

To ensure that someone cannot guess the root password from a remote system, you would have to press Y to the next question.

In the next question go ahead and remove the default MySQL database with the name “test” from your current system and remove the access to it.

Remove test database

For the changes made so far to take effect, the system has to reload the privilege tables. To commit all your security changes, hit Y.

Installing MySQL using the official repository

1 – Visit the download page of the official MySQL website.

2 – Right click on the last statement shown above, “No thanks, just start my download’ and choose Copy link location.

3 – Once you have the download link to the deb package, run the command below :

curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

The link show above might be different for you depending on the current version of MySQL. You simply have to replace the name of the download in the link above with the one you obtained in the previous step.

Read :How to install and uninstall applications on Ubuntu : A Beginner’s guide

3- Let’s now use dpkg to start the installation of MySQL:

sudo dpkg -i mysql-apt-config*

4 – Now to update the repositories, run the command :

sudo apt update

5 – In order to install MySQL, run the command that we used previously:

sudo apt install mysql-server -y

This will launch a prompt in your terminal related to package configuration.

6 – Use the down key to choose the Ok option.

Hit Enter key. You will be asked to provide a password. This is to set the root password for MySQL which is not to be confused with your Ubuntu root password.

Enter a password and hit Tab to select <Ok>. Hit Enter. You’ll need to enter the password again.

As shown above, you will be presented with some detailed information on MySQL Server configuration. Now hit Tab to select <Ok> and press Enter again:

In this step, you would need to select a default authentication plugin. Go ahead and choose ‘Use Strong Password Encryption’. Click on Tab and then hit Enter.

You have now successfully installed MySQL using Ubuntu official repositories.

Once MySQL is installed on your system, the mysql.service should be up and running automatically. To check this , run the command below :

sudo systemctl status mysql.service

If this is not your case,, .i.e. the service is not active or running, execute the following command in order to start it:

sudo systemctl start mysql.service

To stop the mysql service, type in the command :

sudo systemctl stop mysql

Uninstalling MySQL

To uninstall MySQL, you would first need to disable the service by running the command:

sudo systemctl stop mysql.service && sudo systemctl disable mysql.service

In case you set out to re-use your databases, make sure to back them up beforehand.
To uninstall MySQL issue the command:

sudo apt purge mysql*

To clean up potential hanging dependencies:

sudo apt autoremove

Conclusion

In this article, you have seen how to install MySQL in Ubuntu using two methods: the apt command and Ubuntu official repos.

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply

Close Menu