简体   繁体   中英

Obtain server error for Flask development server when deployed on Gcloud but works fine as local server due to permission

Error: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) attempt to write a readonly database 2019-12-17 10:12:11 default[20191217t021035] [SQL: INSERT INTO post (title, date_posted, content, user_id) VALUES (?, ?, ?, ?)] 2019-12-17 10:12:11 default[20191217t021035] [parameters: ('hi', '2019-12-17 10:12:11.435775', 'hi', 3)] 2019-12-17 10:12:11 default[20191217t021035] (Background on this error at: http://sqlalche.me/e/e3q8 )

app.yaml

runtime: python37
env: standard
handlers:
- url: /main.css
  static_dir: static/*
  expiration: "1h"
runtime_config:
  python_version: 3.7
  # This configures Google App Engine to serve the files in the app's static
  # directory.

#beta_settings:
#  cloud_sql_instances: flask-journal:us-west1:flask-project1

entrypoint: gunicorn -b :$PORT run:app 

config.py

import os

class Config:
    SECRET_KEY = '[a correct key]'
    SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'

    # E-Mail with Flask
    MAIL_SERVER = 'smtp.googlemail.com'
    MAIL_PORT = 587
    MAIL_USER_SSL = True
    # Need to set environment variable
    MAIL_USERNAME = os.environ.get('EMAIL_USER') 
    MAIL_PASSWORD = os.environ.get('EMAIL_PW')
    MAIL_DEFAULT_SENDER = os.environ.get('EMAIL_USER') 

Then I also have a site.db where the database is. After using

gcloud app deploy

The website hosted, but I am not able to engage in any operation that would change the database. I am almost sure it is a permission error given the error output shown above. I saw solutions you can do chmod on Linux, but what do you do on Windows 10? I guess the "user" changing the database is now no longer permitted (even logged on) to write to the database when doing

db.commit()

The file system of the Google Cloud runtime is read-only ( https://cloud.google.com/appengine/docs/standard/python3/runtime#filesystem ):

The runtime includes a full filesystem. The filesystem is read-only except for the location /tmp , which is a virtual disk storing data in your App Engine instance's RAM.

  • So in theory you should be able to store the file inside /tmp (note four slashes in the SQLAlchemy DB URI for an absolute file path):
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/site.db'
  • But this means that a) the database will disappear every time your instance stops b) If you ever scale your application to more than one instance, each instance will have its own database.
  • I suggest you have a look Postgresql on Google's Cloud SQL, see https://cloud.google.com/sql/docs/postgres/connect-app-engine for how to connect from app engine to a Google Cloud database server. The config will then change to something like:
SQLALCHEMY_DATABASE_URI = 'postgres://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432'

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