I opened a ssh tunnel as described in this post: Zend_Db: How to connect to a MySQL database over SSH tunnel?
But now I don’t know what I actually did. Does this command affect anything on the server?
And how do I close this tunnel, because now I can’t use my local mysql properly.
I use OSX Lion and the server runs on Ubuntu 11.10.
Assuming you ran this command:
ssh -f user@mysql-server.com -L 3306:mysql-server.com:3306 -Nas described in the post you linked.A breakdown of the command:
ssh: that’s pretty self-explanatory. Invokesssh.-f: (From theman sshpage)Essentially, send
sshto background once you’ve entered any passwords to establish the connection; it gives the shell prompt back to you atlocalhostrather than logging you in toremote-host.user@mysql-server.com: the remote server you’d like to log into.-L 3306:mysql-server.com:3306: This is the interesting bit.-L(from theman sshpage):So
-L 3306:mysql-server.com:3306binds the local port3306to the remote port3306on hostmysql-server.com.When you connect to local port
3306, the connection is forwarded over the secure channel tomysql-server.com. The remote host,mysql-server.comthen connects tomysql-server.comon port3306.-N: don’t execute a command. This is useful for “just forwarding ports” (quoting the man page).Yes, it establishes a connection between localhost and mysql-server.com on port 3306.
If you’ve used
-f, you’ll notice that thesshprocess you’ve opened heads into the background. The nicer method of closing it is to runps aux | grep 3306, find thepidof thessh -f ... -L 3306:mysql-server.com:3306 -N, andkill <pid>. (Or maybekill -9 <pid>; I forget if justkillworks). That has the beautiful benefit of not killing all your othersshconnections; if you’ve got more than one, re-establishing them can be a slight … pain.This is because you’ve effectively “captured” the local
mysqlprocess and forwarded any traffic that attempts to connect to it, off to the remotemysqlprocess. A much nicer solution would be to not use local port 3306 in the port-forward. Use something that’s not used, like 33060. (Higher numbers are generally less used; it’s pretty common to port-forward a combination like this: “2525->25”, “8080->80”, “33060->3306” or similar. Makes remembering slightly easier).So, if you used
ssh -f user@mysql-server.com -L 33060:mysql-server.com:3306 -N, you’d then point your Zend connect-to-mysql function tolocalhoston port33060, which would connect tomysql-server.comon port3306. You can obviously still connect tolocalhoston port3306, so you can still use the localmysqlserver.