11

I'm on Mac OS X 10.6.2 trying to connect to ubuntu server 8.04.1 at linode.

ssh -L 127.0.0.1:3306:[[remote ip]]:3306 user@server -N

I want to set up ssh tunneling so that I can access a remote mysql server.

First of all, I'm told bind: Address already in use. This is only after I've tried the command before. How do I manually close a port forwarding session?

Second, when I change the command to be ssh -L 127.0.0.1:3310:[[remote ip]]:3306 user@server -N (I changed the local port to listen on). I'm told channel 1: open failed: connect failed: Connection refused when I try to connect to the MySQL server via MySQL workbench or sequel pro.

To connect through MySQL workbench I use the following settings:

  • host: 127.0.0.1 port: 3310 (if 3306 is in use)
  • username: mysql username
  • password: mysql password
  • database: I don't put anything in
1
  • 1
    Keep in mind that there is a known bug/feature related to some mysql libraries and tools refusing to make a TCP connection when you use localhost. When localhost is used to connect unix socket will be used. bugs.mysql.com/bug.php?id=31577 I am not sure if this applies to Mysql Workbench. Your issue seems to more about getting the connection started though. Commented Apr 2, 2010 at 22:09

3 Answers 3

19

This should work:

ssh -L 3310:127.0.0.1:3306 user@server 

The first port number is the local port to use (must not be in use already), the IP in between the colons is the IP to connect to, from the perspective of the host you are sshing into. The second port number is the port to connect to. If you are connecting to a server other than the one you are sshing into, then try the following:

ssh -L 3310:<remote-IP>:3306 user@server 

Usually, when I want to do this more than one time, I create an entry in ~/.ssh/config like the following where sshhost.example.com is the server I am sshing to, the local port I want to open is 5910, the IP on the other side I want to connect to is 192.168.35.69, and the port I want to connect to there is 5900:

Host desk Hostname sshhost.example.com User jed Port 22 LocalForward 5910 192.168.35.69:5900 

Then from a command prompt I can just do

jed@jed-mbp:~$ ssh desk 

and get a tunnel to my desktop at the office.

Good luck,

--jed

3
  • Thanks for the answer! I can get the forwarding itself to sort of work. After I have the command set up to forward all requests from port 3310 to remote ip 3306 and then try to use mysql workbench...it fails. Commented Apr 2, 2010 at 21:52
  • Is your remote host configured to allow forwarding to other hosts? Does it have a firewall? Is 3310 used on the local system (you can check with netstat -an |grep 3310)? Commented Apr 2, 2010 at 22:15
  • Jonathan, when you specify the host for mysql, don't use 'localhost'. You must specify the full ip addr, 127.0.0.1. For example, once your tunnel is open (like -L 3310:127.0.0.1:3306), then using the mysql client, it would look like: 'mysql -h 127.0.0.1 -P 3310 -u jonathan -p' Commented May 8, 2015 at 4:54
5

Currently on Mac OS Catalina the -N argument is needed to avoid a regular login so the updated command would be:

ssh -N -L 5984:127.0.0.1:5984 [email protected] 
0

After several days playing with this, I was able to find a solution .

My problem was that my client's mysql server only used to accep "localhost" conenctions, and If I wered doing any ssh tunneling localhost was always converted to 127.0.0.1, so the only way to connect with is by mysql sock. I found built my solution similar to this:

/opt/homebrew/bin/sshpass -p SERVER_PASSWORD ssh -TNL 3306:/var/run/mysqld/mysqld.sock user@serverdomain 

In this case I used sshpass, that you can simply install it with : brew install sshpass

the /var/run/mysqld/mysqld.sock part is the location of the remote mysql.sock, so you have to change it for the specific on your remote server.

And then you run this command on a terminal and then you can connect like if you were running mysql on your computer with any mysql clien ( CLI OR GUI )

In my case I was able to connect with Dbeaver.

Hope it helps anyone else, because it was a big nightmare

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.