django 和 mysql的一次troubleshooting
下面是一次用django连接mysql的经历,记录下来也许以后会有帮助。
首先是用django的./manage.py syncdb 去连接mysql
-bash-3.2$ ./manage.py syncdb Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in execute translation.activate('en-us') File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate return _trans.activate(language) File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate _active.value = translation(language) File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 177, in translation default_translation = _fetch(settings.LANGUAGE_CODE) File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch app = import_module(appname) File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name) File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 6, in <module> from django.contrib.admin.sites import AdminSite, site File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 4, in <module> from django.contrib.admin.forms import AdminAuthenticationForm File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/forms.py", line 6, in <module> from django.contrib.auth.forms import AuthenticationForm File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 17, in <module> from django.contrib.auth.models import User File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 48, in <module> class Permission(models.Model): File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 96, in __new__ new_class.add_to_class('_meta', Options(meta, **kwargs)) File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 264, in add_to_class value.contribute_to_class(cls, name) File "/usr/local/lib/python2.7/site-packages/django/db/models/options.py", line 124, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 198, in __getitem__ backend = load_backend(db['ENGINE']) File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 113, in load_backend return import_module('%s.base' % backend_name) File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module __import__(name) File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module> raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
这里面最重要的就是最后一句 No module named MySQLdb. 这说明MySQL-python这个package没有安装成功。于是用pip2.7 install MySQL-python来安装,但是遇到如下错误。
[root@kramer site-packages]# pip2.7 install MySQL-python Downloading/unpacking MySQL-python Downloading MySQL-python-1.2.5.zip (108kB): 108kB downloaded Running setup.py (path:/tmp/pip_build_root/MySQL-python/setup.py) egg_info for package MySQL-python Installing collected packages: MySQL-python Running setup.py install for MySQL-python building '_mysql' extension gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv _mysql.c:44:23: error: my_config.h: No such file or directory _mysql.c:46:19: error: mysql.h: No such file or directory _mysql.c:47:26: error: mysqld_error.h: No such file or directory _mysql.c:48:20: error: errmsg.h: No such file or directory
这个错误很长所以只截取一小段来看。发现都是c/c++的头文件找不到,这一般是因为某个dev的包没有找到。在linux上你安装一个软件的时候这个软件通常有两部分,一个是dev包一个是应用包。如果你只要使用的话安装应用包就可以了,但是某些情况下需要dev包。 这里是mysql的一些头文件找不到所以我们应该安装mysql的dev包。 猜测名字应该是类似于mysql-devel
[root@kramer site-packages]# yum install mysql-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.tocici.com * extras: mirror.tocici.com * updates: mirror.tocici.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package mysql-devel.i386 0:5.0.95-5.el5_9 set to be updated ---> Package mysql-devel.x86_64 0:5.0.95-5.el5_9 set to be updated --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================== Package Arch Version Repository Size ======================================================================================================================================================================== Installing: mysql-devel i386 5.0.95-5.el5_9 base 2.4 M mysql-devel x86_64 5.0.95-5.el5_9 base 2.5 M Transaction Summary ======================================================================================================================================================================== Install 2 Package(s) Upgrade 0 Package(s) Total download size: 4.9 M Is this ok [y/N]: y Downloading Packages: (1/2): mysql-devel-5.0.95-5.el5_9.i386.rpm | 2.4 MB 00:00 (2/2): mysql-devel-5.0.95-5.el5_9.x86_64.rpm | 2.5 MB 00:00 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Total 6.5 MB/s | 4.9 MB 00:00 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-devel 1/2 Installing : mysql-devel 2/2 Installed: mysql-devel.i386 0:5.0.95-5.el5_9 mysql-devel.x86_64 0:5.0.95-5.el5_9 Complete!
果然有这么一个包。安装之后就可以执行pip install。