通过SSH连接MySQL

时间:2015-05-06 17:44:32

标签: mysql ssh connection

我尝试从本地MySQL客户端连接到远程MySQL服务器时遇到错误。 (服务器正在运行Debian,客户端运行OSX)

我可以设法ssh到服务器......

local$ ssh john@serverip

(注意:我正在使用ssh公钥,所以我不输入任何密码) ......并从那里连接到MySQL ...

distant$ mysql -u sqluser -p myDatabase

(好吧我已经连接,我可以做任何我想要的MySQL命令)

以下是我的客户端配置的样子: enter image description here

以下是尝试通过ssh连接时客户端的调试跟踪:

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->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<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

知道出了什么问题吗?

(注意:我用serverip替换了我的真实服务器ip,用sqluser替换了真正的sql用户名,用john替换了真正的ssh用户名)

编辑: 在服务器上输入:

netstat -an | grep 3306

给予:

tcp        0      0 serverip:3306       0.0.0.0:*               LISTEN     

编辑:我的问题的解决方案是在我的客户端配置选项中将“127.0.0.1”更改为serverip。我认为首先,serverip只用于SSH服务器,然后MySQL服务器将是127.0.0.1(localhost),但我错了。非常感谢@Honore Doktorr和@Kenster为我指出了正确的解决方案

2 个答案:

答案 0 :(得分:1)

  debug1:连接到端口52004转发到请求的127.0.0.1端口3306.&#xA; debug1:channel 2:new [direct-tcpip]&#xA; channel 2:open failed:connect failed:Connection拒绝&#xA;  
&#xA;&#xA;

远程ssh服务器尝试连接到127.0.0.1端口3306以便为转发请求提供服务,但它得到了一个“连接”拒绝“错误。 “拒绝连接”通常意味着没有任何东西在请求的地址和端口上监听连接。

&#xA;&#xA;

最简单的解释是应该运行的mysql服务器远程服务器实际上并没有运行,或者它正在侦听与您期望的不同的地址和/或端口。

&#xA;&#xA;

您应该检查远程服务器以确保那个mysql实际上正在运行。如果是,则找到它实际侦听的地址和端口,并更正端口转发设置以转发到正确的地址和端口。

&#xA;&#xA;

编辑:

&#xA;&#xA;
  tcp 0 0 serverip:3306 0.0.0.0:* LISTEN&#xA;  
&#xA ;&#xA;

这表明mysql进程专门侦听serverip接口,而不是localhost接口。它只接收针对serverip地址的连接尝试,而不是127.0.0.1。

&#xA;&#xA;

HôteMySql字段的值更改为serverip地址(即netstate输出中出现的任何内容)可能会解决问题。或者,可以重新配置mysql服务器以侦听127.0.0.1地址,或者侦听0.0.0.0(接受任何接口上的连接的“通配符”地址)。

&#xA;

答案 1 :(得分:0)

这可能是一些问题之一:

  1. MySQL正在侦听UNIX套接字(通过localhost)但不是TCP套接字。典型的原因是在skip-networking中使用my.cnf配置选项。您可以通过运行SQL命令SHOW VARIABLES LIKE 'skip_networking';

  2. 来查看是否为您启用了此选项
  3. 您在my.cnf中更改了MySQL正在侦听的端口。要查看它正在侦听的端口(如果有的话),请运行sudo netstat -lnp | grep mysql并查看列出的端口。默认端口为3306

  4. 这种情况可能发生的另一个原因是,防火墙阻止了对端口的访问。我不认为这可能是因为您尝试连接到127.0.0.1,而这通常不会被防火墙阻止。如果上面的netstat显示3306作为监听端口,那么这可能是您的问题。