Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 (127.0.0.1:3306) 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

2 Answers 2

up vote 2 down vote accepted

you need to create a forward ssh tunnel for this

ssh user@ip -p 44422 -L3306:127.0.0.1:3306

then point your mysql queries at 127.0.0.1:3306

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:127.0.0.1:3306 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 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

 
discard

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.