MySQL connection via SSH tunnel

big surMySQLsshterminalvpn

I've been trying to set up a SSH tunnel with VPN on my macbook with Big Sur 11.2, but it doesn't seem to work.

On my linux machine, I can simply turn on the VPN and make a SSH-tunnel. Then I can just connect to the MySQL server via port 3307. If I do the same on my macbook, the SSH tunnel does connect, but I can't connect to the MySQL server on the given port.

My exact steps are:

  1. Turn on VPN so I can access the server via SSH.
  2. Run ssh -g -L 3307:127.0.0.1:3306 user@ip_address in the terminal.
  3. Run mysql -u user -p -h [IP] -P 3307 to connect to the MySQL SSH tunnel.
  4. Error: Can't connect to MySQL server on '[IP]'.

Above works fine on my linux system, but not on my mac. I am able to SSH to the server with the command, but the tunnel itself is not working.

Is there some reason this is happening, and how should I proceed?

Best Answer

It appears you have swapped the local address and the remote server address around. For example this site explains it should be:

  1. Create an SSH tunnel from you client to the server:
    ssh -g -L 3307:server-ip:3306 user@server-ip
    Now port 3306 of the server is exposed on 3307 on the client.
  2. On the client connect MySQL to the 3307 port: mysql -u user -p -h 127.0.01 -P 3307
    Note that you have to connect to the local loopback address 127.0.0.1 as the SSH tunnel already forwarded the connection to the client.