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

前台的ssh隧道适用于mysql主机localhost,后台的ssh隧道适用于mysql主机127.0.0.1,mysql,ssh,ssh-tunnel,Mysql,Ssh,Ssh Tunnel,我在这里尝试的是将所有连接转发到端口3306上机器1上的localhost到端口3306上本地主机上的机器2。因此,如果您在机器1上连接到mysql,它的行为就像您在机器2上连接一样 我认为ssh隧道应该转发特定端口上的通信量,而不是让我登录到另一台机器上。(就像这里一样)我尝试过在“@machine-two-hostname.com”之前不使用“admin”,这也会做同样的事情。正如标题所说的,在后台运行它不允许我在“localhost”上连接,当我尝试在“bind address ready

我在这里尝试的是将所有连接转发到端口3306上机器1上的localhost到端口3306上本地主机上的机器2。因此,如果您在机器1上连接到mysql,它的行为就像您在机器2上连接一样

我认为ssh隧道应该转发特定端口上的通信量,而不是让我登录到另一台机器上。(就像这里一样)我尝试过在“@machine-two-hostname.com”之前不使用“admin”,这也会做同样的事情。正如标题所说的,在后台运行它不允许我在“localhost”上连接,当我尝试在“bind address ready in use”的相同端口上设置另一个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")
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)]> 
在后台运行时更新mysql连接在我使用127.0.0.1而不是localhost时工作,为什么

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)]> 
在这里找到了答案:

“在Unix上,MySQL程序特别对待主机名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文档:

因此,在我在后台设置隧道后,这项工作就开始了:

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")
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隧道真的按照它应该的方式工作,它应该让您登录到另一台机器吗