django怎么链接linux数据库
1个回答
展开全部
编辑 新建的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')
>>>
[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')
>>>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询