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.

2 Answers 2


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.

  • another solution would be a VPN Connection. Nov 15, 2012 at 18:58

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.

  • +1 for the ~C that I didn’t know before.
    – erik
    Mar 14, 2014 at 23:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .