ubuntu下Django连接mysql数据库出错及解决

今天有时间就又开始了Django的学习,下载了《Django Web开发指南》,按照第二章的例子准备做下blog的小例子。没想到在开始不久在连接数据库的时候就开始出现了问题。耗费了我一个多小时的时间才终于将它解决。顺便说下Django Web开发指南这本书,大致的扫了一下,发现第一章讲python基本知识居然有6近0页,这是鸡肋啊。

按照书上的步骤,先创建一个项目

django-admin startproject mysite

之后运行开发服务器,这是一个Django自带的用于开发期间的服务器

1 ./manage.py runserver

之后创建blog应用

./manage.py startapp blog

然后修改setting.py文件,在INSTALLED_APPS元组的最后加上'mysite.blog',(结尾的逗号是必须的).

之后在models.py中设计model

1 from django.db import models
2 class BlogPost(models.Model):
3 title = models.CharField(max_length = 150)
4 body = models.TextField()
5 timestamp = models.DateTimeField()

接下来就是设置数据库了,也就是我出现问题的地方。Django支持mysql,PostgreSQL,Oracle等数据库,还有简单的附带在python中SQLite.我选用mysql来进行数据库的设置.

首先是修改setting.py文件,其中的数据库部分

 1 DATABASES = {
2 'default': {
3 'ENGINE': 'django.db.backends.mysql',
4 'NAME': 'mysql',
5 'USER': 'root',
6 'PASSWORD': 'fan',
7 'HOST': 'localhost',
8 'PORT': '',
9 }
10 }

这是最终的配置这个正确的结果,这里的name一定要设定为你的数据库的名字,如果错了就会出现一下错误信息,我开始写的是djangodb

    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1049, "Unknown database 'djangodb'")

而一开始我出现的是下面的错误,非常的长,而有用的是最后的错误提示

root@Fan's PC:/home/fan/djangoProject/mysite# ./manage.py syncdb
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.7/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/usr/lib/pymodules/python2.7/django/core/management/sql.py", line 6, in <module>
from django.db import models
File "/usr/lib/pymodules/python2.7/django/db/__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 33, in load_backend
return import_module('.base', backend_name)
File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.7/django/db/backends/mysql/base.py", line 14, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
root@Fan's PC:/home/fan/djangoProject/mysite# vi settings.py
root@Fan's PC:/home/fan/djangoProject/mysite# ./manage.py syncdb
Traceback (most recent call last):
File "./manage.py", line 14, in <module>
execute_manager(settings)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/usr/lib/pymodules/python2.7/django/core/management/__init__.py", line 67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.7/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/usr/lib/pymodules/python2.7/django/core/management/sql.py", line 6, in <module>
from django.db import models
File "/usr/lib/pymodules/python2.7/django/db/__init__.py", line 78, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/usr/lib/pymodules/python2.7/django/db/utils.py", line 33, in load_backend
return import_module('.base', backend_name)
File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.7/django/db/backends/mysql/base.py", line 14, 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连接上。我开始尝试进入mysql,不知是为什么,一直出现类似与

error: 'Access denied for user 'root'@'localhost' (using password: NO)'的错误。我猜想可能是密码的原因,不管了,时间紧迫我就想着重新安装mysql。

在ubuntu下,首先进行卸载,然后安装,命令如下

1 卸载
2 sudo apt-get autoremove --purge mysql-server
3 sudo apt-get remove mysql-common //用于彻底删除
4 安装
5 sudo apt-get install mysql-server
6 sudo apt-get install mysql-client

在安装的过程中会弹出一个对话框来设置mysql 的root用户的密码。

但是重新安装好后还是不能成功。查找资料过后发现还需要安装python-mysql的触发器,也就是python-mysqldb

apt-get install python-mysqldb

安装好后,我再进行测试,终于成功了。

root@Fan's PC:/home/fan/djangoProject/mysite# ./manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table blog_blogpost

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'root'): ***
E-mail address: ******@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.

总结一下,引发这些问题其实很简单,就是mysql数据库和django的配置两个方面,setting.py文件不能写错,另外还需要安装python-mysqldb,否则会出现错误。





  

你可能感兴趣的:(ubuntu)