There are ways to configure an SQL server to allow remote connections, but you need to be careful, as allowing remote connections on a MySQL server can make your database an easy target for hackers. If you want to allow secure, remote connections to a MySQL database, here’s what you’ll need to know.

Before You Begin

Before you make any changes to your MySQL database, it’s important that you backup your database, especially if you’re working on a production server (a server in active use). Any changes you make to your database, or the server hosting it, could result in serious data loss if something goes wrong. You may also find that changes to your server’s connections may prevent you from accessing it afterward. If this happens, you may need to consult with a server administrator for further support. A good idea is to trial any changes on a locally-run MySQL server to check if your changes work before trying them remotely. It’s also likely that if you’re making changes to a remote server, you’ll need a safe way to connect and make changes. SSH (Secure Shell) is often the best way to do this, as it allows you to connect to your remote server. You can also use SSH to connect to servers on your local network, such as those hosted on a Raspberry Pi. This guide will lead you through the steps to configuring MySQL to allow remote connections, but you’ll need to ensure you have direct or remote access to the server hosting your MySQL server first. Suppose you don’t have remote access to your server via SSH (for example). In that case, you won’t be able to configure your MySQL database to allow remote connections directly unless your root mySQL account already allows remote connections. So, you will need to establish this connection first before you can proceed.

Editing Your MySQL Configuration File

The first step in configuring MySQL to allow remote connections is to edit your MySQL configuration file. By this stage, this guide will assume you have already connected to the server, PC, or Mac hosting your mySQL database remotely and have console access.  Alternatively, you can configure a local MySQL server using an open terminal on Mac or Linux or a text editor on Windows. The locations referenced above are the default locations for MySQL configuration files. If these commands don’t work, you’ll need to search for the relevant files (my.cnf, mysqld.cnf, or my.ini) manually to locate the relevant file path.

Setting a Safe Bind-Address IP Range

If you’re unsure of the correct service name on Windows, type net start to find it. If you can’t reload your configuration, restart your server and reload MySQL manually (if necessary) instead.

Configuring Your Firewalls

At this stage, your MySQL database should allow remote connections from devices using the IP address you set as the bind-address value in your MySQL configuration file (or from all devices if you set this value to 0.0.0.0 instead). However, connections will still be blocked by your device or network firewall. Most servers and PCs use a firewall to block connections unless access to a specific port is granted. The steps to configure this will vary, depending on whether you’re running MySQL on Windows or Linux. Mac firewalls are disabled by default, so you shouldn’t need to complete any additional steps here.

Configure Linux Firewalls

Many Linux servers use iptables as the default firewall utility. You can configure it by following the steps below. If iptables isn’t the default firewall tool for your Linux distribution, you’ll need to consult your distribution’s user manual for more information. If certain packages (such as netfilter-persistent) are unavailable, use your distribution’s software repository tool to install it (eg. sudo apt install netfilter-persistent).

Configure Windows Firewalls

If you’re using a Windows PC or server to host your database, you can configure your firewall using these steps: If you have trouble connecting, repeat these steps above, making sure to create a new outbound rule in your firewall settings using the same details (port 3306, etc). You may also need to configure your local network router to open the necessary blocked ports to allow inbound and outbound connections to your database. 

Connecting to a Remote Server Using MySQL

After configuring your MySQL database to allow remote connections, you’ll need to actually establish a connection to it. You can do this using the mysql command (mysql.exe on Windows) from a terminal or PowerShell window. If you’re running Windows, you will need to make sure that MySQL is installed locally before you begin. Mac users can install MySQL using homebrew from the terminal (brew install mysql), while Linux users can use their local app repository (eg. sudo apt install mysql) to install the necessary packages.

Connecting to MySQL on Linux or Mac

Connecting to MySQL on Windows

If this doesn’t work, connect to the server or PC hosting your MySQL server using SSH (or access it directly) using these steps and using the -h localhost argument. You can then create a suitable user account by following the steps below.

Allowing Remote User Access to a MySQL Database

By this point, you should be able to connect to your MySQL server remotely using your server’s root user account or another user account with elevated privileges. As this level of access is unsafe, you may prefer to create a more restricted account for accessing your MySQL database. This account will have limited access to your MySQL server, allowing it to interact with only selected databases. It won’t be able to make more serious changes, such as accessing other database data, creating new user accounts, etc.  You’ll need to have the ability to sign into your MySQL server remotely. If you can’t use your root account remotely, you’ll need to access your server’s shell using the mysql command via a remote SSH connection or by directly accessing the PC or server hosting the server. With access granted, use the steps in the section above to connect to your server remotely using your new account (eg. mysql -u username -h X.X.X.X:XXXX -p).

Securing Your Database Data

Whether you’re working with MySQL or another type of SQL database, it’s important to keep your connections secure to maintain your data security. A good way to do this is to generate SSH keys for remote access to your server, rather than relying on outdated (and easily guessable) passwords. If you’re worried about data loss, you can easily back up your database online. Most databases are run using Linux servers—you can automate a Linux file backup easily. If you’re running MySQL on Windows, you can set up a similar automatic backup system for Windows, allowing you to restore your data in an emergency.

How to Allow Remote Connections to MySQL - 94How to Allow Remote Connections to MySQL - 61How to Allow Remote Connections to MySQL - 82How to Allow Remote Connections to MySQL - 6How to Allow Remote Connections to MySQL - 12How to Allow Remote Connections to MySQL - 65How to Allow Remote Connections to MySQL - 31How to Allow Remote Connections to MySQL - 13How to Allow Remote Connections to MySQL - 38How to Allow Remote Connections to MySQL - 35How to Allow Remote Connections to MySQL - 69How to Allow Remote Connections to MySQL - 84How to Allow Remote Connections to MySQL - 12How to Allow Remote Connections to MySQL - 71How to Allow Remote Connections to MySQL - 20How to Allow Remote Connections to MySQL - 42How to Allow Remote Connections to MySQL - 80How to Allow Remote Connections to MySQL - 10How to Allow Remote Connections to MySQL - 68How to Allow Remote Connections to MySQL - 98How to Allow Remote Connections to MySQL - 15How to Allow Remote Connections to MySQL - 33How to Allow Remote Connections to MySQL - 52How to Allow Remote Connections to MySQL - 5How to Allow Remote Connections to MySQL - 46How to Allow Remote Connections to MySQL - 97How to Allow Remote Connections to MySQL - 97How to Allow Remote Connections to MySQL - 39How to Allow Remote Connections to MySQL - 90