Logging to remote MySQL Server using SSH tunneling

17 / Sep / 2012 by Sachin 1 comments

Very often, we all need to access a remote database for debugging or any other related stuff.

The simplest thing that comes to mind is to take the dump of remote database and bring it to the local and run the application using that data.

We can avoid that, if we can connect our local mysql client to a remote machine over a SSH connection so that we can run queries on remote machine from our client machine, without dumping the database of remote machine.

To do that, we need to just need to remote machine over SSH with -L option


ssh -L 32001: ubuntu@hostname.com


This means that we redirected call on port number 32001 to port number 3306 on the remote machine and all responses from the remote machine on 3306 will be redirected to 32001 on the local machine.

All we need to do now is to open mysql client and attach it to port number 32001 on local machine, we need to do


mysql -h -P 32001 -u’username’ -p’password’


Here we are connecting to local machine on port number 32001, with username and password for that of the remote machine.


Sachin Anand




comments (1 “Logging to remote MySQL Server using SSH tunneling”)

  1. grails mysql

    this is a good way instead of directly logging into server via ssh. because you might accident your server and cause problems


Leave a Reply

Your email address will not be published. Required fields are marked *