How To Install MySQL Database on Ubuntu 18.04 LTS

On this short guidance we will explain how to install MySQL on Ubuntu 18.04 TLS.

Introduction

MySQL is the most popular open source SQL database management system with around 6 million installations worldwide. MySQL was built and maintenance by MySQL AB before Sun Microsystems Inc take acquisition action against MySQL AB on January 16, 2008. And finnaly one year later, Oracle’s turn to acquire Sun Microsystems Inc. Since then, since then MySQL has officially become a family of Oracle products.

MySQL is very popular in web applications such as MediaWiki (software used by Wikipedia and similar projects) and PHP-Nuke and functions as a database component in LAMP. Popularity as a web application due to its proximity to the popularity of PHP, so it is often referred to as Dynamic Duo. In this short tutorial, we will explain how to install mysql on the Ubuntu 18.04 LTS operating system.

MySQL Installation On Ubuntu

The MySQL installation process will consist of several stages, namely:

  • Prerequisite
  • Installing MySQL on Ubuntu 18.04
  • Securing MySQL
  • Trying MySQL

Prerequisite

Before we are going to install MySQL database on Ubuntu 18.04 LTS, we have to prepare the environment first as mentioned below :

  • Ubuntu 18.04 LTS System
  • root or ordinary account with sudo privilege
  • Sufficient disk space

Installing MySQL on Ubuntu 18.04

On this tutorial, we will install MySQL version 5.7. To install MySQL on Ubuntu server, we have to add MySQL repository to our system first, after that we just install it.

1. Updating Local Repository

By updating local repository, we will update all repository to the latest version. To do this task, we will use the following command line :

$ sudo apt-get update

The output will be shown below :

ramans@ubuntu:~$ sudo apt-get update
[sudo] password for ramans:
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:2 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]   
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 252 kB in 4s (58.7 kB/s)                       
Reading package lists... Done

2. Installing MySQL Database Engine

After updating local repository, we will do the installation by submitting command line :

$ sudo apt-get install mysql-server

The output will be shown below :

ramans@ubuntu:~$ sudo apt-get install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libllvm8
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 9 newly installed, 0 to remove and 17 not upgraded.
Need to get 20.5 MB of archives.
After this operation, 161 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

...

Renaming removed key_buffer and myisam-recover options (if present)
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up mysql-server (5.7.27-0ubuntu0.18.04.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for systemd (237-3ubuntu10.29) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...

After the installation is completed, the MySQL service will start automatically. We will verify it by submitting command line :

$ systemctl status mysql.service

The output will be shown below :

ramans@ubuntu:~$ systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-09-26 04:40:02 PDT; 19min ago
 Main PID: 23601 (mysqld)
    Tasks: 29 (limit: 5373)
   CGroup: /system.slice/mysql.service
           └─23601 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Sep 26 04:40:02 ubuntu systemd[1]: Starting MySQL Community Server...
Sep 26 04:40:02 ubuntu systemd[1]: Started MySQL Community Server.

We can also verify the MySQL installation by querying the version, by submitting command line :

$ mysql --version

The output will be shown below :

ramans@ubuntu:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using  EditLine wrapper

Securing MySQL

MySQL is equipped with scripts for handling security issues. For fresh installation, we are recommended to run a script that can increase the security level of the MySQL database that we have had installed. To do this task, we just type the command line :

$ sudo mysql_secure_installation

The output will be as following :

ramans@ubuntu:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: Y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : N

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done!

Trying MySQL

On this sub section, we will try to login to our MySQL databases. When we are running MySQL 5.7 (and later), by default the root user is authenticated by the auth_socket plugin. We will login and list all database which was formed since installation. To do so, we just type the command line :

$ sudo mysql

The output will be shown below:

ramans@ubuntu:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  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 | *5C1961C39B0E96E5F2F1F3A76E0C848BC7FF88D5 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

Conclusion

On this article, we have installed MySQL version 5.17 on Ubuntu 18.04 LTS operating system.

Add a Comment

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