Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to establish an SSH tunnel so that my local PHP script can connect to a MySQL server on a remote server.

Remote machine: MySQL ( on Linux (SSH port 44422)
Local machine: Linux (Debian Squeeze)

The problem is that it doesn't allow me to connect with this command:

ssh -oPort=44422 user@ip_address

How do I establish an SSH tunnel so that my PHP script can connect to the remote MySQL?

(If you believe that an alternative connection method, such as establishing SSH tunnel via PHP script is better, please let me know.)

Thanks in advance!

EDIT: I forgot to mention that the SSH has a separate username/password and MySQL has another username/password.

share|improve this question
up vote 2 down vote accepted

you need to create a forward ssh tunnel for this

ssh user@ip -p 44422 -L3306:

then point your mysql queries at

or if you are already in an ssh session with the remote host you can ~C to open the ssh shell and then type -L3306: and hit return to add the tunnel to your session

ALL of your mysql connections will rely on your ssh session staying alive so I would look into a more permanent solution like SSL.

share|improve this answer
+1 for the ~C that I didn’t know before. – erik Mar 14 '14 at 23:18

Not exactly answering your question (h3rrmiller gave you a good answer already), but I would suggest not doing this. This will break all your mysql connections when your ssh connection dies, and they can only be reestablished when you set up a new ssh connection.

If you're doing this to encrypt the mysql traffic: mysql supports SSL for connections too, that's a much better alternative.

share|improve this answer
another solution would be a VPN Connection. – Bonsi Scott Nov 15 '12 at 18:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.