前台的ssh隧道适用于mysql主机localhost,但后台的ssh隧道适用于mysql主机127.0.0.1

时间:2018-06-20 12:19:24

标签: mysql ssh ssh-tunnel

我在这里想要做的是将所有连接转发到端口3306上一号机上的localhost到端口3306上的localhost二号机上的第二个。在第二台机器上。

我认为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而不是本地主机时,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)]> 

1 个答案:

答案 0 :(得分:0)

在这里找到答案: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选项显式指定连接协议,即使是本地主机也是如此。 >

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隧道是否正在按应有的方式工作,是否应该将您登录到另一台计算机?