How to connect to a MySQL database from an external host
The MySQL server runs on each local hosting server on TCP Port 3306 (the standard port for MySQL). When your web scripts connect to MySQL they can do so usinglocalhost
as the server name. However, you can also connect to your databases from an external host, such as your home PC, office, or another web server.
Preparing to connect
Before getting started you will need the IP address of the host you are connecting from, this can be done by browsing to https://viewmyip.co.uk on the device in question or asking the provider of the server you're connecting from.
This guide assumes you have already successfully logged into cPanel.
- Firewall access
Due to changes in how we handle remote MySQL connections, we no longer need to manually whitelist IP addresses in our firewall. This means the only consideration you need to apply at this stage is whether your IP address is static or dynamic.
If your IP address is not static and changes on a regular basis, then you will need to keep in mind you will need to add each new IP address following on from step 2, as such, we recommend looking into an SSH tunnel so you can connect to MySQL via the SSH tunnel.
This is an advanced topic for most, although we are happy to offer advice, and there is a guide here which explains how to connect to MySQL using an SSH tunnel. - MySQL access
Click on the Remote MySQL icon in the DATABASES section of cPanel. - Enter your IP address in the Add Access Host and if required a Comment (e.g. who the IP address belongs to) and click the Add Host button. That will grant access to MySQL for the host you provided.Please note, although shown in cPanel, Wildcards will not work.
- Once added the host will appear in the Manage Access Hosts. The comment can be updated, and hosts can be deleted here.
BE AWARE THAT THIS METHOD OF CONNECTION IS NOT SECURE - Credentials are passed unencrypted, so are open to eavesdropping attacks. For greater security we recommend you connect using an SSH tunnel - link opens in a new tab.