Configuring MySql For remote access

MySQL server by default will not allow remote access for security reasons. This is great when you applications and Webserver exist on the same machine, but sometimes you need to access the database from an external machine.

This installation will detail how to allow remote access to your databases. While tailored for Ubuntu Linux should be the same for other flavours of linux.

Prerequisites

  • A server running Ubuntu 16.04 or above
  • Sudo or root access on the server

Install MySQL

Before starting you should ensure your server has the latest updates installed

sudo apt-get update
sudo apt-get upgrade

Next install the MySQL package

sudo apt-get install mysql-server

Once the installation is complete we will need to configure it.

mysql_secure_installation

First up enter a new password, enable VALIDATE PASSWORD PLUGIN if you wish the system to ensure that passwords are secure.

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: n
Please set the password for root here.

New password:

Re-enter new password:

By default MySQL allows anonymous users to access, this should be removed for security reasons.

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) : y
Success.

Next decide whether you want 'root' to have remote access to the server. You should not allow this for security reason and should instead setup user level access to the databases as detailed below.

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

Next you should remove the 'test' database as it there for test purposes.

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) : y

Finally reload the the privilege tables to ensure the changes above take effects.

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!

Configure remote access

Lets first connect to the server and create a new database to test remote access.

sudo mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 15
Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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>

At the mysql> prompt create a new database

CREATE DATABASE myproject;

Next we'll create a test user

create user 'myuser'@'localhost' identified by 'mypassword1';

This creates a local user 'myuser' with the password 'mypassword1'. By default it does not have access to any databases or tables. We need to grant access to allow the user to logon.

grant all privileges on myproject.* to 'myuser'@'localhost';

This give the user 'myuser' full admin access to the database myproject and all (*) tables of that database.

If you test the test the remote access to the server you will see that you still can't connect. This is due to the MySQL server listening only on the loopback address 127.0.0.1 .

mysql -h yourhost_addess -u myuser -p 

To fix this we need to make a small change to the MySQL configuration files to enable full remote access. Edit the file '/etc/mysql/mtsql.conf/mysqld.cnf'

sudo nano /etc/mysql/mtsql.conf/mysqld.cnf

Find the line and comment (#) it out

bind-address           = 127.0.0.1

Save the file and then we just need to restart the MySQL service

sudo systemctl restart mysql

Try to remote logon to the server again and you should have access.

mysql -h yourhost_address -u myuser -p 

You will have access to the configured databases only

Tagged Share this page

Leave a Reply