简体   繁体   中英

Can't connect to server error Google App Engine Static Files Issue

I'm having difficulties switching my Google App Engine web app from a standard to a flex environment using Django. I need to switch to a flex environment since I am using the module pdf2image in my app, which uses poppler . Here are my app.yaml, Dockerfile, and snippets from my settings.py.

Note:

  • Before making these changes the app ran as it should locally
  • There are probably multiple errors and we would appreciate any feedback on which parts to fix. Thanks!

Here is the error which is showing:

File "/env/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect return Connection(*args, **kwargs) 
File "/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 164, in __init__ super(Connection, self).__init__(*args, **kwargs2) django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")

Here are our relevant code snippets:

app.yaml:

# [START django_app]
runtime: custom
env: flex
handlers:
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
  static_dir: static/
# This handler routes all requests not caught above to the main app.
# It is required when static routes are defined, but can be omitted
# (along with the entire handlers section) when there are no static
# files defined.
- url: /.*
  script: auto
# [END django_app]

Dockerfile:

FROM gcr.io/google-appengine/python

# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
# Use -p python3 or -p python3.7 to select python version. Default is version 2.
RUN apt-get update
RUN apt-get install poppler-utils --assume-yes
RUN virtualenv -p python3.7 /env

# Setting these environment variables are the same as running
# source /env/bin/activate.
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

# Copy the application's requirements.txt and run pip to install all
# dependencies into the virtualenv.
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

# Add the application source code.
ADD . /app

# Run a WSGI server to serve the application. gunicorn must be declared as
# a dependency in requirements.txt.
CMD gunicorn -b :$PORT main:app

settings.py:

# STATIC_URL = 'https://storage.googleapis.com/tulaibucket/static/'

if os.getenv('GAE_APPLICATION', None):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': ‘MYHOST’,
            'USER': ‘MYUSER’,
            'PASSWORD': ‘MYPASSWORD’,
            'NAME': ‘DBNAME’,
        }
    }
else:
    # Running in development, so use a local MySQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '127.0.0.1',
            'PORT': '3306',
            'NAME’: ‘DBNAME’,
            'USER': ‘MYUSER’,
            'PASSWORD': ‘MYPASSWORD’,
        }
    }

You have a problem with the quotes at settings.py , the file should look like this:

# STATIC_URL = 'https://storage.googleapis.com/tulaibucket/static/'

if os.getenv('GAE_APPLICATION', None):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'MYHOST',
            'USER': 'MYUSER',
            'PASSWORD': 'MYPASSWORD',
            'NAME': 'DBNAME',
        }
    }
else:
    # Running in development, so use a local MySQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '127.0.0.1',
            'PORT': '3306',
            'NAME': 'DBNAME',
            'USER': 'MYUSER',
            'PASSWORD': 'MYPASSWORD',
        }
    }

GAE_APPLICATION variable is not set for GAE Flexible. Use GAE_INSTANCE instead.

For production you need to use the following in app.yaml:

beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM