windows下用Python连接mysql报错问题“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””

 

      这几天在windows用Python连接mysql,报出了“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””, 写了一个以下的挫代码,然后出错了,各种“屌丝百科”没有果

 

"""
__author__ = 'chenguolin'
__date__=2014-02-17
"""
import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="test")
cursor = db.cursor()

cursor.execute("create table chenguolin(name char(8), age int)")


       出现这个错误

D:\Python27\python.exe D:/scrapyProjects/testPython/p.py
Traceback (most recent call last):
  File "D:/scrapyProjects/testPython/p.py", line 7, in <module>
    db = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="123456", db="test")
  File "D:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "D:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")


               果断把错误放到谷歌,于是在stackoverflow搜到了一个问题

windows下用Python连接mysql报错问题“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””_第1张图片


         看到回答非常欣慰,顿时感觉,百度就是一个“屌丝”的百科

windows下用Python连接mysql报错问题“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””_第2张图片


 

         于是改成了以下的代码就ok了,实际上就是localhost的问题,只要替换为127.0.0.1即可

"""
__author__ = 'chenguolin'
__date__=2014-02-17
"""
import MySQLdb

db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="123456", db="test")
cursor = db.cursor()

cursor.execute("create table chenguolin(name char(8), age int)")


      

你可能感兴趣的:(windows下用Python连接mysql报错问题“MySQLdb in Python: “Can't connect to MySQL server on 'localhost'””)