Utilize Port Forwarding to Access MySQL Database in Another Private Network

Hands-on how your Golang app can do thatPhoto by Gaelle Marcel on UnsplashSometimes, you must develop an app that interacts with the database in some private network. Your development server can’t access the database (in this case, MySQL) directly beca…


This content originally appeared on Level Up Coding - Medium and was authored by Muhammad Ryan

Hands-on how your Golang app can do that

Photo by Gaelle Marcel on Unsplash

Sometimes, you must develop an app that interacts with the database in some private network. Your development server can’t access the database (in this case, MySQL) directly because it lies in another private network. To access it, you must access another server with public IP first in that private network, and then from there, you can remote the database server to access the database. To make it easier to understand, here the diagram for the condition we talk about.

In this post, I will demonstrate how to access that database in your app. In this case, I will use Golang. Here the step by step:

Do an SSH Tunneling

In this case, we will use port forwarding. The command is

-L here means we will bind local_port for this tunneling and 3306 here is the default port for MySQL. If your MySQL uses a different port, just use that port instead of 3306. database_local_ip is the IP of the database server in that private network. In case the MySQL is in the public server, the command is

Think of this as if you run the app on that server with public IP. What is the database server IP in the perspective of that server, that is how you determine database_local_ip.

When you run this command, all the connections to the local_port in the localhost of your development server will be directed to the database server in the private network.

Extra tips when you successfully connect using SSH, run ping google.com , or ping whatever address you want, to keep the connection alive.

Write The Code that Accesses The Database Through Localhost

All the preparation on the network side is done, now we write the code. The staple code to play with MySQL using Go is

change line 12 to this

where local_port is the same as local_port in the SSH command beforehand. username and password is the username and password of MySQL. So when your app connects through localhost in the port of local_port, that connection will be directed to the database_local_ip with port 3306 (assume your MySQL using default port). It looks like you try to connect with your local MySQL but it’s not.

And that’s how to access MySQL from another private network. Another tip is that if the port forwarding fails, the resulting query will return nil whatever query. I will not elaborate further because it’s out of topic and will need more explanation than this topic. We will save the topic about Go and MySQL in another post.


Utilize Port Forwarding to Access MySQL Database in Another Private Network was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Muhammad Ryan


Print Share Comment Cite Upload Translate Updates
APA

Muhammad Ryan | Sciencx (2021-04-06T15:18:29+00:00) Utilize Port Forwarding to Access MySQL Database in Another Private Network. Retrieved from https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/

MLA
" » Utilize Port Forwarding to Access MySQL Database in Another Private Network." Muhammad Ryan | Sciencx - Tuesday April 6, 2021, https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/
HARVARD
Muhammad Ryan | Sciencx Tuesday April 6, 2021 » Utilize Port Forwarding to Access MySQL Database in Another Private Network., viewed ,<https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/>
VANCOUVER
Muhammad Ryan | Sciencx - » Utilize Port Forwarding to Access MySQL Database in Another Private Network. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/
CHICAGO
" » Utilize Port Forwarding to Access MySQL Database in Another Private Network." Muhammad Ryan | Sciencx - Accessed . https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/
IEEE
" » Utilize Port Forwarding to Access MySQL Database in Another Private Network." Muhammad Ryan | Sciencx [Online]. Available: https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/. [Accessed: ]
rf:citation
» Utilize Port Forwarding to Access MySQL Database in Another Private Network | Muhammad Ryan | Sciencx | https://www.scien.cx/2021/04/06/utilize-port-forwarding-to-access-mysql-database-in-another-private-network/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.