If you’re a web developer, you know that SSH is a great way to access your MySQL server from anywhere in the world. You can even use it to remotely manage your database servers! But what if you want to do the same thing with your SSH keys? You can, but there’s a bit of work involved. First, you need to create a new keypair. Second, you need to set up an SSH tunnel. Finally, you need to add the keypair to your ssh configuration.
- Create a new keypair: ssh-keygen -t rsa -b 4096 -C “mysql://localhost/my_database”
- Set up an SSH tunnel: ssh -L 10.0.0.1:8080 my_database@mysql
- Add the keypair to your ssh configuration: 2>&1
- Start your MySQL server: mysql -u root -p
So instead, we’ll just use port-forwarding through an SSH tunnel, so your MySQL client thinks it’s connecting to your localhost machine, but it’s really connecting to the other server through the tunnel.
If you are using the command line ssh, the command would look like this. (You can do the same thing graphically in Putty or SecureCRT options if you need to)
The syntax is ssh -L
If you already have mysql running on your local machine then you can use a different local port for the port-forwarding, and just set your client tools to access MySQL on a different port.
Once you’ve got the ssh tunnel going, you can open up MySQL Query Browser and enter in the details for your remote server, using localhost as the server host, and adjust the port to whatever you used.
Once you get used to this method, you’ll wonder why you ever used phpmyadmin or the command line version.