【问题标题】:ssh tunnel in foreground works for mysql host localhost but ssh-tunnel in background works for mysql host 127.0.0.1前台的 ssh 隧道适用于 mysql 主机 localhost,但后台的 ssh 隧道适用于 mysql 主机 127.0.0.1
【发布时间】:2018-06-21 08:04:05
【问题描述】:

我在这里尝试做的是将端口 3306 上机器一上的所有连接转发到端口 3306 上本地主机上的机器二。因此,如果您连接到机器一上的 mysql,它就像您正在连接在第二台机器上。

我认为 ssh 隧道应该转发特定端口上的流量,而不是像将我登录到另一台机器一样。 (就像它在这里做的那样)我试过在“@machine-two-hostname.com”之前没有“admin”,并且做同样的事情。正如标题所说,在后台运行它不会让我在“localhost”上连接,当我尝试在“绑定地址已在使用”的相同端口上设置另一个 ssh 隧道时,它确实给了我一条新消息。怀疑我在下面运行的命令也不起作用,但它只是将我登录到另一台机器而不连接端口。

admin@machine-one:~$ ssh -L 3306:localhost:3306 admin@machine-two-hostname.com
Linux machine-two 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/\*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Jun 20 11:16:07 2018 from 172.31.93.22
admin@machine-two:~$ mysql -uroot -proot-pass
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \q
Bye
admin@machine-two:~$ exit
logout
Connection to machine-two-hostname.com closed.
admin@machine-one:~$ ssh -fN -L 3306:localhost:3306 admin@machine-two-hostname.com
admin@machine-one:~$ mysql -uroot -proot-pass
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

在后台运行:

admin@machine-one:~$ ssh -fN -L 3306:localhost:3306 machine-two-hostname.com
admin@machine-one:~$ mysql -uroot -proot-pass -hlocalhost
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

在后台运行时更新,当我使用 127.0.0.1 而不是 localhost 时,mysql 连接有效,为什么?

admin@machine-one:~$ mysql -uroot -proot-pass -hlocalhost
ERROR 2002 (HY000): Cant connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
admin@machine-one:~$ mysql -uroot -proot-pass -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

【问题讨论】:

    标签: mysql ssh ssh-tunnel


    【解决方案1】:

    在这里找到答案:Can't connect to local MySQL server through socket error when using SSH tunel

    “在 Unix 上,MySQL 程序对主机名 localhost 进行特殊处理,与其他基于网络的程序相比,这种方式可能与您所期望的不同。对于到 localhost 的连接,MySQL 程序尝试通过以下方式连接到本地服务器使用 Unix 套接字文件。即使给出了 --port 或 -P 选项来指定端口号,也会发生这种情况。为了确保客户端与本地服务器建立 TCP/IP 连接,请使用 --host 或 -h 来指定主机名值 127.0.0.1,或本地服务器的 IP 地址或名称。您还可以使用 --protocol=TCP 选项显式指定连接协议,即使对于 localhost。例如:

    shell> mysql --host=127.0.0.1
    shell> mysql --protocol=TCP
    

    来自 mysql 文档:https://dev.mysql.com/doc/refman/8.0/en/connecting.html

    所以这在我在后台设置隧道后有效:

    admin@machine-one:~$ mysql -uroot -proot-pass --protocol=TCP -hlocalhost
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 23
    Server version: 10.1.26-MariaDB-0+deb9u1 Debian 9.1
    
    Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> 
    

    但是我想知道我在前台的 ssh 隧道是否真的按照它应该的方式工作,它应该让你登录到另一台机器吗?

    【讨论】:

      猜你喜欢
      • 2018-11-30
      • 2018-07-20
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多