gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:26:14 27 4
gpt4 key购买 nike

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

我认为 ssh 隧道应该转发特定端口上的流量,而不是像将我登录到另一台机器上一样。 (就像这里所做的那样)我尝试过在“@machine-two-hostname.com”之前没有“admin”,并且做了同样的事情。正如标题所说,在后台运行它不会让我连接到“localhost”,当我尝试在“绑定(bind)地址已在使用”的相同端口上设置另一个 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)]>

最佳答案

在这里找到答案: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 选项显式指定连接协议(protocol),即使是 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 隧道是否确实按应有的方式工作,是否应该让您登录到另一台计算机?

关于mysql - 前台的 ssh 隧道适用于 mysql 主机 localhost,但后台的 ssh 隧道适用于 mysql 主机 127.0.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com