1. 按照django官網的文檔https://docs.djangoproject.com/en/1.5/intro/tutorial01/ 一步一步安裝成功,修改好setting.py之後 ,執行
python manage.py syncdb
拋出如下異常
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
2. google了一下相關的資訊說是MySQL-python沒裝,yum install MySQL-python安裝,提示本機已經有更新的版本,但執行python manage.py sycndb還是提示一樣的錯誤。
在stackoverflow上看到一個問題:http://stackoverflow.com/questions/2952187/getting-error-loading-mysqldb-module-no-module-named-mysqldb-have-tried-pre
我本機沒有pip命令,也沒有研究它,執行上面文章說的命令:easy_install
MySQL-python,提示我:
The required
version of distribute (>=0.6.28) is not available,
and can't be installed while this script is running. Please
install a more recent version first, using
'easy_install -U distribute'.
於是執行easy_install
-U distribute,安裝成功後,再次執行easy_install MySQL-python成功!
3 再次執行:
python manage.py syncdb
setting.py設定HOST為空白時,提示:OperationalError:
(1045, "Access denied for user 'root'@'localhost' (using password: NO)")
但是查看mysql資料庫,host+使用者名稱+密碼都沒有問題。
注意到setting.py設定HOST那一行有注釋:Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
我的Host配的是127.0.0.1,意思好像是會通過TCP串連,如果為空白會通過socket串連,於是改成127.0.0.1,再執行python manage.py syncdb,建立成功。
這裡還要注意setting.py檔案中:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangotest', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '4306', # Set to empty string for default.
}
}
這裡的'NAME'值填資料庫的名字,資料庫必須預先建立好,django不會為你建立資料庫,在django documentation上也有說明。