YouTube Icon

Code Playground.

How to Connect to MySQL through SSH Tunnel


How to Connect to MySQL through SSH Tunnel

Of course, the MySQL worker listens just on localhost, which implies it tends to be gotten to simply by applications running on a similar host. 

Nonetheless, in certain circumstances, you should associate with the worker from far off areas. One choice is design the MySQL worker to permit far off associations , however that requires managerial advantages, and it might cause security hazards. 

A safer option is make a SSH burrow from the nearby framework to the worker. SSH burrowing is a technique for making an encoded SSH association between a customer and a worker machine through which administrations ports can be handed-off. 

In this guide, we will disclose how to make a SSH passage and interface with the MySQL worker from far off customers. Similar directions apply for MariaDB. 


  • SSH Client. 
  • SSH admittance to the framework on which the MySQL worker runs. 

Create an SSH Tunnel on Linux and macOS 

The ssh customer is preinstalled on most Linux and Unix-based frameworks. 

In the event that you are utilizing Linux or macOS as your working framework, you can make a SSH burrow utilizing the accompanying order: 

ssh -N -L 3336: [USER]@[SERVER_IP]

The alternatives utilized are as per the following: 

  • - N - Tells SSH not to execute a distant order. 
  • - L 3336: - Creates a nearby port sending. The neighborhood port (3306), the objective IP ( and the far off port (3306) are isolated with a colon (:). 
  • [USER]@[SERVER_IP] - The distant SSH client and worker IP address. 
  • To run the order out of sight, utilize the - f alternative. 

On the off chance that the SSH worker is tuning in on a port other than 22 (the default) indicate the port with the - p [PORT_NUMBER] choice. 

When you run the order, you'll be provoked to enter your SSH client secret phrase. Subsequent to entering it, you will be signed in to the worker, and the SSH passage will be set up. It is a smart thought to set up a SSH key-based confirmation and interface with the worker without entering a secret phrase. 

Presently you can point your neighborhood machine MySQL customer to enter the distant information base login qualifications and access the MySQL worker. 

For instance, to associate with the MySQL worker utilizing the order line mysql customer you would issue: 

mysql -u MYSQL_USER -p -h

Where MYSQL_USER is the far off MySQL client having advantages to get to the information base. 

When incited, enter the MySQL client secret phrase. 

To end the SSH burrow type CTRL+C in the support where the ssh customer is running. 

Create an SSH Tunnel on Windows

Windows clients will initially need to download and introduce a SSH customer program. The most famous Windows SSH customer is PuTTY. You can download PuTTY here . 

Play out the accompanying strides to make a SSH passage to the MySQL worker with PuTTY: 

Dispatch Putty and enter the IP Address of the worker in the Host name (or IP address) field: 

Under the Connection menu, extend SSH and select Tunnels. Enter 3306 in the Source Port field, and in the Destination field: 

Snap on the Add catch to add the passage. 

Return to the Session page to spare the settings with the goal that you don't have to enter them once more. 

Enter the meeting name in the Saved Session field and snap on the Save button. 

Select the spared meeting and sign in to the distant worker by tapping on the Open catch. 

Another window requesting your username and secret key will appear. When you enter the username and secret phrase, you will be signed in to the worker, and the SSH passage will be made. 

Setting up open key validation will permit you to interface with the worker without entering a secret word. 

You would now be able to associate with the distant information base utilizing your nearby MySQL customer. 

For instance, in the event that you are utilizing HeidiSQL enter in the Hostname/IP field and the MySQL client and secret key in the User and Password fields: 


MySQL, the most famous open-source information base worker, tunes in for approaching associations just on localhost. Making a SSH burrow permits you to safely interface with the distant MySQL worker from your nearby customer. 

In the event that you have questions, don't hesitate to leave a remark underneath.