尝试从python cgi脚本连接到mysql服务器时,连接建立失败

编程入门 行业动态 更新时间:2024-10-12 05:50:48
本文介绍了尝试从python cgi脚本连接到mysql服务器时,连接建立失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个配置为使用python脚本的apache服务器(版本Apache / 2.2.15)。该脚本应该连接到数据库并将数据插入表。 我写了一个简单的代码如下:

Hi, I have an apache server (version Apache/2.2.15) configured to use python script. The script is supposed to connect to database and inserts data to table. I have written a simple code as below:

import MySQLdb as mdb db = mdb.connect(host="mysql", user="one", passwd="two", db="test") cur = db.cursor() querystring = "INSERT INTO mytable (username, password) value (\"%s\", \"%s\")"%("aaa","bbb")

从python命令使用时此代码正常工作终端,但从cgi脚本使用时失败。

This code works fine when used from python command terminal, but fails when used from cgi script.

File "/var/www/cgi-bin/add_user.py", line 39, in <module> addEntry(prop.text.strip()) File "/var/www/cgi-bin/add_user.py", line 19, in addEntry db = mdb.connect(host="203.171.xx.xx", user="usrone", passwd="psdone", db="test", port=3306) File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 187, in __init__ super(Connection, self).__init__(*args, **kwargs2) OperationalError: (2003, "Can't connect to MySQL server on '203.171.xx.xx' (13)")

脚本文件具有所有权限(chmod 777),并且在两个pc的 中都禁用了防火墙任何关于为什么这样做的建议可能会发生吗? 谢谢

The script file has all permissions (chmod 777) and firewall is disabled in both the pc's any suggestions as to why this might happen? Thanks

推荐答案

端口号?通过为数据库连接设置错误的端口号,我能够在我的设置上重现此错误消息。不正确的主机地址也会产生它。 Port number? I was able to reproduce this error message on my setup by setting an incorrect port number for the database connection. An incorrect host address also produces it.

更多推荐

尝试从python cgi脚本连接到mysql服务器时,连接建立失败

本文发布于:2023-11-27 15:02:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638552.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   脚本   服务器   python   cgi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!