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

ttvkxqim  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(379)

我在这里要做的是将所有连接转发到端口3306上的机器1上的localhost到端口3306上的机器2上的localhost。因此,如果您在机器1上连接到mysql,它的行为就像您在机器2上连接一样。
我以为ssh隧道应该转发特定端口上的通信量,而不是让我登录到另一台机器上(我尝试过在“@machine-two-hostname.com”之前不使用“admin”,这样做也是一样的。正如标题所说,在后台运行此命令不允许我在“localhost”上连接,但当我尝试在“bind address already 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")

在后台运行时更新当我使用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)]>
5tmbdcev

5tmbdcev1#

在这里找到答案:当使用ssh tunel时,无法通过套接字错误连接到本地mysql服务器
“在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隧道在前台实际上是工作的方式,它应该登录到另一台机器你?

相关问题