【发布时间】:2015-05-06 17:44:31
【问题描述】:
我在尝试从本地 MySQL 客户端连接到远程 MySQL 服务器时遇到错误。 (服务器运行 Debian,客户端运行 OSX)
我可以通过 ssh 连接到服务器 ...
local$ ssh john@serverip
(注意:我使用的是 ssh 公钥,所以我不输入任何密码) ...然后从那里连接到 MySQL ...
distant$ mysql -u sqluser -p myDatabase
(好的,我已连接,我可以执行任何我想要的 MySQL 命令)
下面是我的客户端配置的样子:
以下是我的客户端尝试通过 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 Server 将是 127.0.0.1 (localhost),但我错了。非常感谢@Honore Doktorr 和@Kenster 为我指出了正确的解决方案
【问题讨论】:
-
listening port 52004 for 127.0.0.1 port 3306, connect from 127.0.0.1 port 52006让我觉得你的 mysql 客户端的“Hôte MySql”可能需要设置为 serverip,而不是 127.0.0.1。 -
你可以试试
distant$ mysql -u sqluser -p myDatabase -h 127.0.0.1并告诉我它是否连接? -
@Oz Solomon:命令
distant$ mysql -u sqluser -p myDatabase -h 127.0.0.1导致“ERROR 2003 (HY000) Can't connect to MySQL server on '127.0.0.1' (111)” 但distant$ mysql -u sqluser -p myDatabase -h localhost运行良好. -
@HonoreDoktorr :您实际上是第一个给我正确解决方案的人。用 serverip 替换“127.0.0.1”是要走的路!非常感谢你帮助我!我很高兴这个远程连接问题现在正在困扰我!
标签: mysql ssh connection