前台的 ssh 隧道适用于 mysql host localhost 但 ssh-tunnel 在后台适用于 mysql host 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
我在这里要做的是将所有连接到本地主机上 machine-one 端口 3306 上的所有连接转发到本地主机上端口 3306 上的 machine-two。所以如果你连接到 mysql在 machine-one 上,它就像您正在连接第二台机器一样。
我认为 ssh 隧道应该转发特定端口上的流量,而不是让我登录到另一台机器。 (就像它在这里做的那样)我试过在“@machine-two-hostname.com”之前没有 "admin" 并且做同样的事情。正如标题所说 运行 这在后台不允许我连接 "localhost" 当我尝试在与 [=26= 相同的端口上设置另一个 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)]>
在这里找到答案:
“在 Unix 上,MySQL 程序会特别对待主机名 localhost,与其他基于网络的程序相比,其处理方式可能与您所期望的不同。对于与 localhost 的连接,MySQL 程序尝试使用 Unix 套接字文件连接到本地服务器。即使给出了 --port 或 -P 选项来指定端口号,也会发生这种情况。确保客户端建立 TCP/IP 连接本地服务器,使用 --host 或 -h 指定主机名值 127.0.0.1,或本地服务器的 IP 地址或名称。您还可以明确指定连接协议,即使对于 localhost,也可以使用 - -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 隧道是否真的按照它应该的方式工作,它应该让你登录到另一台机器吗?
我在这里要做的是将所有连接到本地主机上 machine-one 端口 3306 上的所有连接转发到本地主机上端口 3306 上的 machine-two。所以如果你连接到 mysql在 machine-one 上,它就像您正在连接第二台机器一样。
我认为 ssh 隧道应该转发特定端口上的流量,而不是让我登录到另一台机器。 (就像它在这里做的那样)我试过在“@machine-two-hostname.com”之前没有 "admin" 并且做同样的事情。正如标题所说 运行 这在后台不允许我连接 "localhost" 当我尝试在与 [=26= 相同的端口上设置另一个 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)]>
在这里找到答案:
“在 Unix 上,MySQL 程序会特别对待主机名 localhost,与其他基于网络的程序相比,其处理方式可能与您所期望的不同。对于与 localhost 的连接,MySQL 程序尝试使用 Unix 套接字文件连接到本地服务器。即使给出了 --port 或 -P 选项来指定端口号,也会发生这种情况。确保客户端建立 TCP/IP 连接本地服务器,使用 --host 或 -h 指定主机名值 127.0.0.1,或本地服务器的 IP 地址或名称。您还可以明确指定连接协议,即使对于 localhost,也可以使用 - -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 隧道是否真的按照它应该的方式工作,它应该让你登录到另一台机器吗?