问题描述
目前,我已经在谷歌应用引擎上部署了我的 django 项目.我需要运行 python manage.py migrate
命令,以便 auth_user
表应该在我的谷歌云实例上创建.但是不知道在哪里运行这个命令.
Currently , I have deployed my django project on google app engine. I need to run python manage.py migrate
command so that auth_user
table should be created on my google cloud instance . But don't know where to run this command.
推荐答案
如果我没猜错,您的应用会在 App Engine(沙盒环境)上运行并使用 Cloud SQL.
If I get it right, your app runs on App Engine (sandboxed environment) and uses Cloud SQL.
1) 在 settings.py
中配置您的数据库,如下所示.
1) Configure your database in settings.py
as you can see below.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
# Running on production App Engine, so use a Google Cloud SQL database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/project-id:instance-name',
'NAME': 'database-name',
'USER': 'root',
}
}
elif os.getenv('SETTINGS_MODE') == 'prod':
# Running in development, but want to access the Google Cloud SQL instance in production.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'INSTANCE': 'cloud-sql-instance-ip-address',
'NAME': 'database-name',
'USER': 'root',
'PASSWORD': 'password',
}
}
else:
# Running in development, so use a local MySQL database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database-name',
'USER': 'username',
'PASSWORD': 'password',
}
}
2) 将环境变量 SETTINGS_MODE 设置为 prod(如果要访问本地 MySQL 服务器,则不要设置).
2) Set environment variable SETTINGS_MODE to prod (or do not set if you want to access your local MySQL server).
3) 在您的机器上运行以下命令.
3) Run the below command from your machine.
$ SETTINGS_MODE=prod python manage.py migrate
您可以在 App Engine 文档中找到更多详细信息 - 管理命令 和 备用开发数据库和设置.
You can find more details in App Engine documentation - Management commands and Alternate development database and settings.
这篇关于如何针对 Google Cloud SQL 运行 Django 管理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!