a number of articles and blog posts exist that (attempt to) explain how one might go about connecting to a remote mysql server by way of an ssh tunnel… a vast majority of them are a copy-and-paste of the same information on how to do it using PuTTY as go-between.
seeing as how PuTTY serves the WIntel folks, and i prefer my powerbook to my thinkpad these days, i searched for a solution to do this simply on OS X… and found mention, but not working example, of using Terminal to handle the necessary port forwarding.
i looked more carefully at the man pages for ssh, and lo and behold, the -L option will do everything you need.
launch Terminal and issue this command:
ssh [host] -l [username] -p [port] -L 127.0.0.1:3306:[mysql_server]:[mysql_port] -N
[host], [username] and [port] pertain to the login credentials for the ssh connection (the port will usually but not always be 22)… the -L option is to establish forwarding the standard mysql port (3306) from your own computer (127.0.0.1 aka localhost) to the remote mysql server (usually but not always localhost)… -N tells Terminal to make the connection but provide no interaction, so the console will just sit there without giving you a command shell on the remote machine.
with the tunnel and port forwarding established, you can use MySQL Query Browser or another front-end to connect to 127.0.0.1 on port 3306 and you’ll be on your merry way administering rows of data…
* it should go without saying that this won’t work if you are already running an instance of mysql server on your own machine on port 3306.
| bookmark it! | ||||||
|
2 Responses
Hawkeye
February 20th, 2009 at 3:18 pm
1This Terminal command can be used:
ssh -nNL33060:127.0.0.1:3306 username@host
or to background the tunnel so you can use the terminal window for other commands (make note of the process ID it returns so you can “kill” the tunnel when you are finished with it):
ssh -nNL33060:127.0.0.1:3306 username@host &> /dev/null &
Notice the use of local port 33060 instead of 3306; this works even if you have a local mysql server running on your machine, but means you have to connect to the remote server using local port 33060, not 3306. (On the remote end, connection is to port 3306.)
Note: If trying to connect to a mysql server on a machine running Mac OS X Server, the “allow network connections” checkbox must be checked in Server Admin for tunneling to work with GUI tools.
krisgale
February 20th, 2009 at 4:18 pm
2thanks for the additional detail!
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar