How to install MySQL on Ubuntu 18.04

Last updated on July 01, 2020

MySQL is an open-source relational database management system, here the tutorial is how to install MySQL database management system on Ubuntu 18.04.

Prerequisites

Make sure you have a non-root user account with sudo privileges.

Installing MySQL on Ubuntu 18.04

Before going to install MySQL need to update the apt package by using the following command:

sudo apt update

Then, install MySQL package using the following the command:

sudo apt install mysql-server

Once the MySQL package installation is completed, the MySQL service will start automatically. To verify it using the following command:

sudo systemctl status mysql

We hope you are getting the output like this:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-06-20 11:30:23 PDT; 5min ago
 Main PID: 17382 (mysqld)
    Tasks: 27 (limit: 2321)
   CGroup: /system.slice/mysql.service
           `-17382 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Configuring MySQL

MySQL package comes with the script called mysql_secure_installation. You need to run the script and enable those security-related operations. It will prevent the database from local and remote user attacks.

Run the script by using the below command:

sudo mysql_secure_installation

It will promote you can make some changes to your MySQL installation’s security options. The first prompt is to check the strength of your MySQL password using the Validate Password Plugin. Next is to set the password for the MySQL root user. Enter and then confirm a secure password of your choice.

Next, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

Adjusting User Authentication

In Ubuntu running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin. In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. For this, you need to open up the MySQL prompt using the following command:

sudo mysql

Next, to check the MySQL users authentication method by using the below command:

SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

In this, the root user using auth_socket plugin for authentication. By using the below command you can alter the root user authentication by strong passwords. In the below command replace password with your own strong password for root user authentication.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Finally, run the FLUSH PRIVILEGES MySQL command to reload all the tables and put your new changes into effect.

FLUSH PRIVILEGES;

Conclusion

Now that your MySQL server was up and run successfully on Ubuntu 18.04. If you are facing any issues during the installation feel free to post your issues in the comment section we will rectify your issues as soon as possible.