Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to connect to a remote MySQL from my host, but the host is behind a ssh proxy, like this.

I have 3 hosts in this problem

  • A: my local machine (that can ssh to B)
  • B: Intermediate machine that can SSH to C
  • C: a remote server running MySQL and allows only connections from localhost (grant)

I use ~/.ssh/config to allow me to ssh directly from A to C (using B as proxy).

Host B
    ProtocolKeepAlives 30
    HostName hostnameofB

Host C
    ProtocolKeepAlives 30
    ProxyCommand ssh -q B nc -q0 hostnameofC 22
    LocalForward 3336 localhost:3306
    Port 21343

I open a ssh from A to C with -vvv and see this:

debug1: Local connections to LOCALHOST:3336 forwarded to remote address localhost:3306

I then try to login to MySQL from A:

mysql -uMyUsername -pMyPassword  DatabaseName -P3336 -h127.0.0.1
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

(if I try to use -hlocalhost it will try to log in to my local MySQL server, even if I use different ports)

in my SSH -vvv window I also see this:

client-session (t4 r0 i0/0 o0/0 fd 7/8 cc -1)

there is no logs on the C MySQL server.

any good suggestions?

share|improve this question
Where is the port forwarding from A to B? – Ram Jul 9 '12 at 17:15
A and B are on the same network trough a VPN, no port-forwarding. – Sverre Jul 11 '12 at 6:55

Looks to me like your mysql command run on host A attempts to connect to port 3366 on A; I don't see anything forwarding A:3366 anywhere. Try adding a forwarding rule from A to B, or if there is no filtering done you can simply change your mysql commend to hit host B rather than "-h127.0.0.1" which refers to A.

share|improve this answer
I though the line: LocalForward 3336 localhost:3306 did this for me, I did some more testing, and tried to do what I wanted to do, with only one jumb (A to B), host B hostName hostnameofB LocalForward 3336 localhost:3306 then I open a SSH to B, and run this SQL mysql -umyusername -pmypassword mydatabasename -hlocalhost --protocol=TCP -P3336 and this works fine. – Sverre Jul 12 '12 at 3:55
I also tried to setup intermediate forwarding on B, with LocalForward 3333 localhost:3336 on B, and LocalForward 3336 localhost:3306 on C but that also did not work. – Sverre Jul 12 '12 at 3:59
what is the -q0 in the proxycommand 'nc' call? – Ram Jul 12 '12 at 19:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.