title: MySQL connexion over SSH
body: <p>I'm getting an error trying to connect to a distant MySQL server from my local MySQL client.
  (server is running Debian and client runs OSX)</p>
  
  <p>I can manage to ssh to the server ...</p>
  
  <pre><code>local$ ssh john@serverip
  </code></pre>
  
  <p>(note: i'm using ssh public key so i don't enter any password)
  ... and from there connecting to MySQL ...</p>
  
  <pre><code>distant$ mysql -u sqluser -p myDatabase
  </code></pre>
  
  <p>(ok i'm connected, i can do any MySQL command i want)</p>
  
  <p>Below is how my client config looks like :
  <img src="https://i.stack.imgur.com/J496S.jpg" alt="enter image description here"></p>
  
  <p>Below are the debugging trace of my client when trying to connect over ssh:</p>
  
  <pre><code>Used command:  /usr/bin/ssh -v -N -o ControlMaster=no -o ExitOnForwardFailure=yes -o ConnectTimeout=10 -o NumberOfPasswordPrompts=3 -o TCPKeepAlive=no -o ServerAliveInterval=60 -o ServerAliveCountMax=1 john@serverip -L 52004/127.0.0.1/3306
  
  OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
  debug1: Reading configuration data /etc/ssh_config
  debug1: /etc/ssh_config line 20: Applying options for *
  debug1: /etc/ssh_config line 102: Applying options for *
  debug1: Connecting to serverip [serverip] port 22.
  debug1: fd 3 clearing O_NONBLOCK
  debug1: Connection established.
  debug1: identity file /Users/john/.ssh/id_rsa type 1
  debug1: identity file /Users/john/.ssh/id_rsa-cert type -1
  debug1: identity file /Users/john/.ssh/id_dsa type -1
  debug1: identity file /Users/john/.ssh/id_dsa-cert type -1
  debug1: Enabling compatibility mode for protocol 2.0
  debug1: Local version string SSH-2.0-OpenSSH_6.2
  debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4+deb7u2
  debug1: match: OpenSSH_6.0p1 Debian-4+deb7u2 pat OpenSSH*
  debug1: SSH2_MSG_KEXINIT sent
  debug1: SSH2_MSG_KEXINIT received
  debug1: kex: server-&gt;client aes128-ctr hmac-md5 none
  debug1: kex: client-&gt;server aes128-ctr hmac-md5 none
  debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024&lt;1024&lt;8192) sent
  debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
  debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
  debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
  debug1: Server host key: RSA 4b:56:4e:f2:71:50:ed:fd:25:aa:bf:0d:1a:a0:89:40
  debug1: Host 'serverip' is known and matches the RSA host key.
  debug1: Found key in /Users/john/.ssh/known_hosts:1
  debug1: ssh_rsa_verify: signature correct
  debug1: SSH2_MSG_NEWKEYS sent
  debug1: expecting SSH2_MSG_NEWKEYS
  debug1: SSH2_MSG_NEWKEYS received
  debug1: Roaming not allowed by server
  debug1: SSH2_MSG_SERVICE_REQUEST sent
  debug1: SSH2_MSG_SERVICE_ACCEPT received
  debug1: Authentications that can continue: publickey
  debug1: Next authentication method: publickey
  debug1: Offering RSA public key: /Users/john/.ssh/id_rsa
  debug1: Server accepts key: pkalg ssh-rsa blen 279
  debug1: read PEM private key done: type RSA
  debug1: Authentication succeeded (publickey).
  Authenticated to serverip ([serverip]:22).
  debug1: Local connections to LOCALHOST:52004 forwarded to remote address 127.0.0.1:3306
  debug1: Local forwarding listening on ::1 port 52004.
  debug1: channel 0: new [port listener]
  debug1: Local forwarding listening on 127.0.0.1 port 52004.
  debug1: channel 1: new [port listener]
  debug1: Requesting no-more-sessions@openssh.com
  debug1: Entering interactive session.
  debug1: Connection to port 52004 forwarding to 127.0.0.1 port 3306 requested.
  debug1: channel 2: new [direct-tcpip]
  channel 2: open failed: connect failed: Connection refused
  debug1: channel 2: free: direct-tcpip: listening port 52004 for 127.0.0.1 port 3306, connect from 127.0.0.1 port 52006, nchannels 3
  </code></pre>
  
  <p>Any idea what's going wrong ?</p>
  
  <p>(note: i replaced my real server ip with serverip, my real sql username with sqluser and my real ssh username with john)</p>
  
  <p>Edit:
  Typing this on the server:</p>
  
  <pre><code>netstat -an | grep 3306
  </code></pre>
  
  <p>Gives :</p>
  
  <pre><code>tcp        0      0 serverip:3306       0.0.0.0:*               LISTEN     
  </code></pre>
  
  <p>Edit : Solution to my problem was to change "127.0.0.1" to serverip in my client config options. I thought in the first place that serverip would only be used for the SSH server, and then MySQL Server would be 127.0.0.1 (localhost), but i was wrong. Many thanks to @Honore Doktorr and @Kenster for pointing me the right solution</p>