More SSH fun - tunneling mysql over ssh
I use ssh for port forwarding a lot. One of the most frequent uses is to allow for secure local access to a remote mysql server over ssh. In order for this to work you must have ssh accesss to the mysql box.
This is the format of the command. You must replace LOCAL_PORT and REMOTE_PORT with the ports you wish to use. Mysql is normally on 3306. Also replace user@remote_server with your login info.
ssh -L LOCAL_PORT:127.0.0.1:REMOTE_PORT user@remote_server
While you can forward directly from 3306 to 3306, lets say you already had a local mysql server running on 3306 and wanted to port foward you server's mysql on port 3306 to your localhost at port 3307.
ssh -L 3307:127.0.0.1:3306 user@remote_server
Also if you have ssh running on another port you can specify that with the -p argument
ssh -p 1506 -L 3307:127.0.0.1:3306 user@remote_server
Once you have established the connection, open a new local terminal session and type:
mysql -u YOUR_MYSQL_USER -p -h 127.0.0.1 -P=LOCAL_PORT
Enter the password and you are set to go
Make sure to replace YOUR_MYSQL_USER with your mysql user
account and LOCAL_PORT with the LOCAL_PORT you chose above, say 3306 or 3307.
If you want to have the connection debugged an not end up at the remote terminal you can add these additional arguments
ssh -v -f -N -L 3307:127.0.0.1:3306 user@remote_server
Remember to change the remote ssh port if you need it.
ssh -p 1506 -v -f -N -L 3307:127.0.0.1:3306 user@remote_server
This is the format of the command. You must replace LOCAL_PORT and REMOTE_PORT with the ports you wish to use. Mysql is normally on 3306. Also replace user@remote_server with your login info.
ssh -L LOCAL_PORT:127.0.0.1:REMOTE_PORT user@remote_server
While you can forward directly from 3306 to 3306, lets say you already had a local mysql server running on 3306 and wanted to port foward you server's mysql on port 3306 to your localhost at port 3307.
ssh -L 3307:127.0.0.1:3306 user@remote_server
Also if you have ssh running on another port you can specify that with the -p argument
ssh -p 1506 -L 3307:127.0.0.1:3306 user@remote_server
Once you have established the connection, open a new local terminal session and type:
mysql -u YOUR_MYSQL_USER -p -h 127.0.0.1 -P=LOCAL_PORT
Enter the password and you are set to go
Make sure to replace YOUR_MYSQL_USER with your mysql user
account and LOCAL_PORT with the LOCAL_PORT you chose above, say 3306 or 3307.
If you want to have the connection debugged an not end up at the remote terminal you can add these additional arguments
ssh -v -f -N -L 3307:127.0.0.1:3306 user@remote_server
Remember to change the remote ssh port if you need it.
ssh -p 1506 -v -f -N -L 3307:127.0.0.1:3306 user@remote_server


















