Introduction
Database is one of the must-have items for today’s web application projects. I believe there are plenty of project teams who use MySQL, and I’m one of them. During the development process, there might be a case where the team members want to share a single database instance rather than keeping separate copies for each of members. However, in this case, security becomes our primary concern as MySQL does not provide a secure connection to remote clients.
(Still working on this part…)
In my opinion, SSH is such an awesome protocol. I can access to any remote computers from anywhere in this world, securely transfer files, and so on and so forth. In this article, I’ll make a brief explanation on how to setup a SSH tunneling for secure connection to a remote MySQL server.
If you’re not sure what ‘tunneling’ means, you might want to refer this article.
HOW-TO
ssh -L $LOCAL_PORT:localhost:3306 $REMOTE_SERVER
mysql -u $USER -p -h 127.0.0.1
Example
ssh -L 3306:localhost:3306 db.sumin.us
Then now I can connect to
mysql -u sumin -p -h 127.0.0.1
No localhost, but 127.0.0.1
If you tried to connect to localhost, then you probably got this kind of message.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/opt/local/var/run/mysql5/mysqld.sock' (2)
My perception is that when you connect to localhost then the MySQL client tries to establish an inter process communication through mysql.sock file.
Further Applications
You can use SSH tunneling for pretty much anything you want. AFP, SMB, or FTP. You name it.