通过 SSH 隧道的 MySQL 连接 - 如何指定其他 MySQL 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18373366/
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
MySQL connection over SSH tunnel - how to specify other MySQL server?
提问by Mad Marvin
I've got ~50 databases all set up at different host names, with the requirement that I connect to them through an SSH tunnel.
我有大约 50 个数据库都设置在不同的主机名上,要求我通过 SSH 隧道连接到它们。
For example:
例如:
SSH host at
ssh.example.com
MySQL host at
mysql1.example.com
SSH 主机在
ssh.example.com
MySQL 主机位于
mysql1.example.com
I have managed to create the tunnel using autossh (web server running Debian), but I can't seem to figure out how to connect to a specific MySQL hostname "beyond" the SSH tunnel.
我已经设法使用 autossh(运行 Debian 的 Web 服务器)创建了隧道,但我似乎无法弄清楚如何连接到“超出”SSH 隧道的特定 MySQL 主机名。
Typing lsof -i -n | egrep '\<ssh\>'
confirms that the tunnel is working (sends port 3307 to ssh.example.com port 3306)
键入lsof -i -n | egrep '\<ssh\>'
确认隧道正在工作(将端口 3307 发送到 ssh.example.com 端口 3306)
So when I try mysql -h 127.0.0.1 -P 3307
I get Connection refused. Not too weird since it's not an MySQL server.
因此,当我尝试时,mysql -h 127.0.0.1 -P 3307
连接被拒绝。不太奇怪,因为它不是 MySQL 服务器。
My question to you guys:
我想问大家的问题:
How do I specify the mysql1.example.com
host AFTER creating the SSH tunnel? I've tried searching everywhere but can't seem to figure it out.
mysql1.example.com
创建 SSH 隧道后如何指定主机?我试过到处搜索,但似乎无法弄清楚。
回答by Mad Marvin
Solved it! The thing was to connect to the correct server when creating the tunnel itself - should've seen that one coming.
解决了!事情是在创建隧道本身时连接到正确的服务器 - 应该已经看到了。
ssh -f [email protected] -L 3307:mysql1.example.com:3306 -N
ssh -f [email protected] -L 3307:mysql1.example.com:3306 -N
Then mysql -h 127.0.0.1 -P 3307
worked as intended. :)
然后mysql -h 127.0.0.1 -P 3307
按预期工作。:)
回答by sandra kevin
I got a nice blog about how to connect to MySQL using SSH tunnel. It is very well explained here.
我有一篇关于如何使用 SSH 隧道连接到 MySQL 的不错的博客。这很好解释here。
The command to connect to SSH tunnel:
连接SSH隧道的命令:
ssh -L [listening port]:[REMOTE_MYSQL_HOST]:[REMOTE_MYSQL_PORT] [SSH_USER]@[SSH_HOST]
The command to connect to MySQL:
连接MySQL的命令:
localhost:[listening port]
回答by ketankk
When you don't have direct access to mysql-server
, you use jump-server
.
当您无法直接访问 时mysql-server
,请使用jump-server
。
From your machine, you connect(ssh) to jump-server and from there you connect to your mysql-server
.
从你的机器上,你连接(ssh)到跳转服务器,然后你从那里连接到你的mysql-server
.
This can be avoided by using ssh- tunneling
.
这可以通过使用来避免ssh- tunneling
。
Suppose your
假设你的
jump server is `jump-ip`
mysql server is `mysql-ip`
your machine is `machine-ip`
Just open ssh client(Putty in windows or terminal in linux/ios).
只需打开 ssh 客户端(Windows 中的 Putty 或 linux/ios 中的终端)。
Type:
类型:
ssh -L [local-port]:[mysql-ip]:[mysql-port] [jump-server-user]@[jump-ip]
After this, you can use your localhost
and local-port
to access mysql-server on the remote machine directly.
在此之后,您可以使用您的localhost
和local-port
直接访问远程机器上的 mysql-server。
Eg. Your Jdbc url to access mysql database, in that case, will be
例如。在这种情况下,您访问 mysql 数据库的 Jdbc url 将是
jdbc:mysql://localhost:[local-port]/[database-name]
For Windows Users, Using Putty to connect to remote MySQL Server via Tunneling
对于 Windows 用户,使用 Putty 通过隧道连接到远程 MySQL 服务器
Step1: Enter your Jump server Host/IP in the session tab
步骤 1:在会话选项卡中输入您的 Jump 服务器主机/IP
Step2: Go to SSH
tab--> Tunnels
: Enter Your MySQL server HostName: Port as destination and Source port as your local port where you want to tunnel that service and click on Add
步骤 2:转到SSH
选项卡--> Tunnels
:输入您的 MySQL 服务器主机名:端口作为目标,源端口作为您想要隧道该服务的本地端口,然后单击Add
Step3: Go back to Session tab and click on Open
and Enter your credentials, if it is Username/Password based.
步骤 3:返回会话选项卡并单击Open
并输入您的凭据,如果它是基于用户名/密码的。
And use same credential as mentioned above:
并使用与上述相同的凭据:
jdbc:mysql://localhost:[local-port]/[database-name]
回答by Javeed Shakeel
I created a tunnelto the DB using this command
我使用此命令创建了一条通往数据库的隧道
ssh -L 10000:localhost:3306 user@<ip addess to connect DB> -N -f
-L is local host port it is user defined u can provide any port number
-L 是本地主机端口它是用户定义的你可以提供任何端口号
between 0 to 65535. 0 to 1023 are reserved.
0 到 65535 之间。0 到 1023 是保留的。
whether you are using key based authentication to the server you should
无论您是否对服务器使用基于密钥的身份验证,您都应该
mention the key like this.
像这样提到钥匙。
ssh -i <path of the private key> -L 10000:localhost:3306 user@<ip addess to connect DB> -N -f