Connect Django to Google Cloud SQL

前端 未结 2 688
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 16:29

I\'m trying to connect Django to the Google cloud SQL, working with python 2.7 and django 1.5 under windows. I went through the instructions on this page: https://developers

2条回答
  •  -上瘾入骨i
    2020-12-29 17:07

    Example connection to Google Cloud SQL in Django:

    AppEngine Standard, Python 2.7:

    try:
        import MySQLdb  # noqa: F401
    except ImportError:
        import pymysql
        pymysql.install_as_MySQLdb()
    
    if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
        # Running on production App Engine, so connect to Google Cloud SQL using
        # the unix socket at /cloudsql/
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '/cloudsql/',
                'NAME': '',
                'USER': '',
                'PASSWORD': '',
            }
        }
    else:
        # Running locally so connect to either a local MySQL instance or connect to
        # Cloud SQL via the proxy. To start the proxy via command line:
        #
        #     $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
        #
        # See https://cloud.google.com/sql/docs/mysql-connect-proxy
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '127.0.0.1', # DB's IP address
                'PORT': '3306',
                'NAME': '',
                'USER': '',
                'PASSWORD': '',
            }
        }
    

    Source: GCP Python Django Samples AppEngine Standard Python 2.7


    AppEngine Standard, Python 3.7:

    # Install PyMySQL as mysqlclient/MySQLdb to use Django's mysqlclient adapter
    # See https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-db-api-drivers
    # for more information
    import pymysql  # noqa: 402
    pymysql.install_as_MySQLdb()
    
    if os.getenv('GAE_APPLICATION', None):
        # Running on production App Engine, so connect to Google Cloud SQL using
        # the unix socket at /cloudsql/
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '/cloudsql/[YOUR-CONNECTION-NAME]',
                'USER': '[YOUR-USERNAME]',
                'PASSWORD': '[YOUR-PASSWORD]',
                'NAME': '[YOUR-DATABASE]',
            }
        }
    else:
        # Running locally so connect to either a local MySQL instance or connect to
        # Cloud SQL via the proxy. To start the proxy via command line:
        #
        #     $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
        #
        # See https://cloud.google.com/sql/docs/mysql-connect-proxy
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '127.0.0.1', # DB's IP address
                'PORT': '3306',
                'NAME': '[YOUR-DATABASE]',
                'USER': '[YOUR-USERNAME]',
                'PASSWORD': '[YOUR-PASSWORD]',
            }
        }
    

    Source GCP Python Django Samples AppEngine Standard Python 3.7


    AppEngine Flexible:

    DATABASES = {
        'default': {
            # If you are using Cloud SQL for MySQL rather than PostgreSQL, set
            # 'ENGINE': 'django.db.backends.mysql' instead of the following.
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'polls',
            'USER': '',
            'PASSWORD': '',
            # For MySQL, set 'PORT': '3306' instead of the following. Any Cloud
            # SQL Proxy instances running locally must also be set to tcp:3306.
            'PORT': '5432',
        }
    }
    # In the flexible environment, you connect to CloudSQL using a unix socket.
    # Locally, you can use the CloudSQL proxy to proxy a localhost connection
    # to the instance
    DATABASES['default']['HOST'] = '/cloudsql/'
    if os.getenv('GAE_INSTANCE'):
        pass
    else:
        DATABASES['default']['HOST'] = '127.0.0.1' # DB's IP address
    

    Source GCP Python Django Samples AppEngine Flexible

提交回复
热议问题