在根据这个提问尝试使用python连接MySQL时,使用如下连接配置:

import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="student")

但是在尝试连接的时候报错如下:

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

查阅了一些资料,有很多中解释,概况起来大概可以这样理解:

1.MySQL在Windows上把localhost解析为了IPv6的::1,参考这篇博客

2.官网的解释,大概是指localhost是UNIX系统使用的(亲测可用),要想在Windows下使用,是比较麻烦滴。

概况起来讲,就是在Windows上就不要用localhost了,反正它最后也要被解析为127.0.0.1,还不如直接用127.0.0.1来的方便。

所以,上面的代码改为这样就没有问题了:

import MySQLdb

db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="student")

其他参考资料:

[1].http://stackoverflow.com/questions/7382602/what-is-the-difference-between-127-0-0-1-and-localhost

[2].http://forum.wampserver.com/read.php?2,110383,110408

[3].http://mysql-python.sourceforge.net/MySQLdb.html



blog comments powered by Disqus

Published

09 July 2014

Category

tech_world

Tags