如何运行针对Google Cloud SQL的Django管理命令 [英] How to run Django management commands against Google Cloud SQL

查看:91
本文介绍了如何运行针对Google Cloud SQL的Django管理命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我已经在Google应用引擎上部署了我的django项目。我需要运行 python manage.py migrate 命令,以便在我的Google云实例上创建 auth_user 表。但是不知道在哪里运行这个命令。

解决方案

如果我的意思是正确的,你的应用程序在App Engine(沙盒环境)上运行,并使用Cloud SQL。



1)在 settings.py 中配置数据库,如下所示。

 如果os.getenv('SERVER_SOFTWARE','').startswith('Google App Engine'):
#在生产App Engine上运行,所以使用Google Cloud SQL数据库。
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'HOST':'/ cloudsql / project-id: instance-name',
'NAME':'database-name',
'USER':'root',
}
}
elif os.getenv 'SETTINGS_MODE')=='prod':
#正在开发中运行,但要在生产中访问Google Cloud SQL实例。
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'INSTANCE':'cloud-sql-instance-ip -address',
'NAME':'database-name',
'USER':'root',
'PASSWORD':'password',
}
}
else:
#运行在开发中,所以使用本地的MySQL数据库。
DATABASES = {
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'database-name',
'USER':'username',
'PASSWORD':'password',
}
}

2)将环境变量SETTINGS_MODE设置为prod(或者如果要访问本地MySQL服务器,则不设置)。



3)从您的机器运行以下命令。

  $ SETTINGS_MODE = prod python manage.py migrate 

您可以在App Engine文档中找到更多详细信息 - 管理命令备用开发数据库和设置


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.

解决方案

If I get it right, your app runs on App Engine (sandboxed environment) and uses Cloud SQL.

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) Set environment variable SETTINGS_MODE to prod (or do not set if you want to access your local MySQL server).

3) Run the below command from your machine.

$ SETTINGS_MODE=prod python manage.py migrate

You can find more details in App Engine documentation - Management commands and Alternate development database and settings.

这篇关于如何运行针对Google Cloud SQL的Django管理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆