Install MySQL on Ubuntu 18.04 Bionic Beaver Linux

Objective

The objective is to install MySQL client or MySQL server on Ubuntu 18.04 Bionic Beaver Linux.

Operating System and Software Versions

  • Operating System: – Ubuntu 18.04 Bionic Beaver
  • Software: – MySQL 5.7

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Instructions

Install MySQL client on Ubuntu

If you only wish to connect to MySQL server remotely but missing the mysql command you can install it my executing:

$ sudo apt install mysql-client

Confirm the correct installation by retrieving MySQL client’s version number:

$ mysql -V
mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper

To connect remotely to a MySQL server use the following syntax:

$ mysql -u USERNAME -p PASSWORD -h HOST-OR-SERVER-IP

Install MySQL Server on Ubuntu

The following linux command will install MySQL server on your Ubuntu 18.04 Bionic Beaver host:

$ sudo apt install mysql-server

Before going live make sure to secure your MySQL installation by executing:

$ sudo mysql_secure_installation

If you wish to connect to your MySQL server remotely using MySQL client edit the /etc/mysql/mysql.conf.d/mysqld.cnf configuration:

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

and change the bind-address

FROM:
bind-address            = 127.0.0.1
TO:
bind-address            = 0.0.0.0

Once ready, reboot your MySQL server:

$ sudo service mysql restart


MySQL server should now be listening on port 3306 on all interfaces 0.0.0.0:

$ ss -ltn
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      80        0.0.0.0:3306                  0.0.0.0:* 
LISTEN      0      128       0.0.0.0:5355                  0.0.0.0:*
LISTEN      0      128       0.0.0.0:22                    0.0.0.0:*            
LISTEN      0      128          [::]:5355                     [::]:*                  
LISTEN      0      128          [::]:22                       [::]:*                
LISTEN      0      128         [::1]:6010                     [::]:*                  
LISTEN      0      128         [::1]:6011                     [::]:*

In case you are running the ufw firewall the following linux command will allow TCP incoming traffic from any source to your Tomcat 8 server’s port 3306:

$ sudo ufw allow from any to any port 3306 proto tcp

If you wish to make your firewall rules more strict visit our How to Open/Allow incoming firewall port guide for more information.