配置Django使用MySQL数据库的例子

Seven 绯闻SEO 绯闻SEO,一个专注中小企业网站优化的SEO爱好者

Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了.

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

  1. [root@itchenyi-1Django-1.3.3]#yuminstallmysql-servermysql-devel
  2. [root@itchenyi-1Django-1.3.3]#yuminstallMySQL-python

2、设置Mysql 数据库及用户:

  1. [root@itchenyi-1Django-1.3.3]#servicemysqldstart
  2. [root@itchenyi-1Django-1.3.3]#mysql-uroot-p
  3. Copyright(c)2000,2010,Oracleand/oritsaffiliates.Allrightsreserved.
  4. ThissoftwarecomeswithABSOLUTELYNOWARRANTY.Thisisfreesoftware,
  5. andyouarewelcometomodifyandredistributeitundertheGPLv2license--phpfensi.com
  6. type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
  7. mysql>createdatabaseitchenyi_db;
  8. QueryOK,1rowaffected(0.00sec)
  9. mysql>GRANTALLONitchenyi_db.*TO'itchenyi'@'localhost'IDENTIFIEDBY'yourpassword';
  10. QueryOK,0rowsaffected(0.00sec)
  11. mysql>quit
  12. Bye

3、create a django project:

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

4、编辑 新建的project 配置文件(settings.py):

  1. [root@itchenyi-1Django-1.3.3]#viitchenyi/settings.py
  2. DATABASES={
  3. 'default':{
  4. 'ENGINE':'django.db.backends.mysql',#Add'postgresql_psycopg2','postgresql','mysql','sqlite3'or'oracle'.
  5. 'NAME':'itchenyi_db',#Orpathtodatabasefileifusingsqlite3.
  6. 'USER':'itchenyi',#Notusedwithsqlite3.
  7. 'PASSWORD':'yourpassword',#Notusedwithsqlite3.
  8. 'host':'',#settoemptystringforlocalhost.Notusedwithsqlite3.
  9. 'PORT':'',#Settoemptystringfordefault.Notusedwithsqlite3.
  10. }
  11. }

5、切换到新建的project 创建数据库和表:

  1. [root@itchenyi-1Django-1.3.3]#cditchenyi/
  2. [root@itchenyi-1itchenyi]#pythonmanage.pysyncdb
  3. Creatingtables...
  4. Creatingtableauth_permission
  5. Creatingtableauth_group_permissions
  6. Creatingtableauth_group
  7. Creatingtableauth_user_user_permissions
  8. Creatingtableauth_user_groups
  9. Creatingtableauth_user
  10. Creatingtableauth_message
  11. Creatingtabledjango_content_type
  12. Creatingtabledjango_session
  13. Creatingtabledjango_site
  14. YoujustinstalledDjango'sauthsystem,whichmeansyoudon'thaveanysuperusersdefined.--phpfensi.com
  15. Wouldyouliketocreateonenow?(yes/no):yes
  16. Username(Leaveblanktouse'root'):itchenyi
  17. E-mailaddress:itchenyi@gmail.com
  18. Password:
  19. Password(again):
  20. Superusercreatedsuccessfully.
  21. InstallingcustomSQL...
  22. Installingindexes...
  23. Nofixturesfound.

6、简单验证:

  1. [root@itchenyi-1itchenyi]#pythonmanage.pyShell
  2. Python2.6.6(r266:84292,Dec72011,20:48:22)
  3. [gcc4.4.620110731(RedHat4.4.6-3)]onlinux2
  4. Type"help","copyright","credits"or"license"formoreinformation.
  5. (InteractiveConsole)
  6. >>>importMySQLdb
  7. >>>db=MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='yourpassword'
  8. ,host='localhost')
  9. >>>

相关广告
  • 配置Django使用MySQL数据库的例子 配置Django使用MySQL数据库的例子 配置Django使用MySQL数据库的例子
相关阅读

配置Django使用MySQL数据库的例子

2019/10/10 17:46:40 | 谷歌SEO算法 | 发外链