django使用mysql

來源:互聯網
上載者:User

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上也有說明。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.