Python MySQLdb 在连接尝试时冻结

发布于 2024-12-07 21:11:14 字数 719 浏览 2 评论 0原文

我正在尝试在 Python 中使用 MySQLdb 连接到我在 Pagoda Box 应用程序上建立的数据库。首先,我使用以下命令打开到数据库的 Pagoda 隧道:

$ pagoda tunnel -a <app-name>

它返回隧道已成功打开,连接在 127.0.0.1:3307 上可用。我在 Python IDLE 中运行以下命令:

import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1', port=3307,user='user',passwd='pass')

之后 IDLE 屏幕冻结(即它卡在无限循环中的当前命令上)。似乎挂在connections.py中的Connect()方法上。我不确定这是为什么或者如何解决它。非常感谢您的任何指导

===========================================更新============================================

我让脚本运行更长时间,并且我还尝试使用 phpmyadmin 以及本地计算机上的简单 mysqli_connect() 脚本进行连接。他们都返回:

MySQL错误:2013,“在‘读取初始通信数据包’时失去与MySQL服务器的连接,系统错误:0”

这似乎是问题的根源。我可以修复我的电脑上的某些配置来消除该问题吗?

I'm trying to use MySQLdb in Python to connect to the database I have established on a Pagoda Box application. First, I open the Pagoda tunnel to the database with:

$ pagoda tunnel -a <app-name>

and it returns that the tunnel has been successfully opened, with the connection available on 127.0.0.1:3307. I run the following commands in Python IDLE:

import MySQLdb
conn = MySQLdb.connect(host = '127.0.0.1', port=3307,user='user',passwd='pass')

after which the IDLE screen freezes (i.e. it is stuck on the current command on an infinite loop). It seems to be hung up on the Connect() method in connections.py. I'm not sure why this is or how to fix it. Any guidance you may have is greatly appreciated

=========================================UPDATE==========================================

I let the script run longer, and I additionally tried to connect using phpmyadmin, as well as a simple mysqli_connect() script on my local computer. All of them returned:

MySQL error: 2013, “Lost connection to MySQL server at 'reading initial communication packet', system error: 0”

This seems to be the root of the problem. Is there some configuration I can fix on my PC to eliminate the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔落墨 2024-12-14 21:11:15

作为一般规则,每当您试图找出程序停止的位置时,您可以使用 strace (strace myprog.py) 启动它,也可以在 pid 上运行 strace (strace -p pidnum)

例如,我的 ipython 有pid 为 12608,所以我运行

    strace -po 12608

然后运行

    con = MySQLdb.connect('xxx.xxx.xxx.xxx', 'user', '', 'database');

,我看到 strace 产生了成功的连接,

    setsockopt(4, SOL_SOCKET, SO_RCVTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
    setsockopt(4, SOL_SOCKET, SO_SNDTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
    setsockopt(4, SOL_IP, IP_TOS, [8], 4)   = 0
    setsockopt(4, SOL_TCP, TCP_NODELAY, [1], 4) = 0
    setsockopt(4, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0

我不确定这如何与 Pagoda Box 一起使用,但在正常的终端会话中,这就是我开始的地方

As a general rule whenever you are trying to figure out where a program is stalled you can either launch it with strace (strace myprog.py) or you can run an strace on the pid (strace -p pidnum)

for example, my ipython has a pid of 12608 so I run

    strace -po 12608

then I run

    con = MySQLdb.connect('xxx.xxx.xxx.xxx', 'user', '', 'database');

and I see the strace produce successful connections

    setsockopt(4, SOL_SOCKET, SO_RCVTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
    setsockopt(4, SOL_SOCKET, SO_SNDTIMEO, "\2003\341\1\0\0\0\0\0\0\0\0\0\0\0\0", 16) = 0
    setsockopt(4, SOL_IP, IP_TOS, [8], 4)   = 0
    setsockopt(4, SOL_TCP, TCP_NODELAY, [1], 4) = 0
    setsockopt(4, SOL_SOCKET, SO_KEEPALIVE, [1], 4) = 0

I am not sure how this works with Pagoda Box, but in a normal terminal session this is where I would start

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文