Dynamic Port Forwarding<\/li><\/ul>\n\n\n\nBy default, MySQL or PostgreSQL server only listens on localhost which means it can only be accessed by other applications hosted on the same server. To connect remotely to the server, we need to make MySQL or PostgreSQL listen not only on localhost, we would also need to add or edit the database user’s permission to be able to connect remotely, modify the firewall, etc. For some people, this would take time and they would use SSH tunneling instead.<\/p>\n\n\n\n
In this tutorial, we will show you how to connect to a MySQL server through SSH tunneling, or local port forwarding.<\/p>\n\n\n\n
Prerequisites:<\/h3>\n\n\n\n SSH Client SSH login credentials MySQL client<\/p>\n\n\n\n
Create SSH Tunnel and Connect to the Database Server from Windows<\/h2>\n\n\n\n To connect to your server where the MySQL server is hosted, you will need an SSH client. If you use Windows as your computer’s operating system, you can use a free software called PuTTY to connect to your server. You can download PuTTY from https:\/\/www.chiark.greenend.org.uk\/~sgtatham\/putty\/latest.html<\/code><\/p>\n\n\n\nOnce downloaded, run the application and enter the following information:<\/p>\n\n\n\n
Host Name (or IP address) \u2013 enter your server hostname or its IP address. Port \u2013 enter the listening port of your server\u2019s SSH daemon Connection type \u2013 SSH<\/p>\n\n\n\n <\/figure>\n\n\n\nOn the left side navigation tree in PuTTY, click on Connection > SSH > Tunnels. Enter the database server (MySQL, PostgreSQL, etc) port under the “Source port”. Then in the destination box type “127.0.0.1:3306” and finally click on Add. <\/p>\n\n\n\n <\/figure>\n\n\n\nNow, click on the Session in the Category navigation tree, type “tunnel” in the “Saved Session” box then click on Save button.<\/p>\n\n\n\n <\/figure>\n\n\n\nIn the future, you do not need to create a new session for tunneling, you can simply load the saved session. Now, we can click on the Load button and you will be asked for your SSH login credentials.<\/p>\n\n\n\n <\/figure>\n\n\n\n<\/h2>\n\n\n\n In this tutorial, we are going to use MySQL shell to access the remote MySQL server through SSH tunneling. Once the SSH session is connected, you can open your Windows command prompt and go to your MySQL shell directory and run the following command as if you are accessing from the same server.<\/p>\n\n\n\n
mysqlsh -u sshtunnel -p -h localhost<\/pre>\n\n\n\n <\/figure>\n\n\n\nAs seen in the picture, we are using MySQL user ‘sshtunnel’, make sure you change this to your MySQL username. Once connected, you will see “MySQL localhost:3306 ssl JS<\/code>“, to switch to MySQL mode just type \\sql <\/code>and hit enter.<\/p>\n\n\n\nNow, you are in MySQL mode and you should be able to run MySQL shell commands.<\/p>\n\n\n\n
Create SSH Tunnel and Connect to the Database Server from Linux and MacOS<\/h2>\n\n\n\n If you are using a Linux distro or MacOS, you do not need to download an SSH client because it’s already available on your system.<\/p>\n\n\n\n
To connect to your MySQL server through SSH, you can use Terminal. Run Terminal and connect to your server by invoking this command:<\/p>\n\n\n\n
ssh -fNg -L 3306:127.0.0.1:3306 sshtunnel@123.123.123.123 -p7022<\/pre>\n\n\n\n <\/figure>\n\n\n\nWith this command, we are going to log in to 123.123.123.123 (replace this with your server’s actual IP address) with sshtunnel as the SSH username through port 7022 (replace this with your SSH listening port), you will be asked for “sshtunnel” password then once entered the command will go into the background (-f) and not execute remote commands (-N), and set up port-forwarding (-L local_port:localhost:remote_port). In this example, we will use 3306 both as a local port and a remote port. If you already have a MySQL server running on your machine, you can use another port.<\/p>\n\n\n\n
Once connected, you should be able to connect to a remote database locally, just like accessing a local database.<\/p>\n\n\n\n