配置Django使用MySQL数据库的例子
2019/10/10/17:46:40 阅读:2027 来源:谷歌SEO算法 标签:
发外链
Django是由Python驱动的开源模型-视图-控制器(MVC)风格的Web应用程序框架了,下面我们就来介绍这款框架配置Django使用MySQL数据库的例子了.
1、安装mysql(Django 安装略):
- [root@itchenyi-1Django-1.3.3]#yuminstallmysql-servermysql-devel
- [root@itchenyi-1Django-1.3.3]#yuminstallMySQL-python
2、设置Mysql 数据库及用户:
- [root@itchenyi-1Django-1.3.3]#servicemysqldstart
- [root@itchenyi-1Django-1.3.3]#mysql-uroot-p
- Copyright(c)2000,2010,Oracleand/oritsaffiliates.Allrightsreserved.
- ThissoftwarecomeswithABSOLUTELYNOWARRANTY.Thisisfreesoftware,
- andyouarewelcometomodifyandredistributeitundertheGPLv2license--phpfensi.com
- type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
- mysql>createdatabaseitchenyi_db;
- QueryOK,1rowaffected(0.00sec)
- mysql>GRANTALLONitchenyi_db.*TO'itchenyi'@'localhost'IDENTIFIEDBY'yourpassword';
- QueryOK,0rowsaffected(0.00sec)
- 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-1Django-1.3.3]#viitchenyi/settings.py
- DATABASES={
- 'default':{
- 'ENGINE':'django.db.backends.mysql',#Add'postgresql_psycopg2','postgresql','mysql','sqlite3'or'oracle'.
- 'NAME':'itchenyi_db',#Orpathtodatabasefileifusingsqlite3.
- 'USER':'itchenyi',#Notusedwithsqlite3.
- 'PASSWORD':'yourpassword',#Notusedwithsqlite3.
- 'host':'',#settoemptystringforlocalhost.Notusedwithsqlite3.
- 'PORT':'',#Settoemptystringfordefault.Notusedwithsqlite3.
- }
- }
5、切换到新建的project 创建数据库和表:
- [root@itchenyi-1Django-1.3.3]#cditchenyi/
- [root@itchenyi-1itchenyi]#pythonmanage.pysyncdb
- Creatingtables...
- Creatingtableauth_permission
- Creatingtableauth_group_permissions
- Creatingtableauth_group
- Creatingtableauth_user_user_permissions
- Creatingtableauth_user_groups
- Creatingtableauth_user
- Creatingtableauth_message
- Creatingtabledjango_content_type
- Creatingtabledjango_session
- Creatingtabledjango_site
- YoujustinstalledDjango'sauthsystem,whichmeansyoudon'thaveanysuperusersdefined.--phpfensi.com
- Wouldyouliketocreateonenow?(yes/no):yes
- Username(Leaveblanktouse'root'):itchenyi
- E-mailaddress:itchenyi@gmail.com
- Password:
- Password(again):
- Superusercreatedsuccessfully.
- InstallingcustomSQL...
- Installingindexes...
- Nofixturesfound.
6、简单验证:
- [root@itchenyi-1itchenyi]#pythonmanage.pyShell
- Python2.6.6(r266:84292,Dec72011,20:48:22)
- [gcc4.4.620110731(RedHat4.4.6-3)]onlinux2
- Type"help","copyright","credits"or"license"formoreinformation.
- (InteractiveConsole)
- >>>importMySQLdb
- >>>db=MySQLdb.connect(user='itchenyi',db='itchenyi_db',passwd='yourpassword'
- ,host='localhost')
- >>>
热门评论