python连接mysql数据库

import MySQLdb as mdb

con=None
try:
    con=mdb.connect(host="localhost",user="root",passwd="root",db="test")
    cur=con.cursor()
    cur.execute("SELECT VERSION()")
    data = cur.fetchone()
    print "Database version : %s " % data 
finally:
    if con:
        con.close()

为什么将localhost改为127.0.0.1就可以成功连接,但是如果就是localhost的话就不能了呢?

问题点:使用localhost为host为参数值,无法正常连接数据库
分析: localhost的默认值应该被修改过,不是127.0.0.1,导致连接失败
处理方法:
打开配置文件dev.py,修改DATABASES的配置信息