I have a MySQL server running on a remote machine and am trying to debug a Django application running locally that connects to that remote database. Our team does this by forwarding the remote port 3306 to the local port 3308 using PuTTY. I found out recently that you can do this within VS Code as well using the Remote - SSH VS Code extension created by Microsoft.
After the SSH connection is configured, this is done by adding the port to the ports tab as shown below:
An alternative, described in the Remote - SSH docs, is to add the port to the ssh config file. This opens the port automatically every time an SSH connection is made:
The trouble with both of these methods is it seems that the SSH connection cannot be made without the entire VS Code window turning into an "SSH Window" that is connected to the remote machine. Since I am working on a the Django application locally, this means I then need a second window open just to forward the port.
I would really like to be able forward a port in the same window that I am working on the local Django application. I would prefer doing it with the Remote - SSH extension, but am open to other methods. Bonus points if the tunnel is created automatically when I open the Django app repo in VS Code.
I tried searching for extensions that would do this and seeing if the Remote - SSH can connect without a dedicated window, but I couldn't find anything.
You don't need the "Remote - SSH" extension to connect over ssh. The ssh config file contains everything you need to make the connection, including the port forward.
Host MyServer
HostName host.address.com
IdentityFile C:\Users\user\identityFile
User ubuntu
LocalForward 127.0.0.1:3308 127.0.0.1:3306
Here I have named the host MyServer
this name can be whatever you like so long as the HostName
contains the right address to the server. With this, you just need to type:
ssh MyServer
into your terminal and you've set up your port forwarding! You can run this alongside your python manage.py runsever
to develop your Django application using split terminals in VS Code.
The second part to this is automatically opening the split terminals, one of which runs the ssh command. This is covered in this question: Save terminal tabs to saved workspace VSCode
I found the first answer to the linked question easiest and implemented the ssh solution in the workspace settings.json
file as follows:
{
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "Remote Server",
"icon": "vm-connect",
"commands":["ssh MyServer"],
},
{
"name": "Django",
"icon": "terminal",
"commands":["python manage.py runserver"],
}
]
}
],
}