前台的 ssh 隧道适用于 mysql 主机 localhost,但后台的 ssh-tunnel 适用于 mysql 主机 127.0.0.1 [英] ssh tunnel in foreground works for mysql host localhost but ssh-tunnel in background works for mysql host 127.0.0.1

查看:31
本文介绍了前台的 ssh 隧道适用于 mysql 主机 localhost,但后台的 ssh-tunnel 适用于 mysql 主机 127.0.0.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What I am trying to do here is forward all connections to localhost on machine-one on port 3306 to machine-two on localhost on port 3306. So if you are connecting to mysql on machine-one it acts like you are connecting on machine two.

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

I thought an ssh tunnel was supposed to forward the traffic on a specific port not act like it is logging me into the other machine. (like it is doing here) I have tried without the "admin" before "@machine-two-hostname.com" and that does the same thing. As the title says running this in the background doesn't let me connect on "localhost" it does give me a new message when I try to set up another ssh tunnel on the same ports that "bind address already in use" I'm suspicious that the commands I am running below aren't working either but that it is just logging me into the other machine and not connecting the ports.

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")

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

UPDATE when running in the background the mysql connection works when I use 127.0.0.1 instead of localhost, why?

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)]> 

推荐答案

在这里找到答案:使用SSH tunel时无法通过socket连接到本地MySQL服务器

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

"On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:

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

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

from mysql documentation: https://dev.mysql.com/doc/refman/8.0/en/connecting.html

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

So this works after I set up the tunnel in the background:

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

I would like to know however if my ssh tunnel in the foreground is actually working the way it should be, is it supposed to log you into the other machine?

这篇关于前台的 ssh 隧道适用于 mysql 主机 localhost,但后台的 ssh-tunnel 适用于 mysql 主机 127.0.0.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆