用于Python MySQLdb连接的SSH隧道 [英] SSH Tunnel for Python MySQLdb connection

查看:70
本文介绍了用于Python MySQLdb连接的SSH隧道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用创建一个SSH隧道

I tried creating a SSH tunnel using

ssh -L 3306:localhost:22 <hostip>

然后运行我的python脚本以通过本地主机进行连接

Then running my python script to connect via localhost

conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test')

但是,我收到以下错误消息

However, I receive the following error

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

如何确保我找到了正确的主机,而不仅仅是绑定问题?

How can I make sure I'm hitting the correct host and not just some problem with the bind?

推荐答案

尝试将"localhost"更改为"127.0.0.1",它将按预期工作. 手册:

Try changing "localhost" to "127.0.0.1", it should work as you expect. This behavior is detailed in the manual:

UNIX套接字和命名管道不 通过网络工作,所以如果您指定 除本地主机以外的主机,TCP将 使用,您可以指定一个奇数 端口(如果需要)(默认端口 是3306):

UNIX sockets and named pipes don't work over a network, so if you specify a host other than localhost, TCP will be used, and you can specify an odd port if you need to (the default port is 3306):

db=_mysql.connect(host="outhouse", port=3307, passwd="moonpie", db="thangs")

如果确实需要,您可以 通过TCP连接到本地主机 指定完整的主机名,或 127.0.0.1 .

If you really had to, you could connect to the local host with TCP by specifying the full host name, or 127.0.0.1.

这篇关于用于Python MySQLdb连接的SSH隧道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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