MySQL 如何关闭这个 ssh 隧道?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9447226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:15:48  来源:igfitidea点击:

How to close this ssh tunnel?

mysqlmacosubuntussh

提问by Jacob

I opened a ssh tunnel as described in this post: Zend_Db: How to connect to a MySQL database over SSH tunnel?

我按照这篇文章中的描述打开了一个 ssh 隧道:Zend_Db:如何通过 SSH 隧道连接到 MySQL 数据库?

But now I don't know what I actually did. Does this command affect anything on the server? And how do I close this tunnel, because now I can't use my local mysql properly.

但现在我不知道我到底做了什么。此命令会影响服务器上的任何内容吗?以及如何关闭此隧道,因为现在我无法正确使用本地 mysql。

I use OSX Lion and the server runs on Ubuntu 11.10.

我使用 OSX Lion,服务器在 Ubuntu 11.10 上运行。

回答by simont

Assuming you ran this command: ssh -f [email protected] -L 3306:mysql-server.com:3306 -Nas described in the post you linked.

假设您运行了此命令:ssh -f [email protected] -L 3306:mysql-server.com:3306 -N如您链接的帖子中所述。

A breakdown of the command:

命令分解:

  1. ssh: that's pretty self-explanatory. Invokes ssh.
  2. -f: (From the man sshpage)

    Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background.

    Essentially, send sshto background once you've entered any passwords to establish the connection; it gives the shell prompt back to you at localhostrather than logging you in to remote-host.

  3. [email protected]: the remote server you'd like to log into.
  4. -L 3306:mysql-server.com:3306: This is the interesting bit. -L(from the man sshpage):

    [bind_address:]port:host:hostport Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

    So -L 3306:mysql-server.com:3306binds the localport 3306to the remote port3306on host mysql-server.com.

    When you connect to localport 3306, the connection is forwarded over the secure channel to mysql-server.com. The remote host, mysql-server.comthen connects to mysql-server.comon port 3306.

  5. -N: don't execute a command. This is useful for "just forwarding ports" (quoting the man page).

  1. ssh: 这很不言自明。调用ssh.
  2. -f:(来自man ssh页面)

    在命令执行之前请求 ssh 进入后台。如果 ssh 将要求输入密码或密码短语,但用户希望在后台使用,这将很有用。

    本质上,ssh一旦您输入任何密码以建立连接,就发送到后台;它会将 shell 提示返回给您 atlocalhost而不是将您登录到remote-host.

  3. [email protected]:您要登录的远程服务器。
  4. -L 3306:mysql-server.com:3306: 这是有趣的一点。-L(来自man ssh页面):

    [bind_address:]port:host:hostport 指定将本地(客户端)主机上的给定端口转发到远程端的给定主机和端口。

    因此-L 3306:mysql-server.com:3306本地端口绑定3306到主机上的远程端口3306mysql-server.com

    当您连接到本地端口时3306,该连接将通过安全通道转发到mysql-server.com. 在远程主机上mysql-server.com然后连接到mysql-server.com端口3306

  5. -N: 不要执行命令。这对于“仅转发端口”(引用手册页)很有用。

Does this command affect anything on the server?

此命令会影响服务器上的任何内容吗?

Yes, it establishes a connection between localhostand mysql-server.comon port 3306.

是的,它在端口3306上在localhostmysql-server.com之间建立连接。

And how do I close this tunnel...

我该如何关闭这条隧道...

If you've used -f, you'll notice that the sshprocess you've opened heads into the background. The nicer method of closing it is to run ps aux | grep 3306, find the pidof the ssh -f ... -L 3306:mysql-server.com:3306 -N, and kill <pid>. (Or maybe kill -9 <pid>; I forget if just killworks). That has the beautiful benefit of notkilling all your other sshconnections; if you've got more than one, re-establishing them can be a slight ... pain.

如果您使用过-f,您会注意到ssh您打开的进程进入了后台。关闭它是运行的更好的方法ps aux | grep 3306,找到pidssh -f ... -L 3306:mysql-server.com:3306 -N,和kill <pid>。(或者也许kill -9 <pid>;我忘记了是否kill有效)。这样做的好处是不会杀死所有其他ssh连接;如果你有多个,重新建立它们可能是一种轻微的......痛苦。

... because now I can't use my local mysql properly.

...因为现在我无法正确使用我的本地 mysql。

This is because you've effectively "captured" the localmysqlprocess and forwarded any traffic that attempts to connect to it, off to the remotemysqlprocess. A much nicersolution would be to not use local port 3306in the port-forward. Use something that's not used, like 33060. (Higher numbers are generally less used; it's pretty common to port-forward a combination like this: "2525->25", "8080->80", "33060->3306" or similar. Makes remembering slightly easier).

这是因为您已经有效地“捕获”了本地mysql进程并将尝试连接到它的任何流量转发到远程mysql进程。一个好得多的解决办法是不使用本地端口3306的端口转发。使用一些不使用的东西,比如 33060。(较高的数字通常较少使用;端口转发这样的组合很常见:“2525->25”、“8080->80”、“33060->3306”或类似。让记忆稍微容易一些)。

So, if you used ssh -f [email protected] -L 33060:mysql-server.com:3306 -N, you'd then point your Zend connect-to-mysql function to localhoston port 33060, which would connect to mysql-server.comon port 3306. You can obviously still connect to localhoston port 3306, so you can still use the local mysqlserver.

因此,如果您使用了ssh -f [email protected] -L 33060:mysql-server.com:3306 -N,那么您应该将 Zend connect-to-mysql 函数指向localhoston port 33060,这将连接到mysql-server.comon port 3306。您显然仍然可以连接到localhostport 3306,因此您仍然可以使用本地mysql服务器。

回答by thenetimp

This will kill all ssh sessions that you have open from the terminal.

这将终止您从终端打开的所有 ssh 会话。

sudo killall ssh

回答by aaron

Note: adding as answer since comments don't support code blocks.

注意:添加为答案,因为注释不支持代码块。

In my opinion it is better to NOT use -fand instead just background the process as normal with &. That will give you the exactpid you need to kill:

在我看来,最好不要使用-f,而只是使用&. 这将为您提供您需要杀死的确切pid:

ssh -N -L1234:other:1234 server &
pid=$!
echo "waiting a few seconds to establish tunnel..."
sleep 5
... do yer stuff... launch mysql workbench whatever
echo "killing ssh tunnel $pid"
kill $pid

Or better yet, just create this as a wrapper script:

或者更好的是,只需将其创建为包装脚本:

# backend-tunnel <your cmd line, possibly 'bash'>
ssh -N -L1234:other:1234 server &
pid=$!
echo "waiting a few seconds to establish tunnel..."
sleep 5
"$@"
echo "killing ssh tunnel $pid"
kill $pid

backend-tunnel mysql-workbench

backend-tunnel mysql-workbench

backend-tunnel bash

backend-tunnel bash