我有一个名为getpos的Django views.py函数,它由apache2和wsgi.py提供服务 . 如果我在Ubuntu命令行的普通Python会话中从views.py运行完全相同的代码,MySQLdb连接正常工作:
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost",port=22,user="root",passwd="mypassword",db="gps")
>>>
>>> cursor = db.cursor()
如果我从django调试服务器运行它,它失败了:
ERROR: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
运行的代码是这样的:
def getpos(request):
query = "SELECT * FROM..."
# connect to the DB and return nearby deals
db = MySQLdb.connect(host="localhost",port=22,user="root",passwd="mypassword",db="gps")
cursor = db.cursor()
# Query the gps Data database
results = cursor.execute(query)
db.commit()
如何在命令行上从Python连接,而不是在Django的views.py文件中连接?谢谢 .
我尝试使用“127.0.0.1”而不是“localhost”,但似乎没有任何效果 . 感谢任何帮助,谢谢!
1 回答
我不得不将connect参数中的端口更改为host =“localhost”,port = 3306 .