MySQLdb 连接Mysql 数据库出错解决

  1. #coding=utf-8  
  2. import MySQLdb  
  3.   
  4. if __name__ == "__main__":  
  5.     db = MySQLdb.connect(host=<span style="color:#FF0000;">'localhost'</span>,  
  6.                      port=3306,  
  7.                      user='root',  
  8.                      passwd=XX',  
  9.                      db='XX')  
  10.     cursor = db.cursor()  
  11.     sql = "select * from student"  
  12.     cursor.execute(sql)  
  13.     for line in cursor.fetchall():  
  14.         print line  
  15.     db.close()  

运行时出现如下错误:

[plain] view plaincopy
 
  1. pydev debugger: starting  
  2. Traceback (most recent call last):  
  3.   File "C:Program Filesaptan3pluginsorg.python.pydev_2.6.0.2012062121pysrcpydevd.py", line 1392, in <module>  
  4.     debugger.run(setup['file'], None, None)  
  5.   File "C:Program Filesaptan3pluginsorg.python.pydev_2.6.0.2012062121pysrcpydevd.py", line 1085, in run  
  6.     pydev_imports.execfile(file, globals, locals) #execute the script  
  7.   File "D:Aptana Studio 3 Workspacefirstcomlin est01.py", line 9, in <module>  
  8.     db='netbase')  
  9.   File "E:python27libsite-packagesMySQLdb\__init__.py", line 81, in Connect  
  10.     return Connection(*args, **kwargs)  
  11.   File "E:python27libsite-packagesMySQLdbconnections.py", line 187, in __init__  
  12.     super(Connection, self).__init__(*args, **kwargs2)  
  13. _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")  
把host="localhost"  改为 host="127.0.0.1"就可以了
[python] view plaincopy
 
  1. #coding=utf-8  
  2. import MySQLdb  
  3.   
  4. if __name__ == "__main__":  
  5.     db = MySQLdb.connect(<span style="color:#FF0000;">host='127.0.0.1',</span>  
  6.                      port=3306,  
  7.                      user='root',  
  8.                      passwd=XX',  
  9.                      db='XX')  
  10.     cursor = db.cursor()  
  11.     sql = "select * from student"  
  12.     cursor.execute(sql)  
  13.     for line in cursor.fetchall():  
  14.         print line  
  15.     db.close()  
运行如下:
[plain] view plaincopy
 
  1. pydev debugger: starting  
  2. ('lin', 88L)  
  3. ('cjm', 8L)  
Django + MySQLdb + Mysql settings 文件数据库设置:
[plain] view plaincopy
 
  1. DATABASES = {  
  2.     'default': {  
  3.         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.  
  4.         'NAME': 'mydb',                      # Or path to database file if using sqlite3.  
  5.         # The following settings are not used with sqlite3:  
  6.         'USER': 'root',  
  7.         'PASSWORD': 'mydb',  
  8.         #'HOST': '',     
  9.         'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.  
  10.         'PORT': '3306',                      # Set to empty string for default.  
  11.     }  
  12. }  
然后连接数据库:
[plain] view plaincopy
 
  1. import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))  
  2. PyDev console: using default backend (IPython not available).  
  3. E:python27python.exe 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]  
  4.   
  5. from django.core import management;import netbase.settings as settings;management.setup_environ(settings)  
  6. u'D:\Aptana Studio 3 Workspace\netbase\netbase'  
  7. from django.db import models  
  8. from django.db import connection  
  9. cursor = connection.cursor()  

转:http://blog.csdn.net/jinnian_lin/article/details/10071081

原文地址:https://www.cnblogs.com/wangjiyuan/p/pythonmysql.html