情景:在python中使用mysql
已知:
[root@iZ28cumdzmgZ muahao03]# pip list Django (1.7.1) MySQL-python (1.2.5) pip (1.5.6) pysqlite (2.6.3) setuptools (8.2.1) wsgiref (0.1.2)
已经安装
MySQL-python
报错:
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
[root@iZ28cumdzmgZ muahao03]# python Python 2.7.9 (default, Dec 19 2014, 18:09:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb Traceback (most recent call last): File "", line 1, in File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__ ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory >>>
[root@iZ28cumdzmgZ muahao03]# service mysql status SUCCESS! MySQL running (22804) [root@iZ28cumdzmgZ muahao03]# netstat -antlpe | grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 500 171576 22804/mysqld [root@iZ28cumdzmgZ muahao03]# rpm -ql mysql /usr/bin/msql2mysql /usr/bin/my_print_defaults /usr/bin/mysql /usr/bin/mysql_config /usr/bin/mysql_find_rows /usr/bin/mysql_waitpid /usr/bin/mysqlaccess /usr/bin/mysqladmin /usr/bin/mysqlbinlog /usr/bin/mysqlcheck /usr/bin/mysqldump /usr/bin/mysqlimport /usr/bin/mysqlshow /usr/bin/mysqlslap /usr/lib64/mysql/mysql_config /usr/lib64/mysql/mysqlbug /usr/share/doc/mysql-5.1.73 /usr/share/doc/mysql-5.1.73/COPYING /usr/share/doc/mysql-5.1.73/README /usr/share/doc/mysql-5.1.73/README.mysql-docs /usr/share/doc/mysql-5.1.73/README.mysql-license /usr/share/man/man1/my_print_defaults.1.gz /usr/share/man/man1/mysql.1.gz /usr/share/man/man1/mysql_config.1.gz /usr/share/man/man1/mysql_find_rows.1.gz /usr/share/man/man1/mysql_waitpid.1.gz /usr/share/man/man1/mysqlaccess.1.gz /usr/share/man/man1/mysqladmin.1.gz /usr/share/man/man1/mysqldump.1.gz /usr/share/man/man1/mysqlshow.1.gz /usr/share/man/man1/mysqlslap.1.gz [root@iZ28cumdzmgZ muahao03]# python manage.py syncdb Traceback (most recent call last): File "manage.py", line 10, inexecute_from_command_line(sys.argv) File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute django.setup() File "/usr/local/python2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models(all_models) File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/usr/local/python2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in class Permission(models.Model): File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__ new_class.add_to_class('_meta', Options(meta, **kwargs)) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/base.py", line 299, in add_to_class value.contribute_to_class(cls, name) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__ backend = load_backend(db['ENGINE']) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend return import_module('%s.base' % backend_name) File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决:
[root@iZ28cumdzmgZ muahao03]# find / -name libmysqlclient.so.18 /usr/local/mysql/lib/libmysqlclient.so.18 [root@iZ28cumdzmgZ muahao03]# ls -ld /usr/lib64/ dr-xr-xr-x. 50 root root 36864 Dec 23 03:40 /usr/lib64/ [root@iZ28cumdzmgZ muahao03]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 [root@iZ28cumdzmgZ muahao03]# python Python 2.7.9 (default, Dec 19 2014, 18:09:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
表的实现,进行数据库同步动作:
再次执行:
又报错:
[root@iZ28cumdzmgZ muahao03]# python manage.py syncdb Traceback (most recent call last): File "manage.py", line 10, inexecute_from_command_line(sys.argv) File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute django.setup() File "/usr/local/python2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models(all_models) File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models self.models_module = import_module(models_module_name) File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/root/muahao03/blog/models.py", line 3, in class Employee(models.Model): File "/root/muahao03/blog/models.py", line 4, in Employee name=models.CharFeild(max_length=20) AttributeError: 'module' object has no attribute 'CharFeild' [root@iZ28cumdzmgZ muahao03]# python Python 2.7.9 (default, Dec 19 2014, 18:09:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from blog.models import Employee Traceback (most recent call last): File " ", line 1, in File "blog/models.py", line 3, in class Employee(models.Model): File "blog/models.py", line 4, in Employee name = models.CharFeild(max_length=20) AttributeError: 'module' object has no attribute 'CharFeild' >>>
解决:原因是我把CharField 写成了CharFeild
[root@iZ28cumdzmgZ muahao03]# vim blog/models.py
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length=20)