本文介绍了尝试从python cgi脚本连接到mysql服务器时,连接建立失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个配置为使用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 mdbdb = 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'sany 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服务器时,连接建立失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 03:57