配置Django使用MySQL資料庫的例子

來源:互聯網
上載者:User


1、安裝mysql (Django 安裝略):

[root@itchenyi-1 Django-1.3.3]# yum install mysql-server mysql-devel
[root@itchenyi-1 Django-1.3.3]# yum install MySQL-python

2、設定Mysql 資料庫 及使用者:

[root@itchenyi-1 Django-1.3.3]# service mysqld start
[root@itchenyi-1 Django-1.3.3]# mysql -u root -p

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database itchenyi_db;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL ON itchenyi_db.* TO 'itchenyi'@'localhost' IDENTIFIED BY 'your password';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
3、create a django project:

[root@itchenyi-1 Django-1.3.3]# django-admin.py startproject itchenyi


4、編輯 建立的project 設定檔(settings.py):

[root@itchenyi-1 Django-1.3.3]# vi itchenyi/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'itchenyi_db',                      # Or path to database file if using sqlite3.
        'USER': 'itchenyi',                      # Not used with sqlite3.
        'PASSWORD': 'your password',                  # Not used with sqlite3.
        'host': '',                      # set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

5、切換到建立的project 建立資料庫和表:

[root@itchenyi-1 Django-1.3.3]# cd itchenyi/
[root@itchenyi-1 itchenyi]# python 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

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'): itchenyi
E-mail address: itchenyi@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.

6、簡單驗證:

[root@itchenyi-1 itchenyi]# python manage.py Shell
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[gcc 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import MySQLdb
>>> db = MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='your password'
,host='localhost')
>>>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.