【问题标题】:MySQL connexion over SSHMySQL 通过 SSH 连接
【发布时间】: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


【解决方案1】:
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

远程 ssh 服务器尝试连接到 127.0.0.1 端口 3306 以服务转发请求,但收到“连接被拒绝”错误。 “连接被拒绝”通常意味着在请求的地址和端口上没有任何连接监听。

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

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

编辑:

tcp        0      0 serverip:3306       0.0.0.0:*               LISTEN     

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

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

【讨论】:

  • 感谢您的回答。我只是在服务器(serverip)上再次 ssh,我可以通过命令行连接到 MySQL 服务器:它已启动并正在运行(在端口 3306 上)。示例:运行此请求:show variables where Variable_name like '%port%'; 给我结果:port | 3306
  • 在 mysql 服务器上,您可以运行netstat -an | grep 3306 并编辑您的问题以添加命令的输出吗?
  • 解决了用 serverip 替换 "127.0.0.1" 的问题!哇,这真是一种解脱!今晚我会睡得很香。我非常感谢帮助我解决这个连接问题。非常感谢 ! sudo vi /etc/mysql/my.cnf 状态bind-address = server ip
【解决方案2】:

这可能是几个问题之一:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 2021-08-07
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 2012-06-19
    • 2014-11-24
    • 2012-08-15
    • 2014-02-18
    相关资源
    最近更新 更多