How to Connect to MySQL Server Databases Remotely ?

Topics Covered

MySQL offers a variety of tools and features to its users to make database management more convenient and user-friendly. One such feature is remote access to MySQL server databases.

Any host or local device that fulfills the required prerequisites and has access to the server can access the database from any remote location without any issue.

Pre-requisites for accessing MySQL server databases remotely:

  1. Check whether remote connections in MySQL are enabled or not. By default, the remote connections are disabled. Enable them if necessary.
  2. Check whether the network connectivity is fine or not.
  3. Verify whether or not the machine you are using for remote access to the MySQL server has all the required permissions.

Steps to Connect to MySQL Remotely:

Step 1:

Go to the MySQL configuration files (my.cnf or my.ini) and set the bind-address parameter to the IP address of the system that needs to access the MySQL server databases remotely.

Editing the bind-address parameter is a very important step to connect to MySQL remotely.

Edits in the "bind-address" parameter:

  • The bind-address ensures or configures which network connection will be accessed by the machine.
  • We can set the bind-address parameter in the configuration files of MySQL.
  • The default bind address of the configuration files adheres to the value 127.0.0.1.
  • The bind address has to be set to the IP address of the host's device if you want to access the remote device using a local device.
  • Setting up the bind address to 0.0.0.0 will allow any device having access to the MySQL server to access the database.

You can use these steps for both WINDOWS and LINUX operating systems.

Step 2:

Check whether the MySQL user account that you are using has the GRANT ALL privilege or specific privileges such as SELECT, INSERT, UPDATE, and DELETE on the database or tables you want to access remotely.

Step 3:

Open the MySQL client application, such as MySQL Workbench, and create a new connection. Enter the server's IP address, port (default is 3306), and MySQL user credentials (username and password of the user).

After completing the following steps, you can now connect to the MySQL server.

How to Enable and Set up a Remote MySQL Server Connection

Step 1:

Provide the user GRANT ALL privilege by using the following SQL query:

This query will provide the user with the needed privilege, for remote access to the MySQL server.

Step 2:

Set up the firewall for remote access to MySQL server databases by allowing all incoming traffic on the MySQL port (default port is 3306).

You can set up the firewall in the Windows Defender Firewall option.

Click on "Advanced Settings" on the windows. Go to the inbound rules choose the TCP in the following menu and set the PORT to 3306.

Once you have completed the mentioned steps you can configure all the network traffic to the MySQL server port which is 3306. After this configuration, you can use the internet access in MySQL.

How to Grant and Set up User Access from the Remote Host

To grant remote access to a MySQL server to a user, you have to first create a MySQL user instance. You can also use any existing user for this purpose.

This is the MySQL query to create a user instance:

Then, you have to grant the user the required permissions for remote access to the server.

This is the MySQL query to grant privileges to a user:

This query will grant the required privileges to the mentioned user.

The GRANT ALL query is always followed by the FLUSH PRIVILEGES keyword.

Flushing Privileges is a very important part to connect with MySQL remotely.

FLUSH PRIVILEGES:

This keyword reloads all the permissions of a user that is present in the memory buffer.

When a user account is created or modified, or privileges are granted or revoked the changes in the permission are just stored in the memory and no change happens until the FLUSH PRIVILEGES keyword is used.

It is important to remember that the FLUSH PRIVILEGES keyword only refreshes the changes made in the permission, it does not change any permission. To change the permission of a user we have to use GRANT or REVOKE keywords.

SYNTAX

How to Connect to a Remote Database

You can easily connect to any MySQL database using MySQL Workbench. These are the steps involved in the configuration of MySQL Workbench.

Follow these steps to connect to MySQL remotely:

Step 1: Open MySQL Workbench and go to the Connections page.

Step 2: Click on the Add symbol on the top of any existing connections.

how to connect a remote database

Step 3: This will open a new tab where you have to fill in the details of the server you want to access. Remember to add the details of the server that you want to access from your local machine.

how to connect a remote database

Step 4: Test the connection to check whether it is proper or not, if not check whether you have granted the required permission or not.

After completing these steps, you will be able to connect to the MySQL server remotely from any place.

SSL Protection:

SSL protection stands for Secure Sockets Layer, this is a security protocol that is used to encrypt the transfer of information between host server and client.

In MySQL, to enable SSL protection we have to generate the server certificate and client certificate, then configure the respective server and client to use these certificates while the transfer of data, you also need to make some changes in the configuration file of the MySQL.

Once you have done the required changes in the setting, now you will be able to transfer the data from the server without any risk of a security breach.

SSL protection is considered an important step in connecting to the MySQL server remotely.

Conclusion

  • MySQL offers a variety of features and tools for accessing, manipulating, and changing databases remotely. We can set up a user using the MySQL workbench and provide the GRANT ALL privileges. Followed by setting up the PORT connection to 3306.
  • It is a convenient feature of the remote access service of MySQL that allows anyone to access the database without having access to the server.
  • Remote server access makes it possible to access the database from anywhere, additionally supporting multiple users, and utilizing the service concurrently, increasing efficiency multifold.