简体   繁体   English

使用 SSHTunnelForwarder 通过 SSH 连接到 MySQL 数据库

[英]Using SSHTunnelForwarder to connect to a MySQL db via SSH

I am trying to connect to a mysql db over ssh in python, but am getting an error.我正在尝试通过 python 中的 ssh 连接到 mysql 数据库,但出现错误。 I have the following saved in a python file 'forward.py'.我将以下内容保存在 python 文件“forward.py”中。 My code is as follows:我的代码如下:

forward.py转发.py

from sshtunnel import SSHTunnelForwarder
import MySQLdb

with SSHTunnelForwarder(
      ('ssh_host', 22),
      ssh_password="ssh_password",
      ssh_username="ssh_username",
      remote_bind_address=('mysql_host', 3306)) as server:

     con = MySQLdb.connect(user='mysql_username',passwd='mysql_password',db='mysql_db_name',host='mysql_host',port=server.local_bind_port)

When I run forward.py in terminal, I receive the following error:当我在终端中运行 forward.py 时,我收到以下错误:

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on     
'mysql_host' (60)")

It is worth noting that the values are hardcoded values for security, ie 'mysql_host' is an actual host that I can ssh to just fine when I run值得注意的是,这些值是为了安全起见的硬编码值,即 'mysql_host' 是一个实际的主机,我可以在运行时通过 ssh 正常运行

ssh mysql_host

via terminal.通过终端。 I can also connect just fine using Sequel Pro with the same values, as per: http://screencast.com/t/0niuWlMDb我也可以使用具有相同值的 Sequel Pro 连接得很好,如: http : //screencast.com/t/0niuWlMDb

Can anyone point me in the right direction?任何人都可以指出我正确的方向吗? Thanks in advance提前致谢

I think your issue lies in the fact that you're creating a local SSH tunnel but attempting to connect directly to a remote host, which defeats the purpose of using a SSH tunnel.我认为您的问题在于您正在创建本地 SSH 隧道,但尝试直接连接到远程主机,这违背了使用 SSH 隧道的目的。

I believe if you change your connection string from host='mysql_host' to host='127.0.0.1' you will have solved your issue.我相信如果您将连接字符串从host='mysql_host'更改为host='127.0.0.1'您将解决您的问题。

If you look at the debugging / connection settings of your Sequel Pro instance you will see the actual connection commands being used and should hopefully confirm that this is the case.如果您查看 Sequel Pro 实例的调试/连接设置,您将看到正在使用的实际连接命令,并希望确认情况确实如此。

Further reference material on SSH forwarding : http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html有关 SSH 转发的更多参考资料: http : //blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html

If you're using SSH tunnel forwarding you need to connect to the 'localhost:3306' instead of your actual 'mysql-host:3306'.如果您使用 SSH 隧道转发,则需要连接到“localhost:3306”而不是实际的“mysql-host:3306”。

Let me explain what happened:让我解释一下发生了什么:

  1. You establish connection with SSH to the remote mysql host您通过 SSH 建立到远程 mysql 主机的连接
  2. Your client setup tunnel and listen for TCP:3306 on your local host: localhost:3306您的客户端设置隧道并在本地主机上侦听 TCP:3306:localhost:3306
  3. Any traffic to localhost:3306 will be transparently redirected to the mysql_host:3306 through your SSH tunnel.任何到 localhost:3306 的流量都将通过您的 SSH 隧道透明地重定向到 mysql_host:3306。

Try using 'localhost:3306' in your config.尝试在您的配置中使用 'localhost:3306'。 Take a look here for few examples, it may help you too.看看这里的几个例子,它也可能对你有帮助。

I think what the problem was is that you did not specify a local_bind_address.我认为问题在于您没有指定 local_bind_address。 For this reason, the tunnel was assigned to your localhost port 60. MySQL cannot be connected to from this port.出于这个原因,隧道被分配到你的本地主机端口 60。MySQL 不能从这个端口连接到。

Specify the local_bind_address port with the local_bind_address parameter of the SSHTunnelForwarder() function.使用 SSHTunnelForwarder() 函数的 local_bind_address 参数指定 local_bind_address 端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM