我正在使用 MySQL-python 包。我可以从命令行执行 MySQL 相关脚本,但是通过浏览器(Apache CGI)执行相同操作会产生以下错误:
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/var/www/html/temp.py in ()
9
10 # Establich a connection
11 db = MySQLdb.connection(host="127.0.0.1", user="root", passwd="", db="inserv")
12
13 # Run a MySQL query from Python and get the result set
db undefined, MySQLdb = <module 'MySQLdb' from
'/usr/lib64/python2.6/site-packages
/MySQLdb/__init__.pyc'>, MySQLdb.connection = <type '_mysql.connection'>,
host undefined, user undefined, passwd undefined
<class '_mysql_exceptions.OperationalError'>:
(2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
args = (2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
message = ''
在过去的几天里,我一直陷入这种情况。通过基于 php 的站点发出的 MySQL 命令可以正常执行,我也可以从命令行登录 MySQL。问题似乎只出在 Python CGI 上。
我也对 oursql 包进行了同样的尝试,似乎也有类似的问题。我该如何解决这种情况?
编辑
根据@Real的回答,我已经编辑了要使用的代码,MySQLdb.connect()
但问题仍然存在,回溯以:
2003, "Can't connect to MySQL server on '127.0.0.1' (13)"