Step 1: SSH forwarding
First, forward the local port 3307 to 3306. That is, when you access the local port 3307, it will redirect it to port 3306 on the remote host.
ssh -fNg4 -L 3307:127.0.0.1:3306 user@hostname
-f sends SSH to the background
-g allows remote hosts to connect to local forwarded ports
-N don’t execute a remote command
-4 this was key! Forces IPv4. Kept getting “bind: Address already in use” errors because I didn’t have this.
-L the forwarding magic happens here . . . syntax is localport:localhost:remoteport
Step 2: Connect to mysql on port 3307
. . . which will redirect to port 3306 on remote host.
mysql -u root -h 127.0.0.1 -P 3307 -p
and you’re in!