Install and configure MySQL on Ubuntu 18.04

MySQL is an open-source database engine, suitable for small to large sites.

This article walks you through a basic installation of MySQL on an Ubuntu 18.04 LTS server.

Prerequisites

You need access to a Linux server, with sudoers or root privileges. Adding sudo to your account is outside the scope of this tutorial.

Installation

Update package information

Update your package information before you get started. For many of these Linux commands, you will receive multiple screens of output that this guide will shorten to ‘‘, but here are the relevant messages to look out for:

sudo apt-get update

...
Fetched 9981 kB in 2s (4052 kB/s)
Reading package lists... Done

Install MySQL

sudo apt install mysql-server -y

...
Setting up mysql-server (5.7.28-0ubuntu0.18.04.4) ...
...

In our example, Ubuntu installed MySQL 5.7 by default.

Verify installation

To verify that MySQL is installed and started on your system, run:

sudo systemctl status mysql.service

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-12-13 19:06:10 UTC; 2min 26s ago
 Main PID: 2742 (mysqld)
    Tasks: 28 (limit: 2354)
   CGroup: /system.slice/mysql.service
           └─2742 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Dec 13 19:06:09 derek-mysql-ubuntu systemd[1]: Starting MySQL Community Server...
Dec 13 19:06:10 derek-mysql-ubuntu systemd[1]: Started MySQL Community Server.

You should see Active: active (running) as in the example above. If you do not, run:

sudo systemctl start mysql.service

Enter the MySQL shell to ensure that you have access. Then type exit to exit the shell.

mysql

mysql> exit
Bye

Now you can reach your MySQL from another remote host by using:

mysql -h host_name -u user -p

Run the Secure Installation Tool (Optional)

Finally, we recommend that you run MySQL Server’s built-in secure installation tool. This will walk you through several steps intended to close some known vulnerabilities with the default MySQL installation, and will prompt you on which changes it should make at every step:

root@real-lynx-42:~# 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?

...

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

All done!

Conclusion

In this article, you learned how to install MySQL on Ubuntu 18.04 LTS. The next steps should include configuring your firewall to properly allow only the needed traffic to your MySQL server instance and configuring users with the correct permissions for your needs.

Leave a Reply

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