Search code examples
pythonflasksqlalchemyflask-sqlalchemy

pymysql.err.OperationalError 2006 on a flask app


hello i have a flask app that running on a cplanel host and i use flask sqlalchemy i face this erorr every time i want tp update something in the website:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has 
gone away (BrokenPipeError(32, 'Broken pipe'))")
[SQL: SELECT user.id AS user_id, user.is_admin AS user_is_admin, user.is_owner AS 
user_is_owner, user.email AS user_email, user.username AS user_username, user.password 
AS user_password, user.confirmed AS user_confirmed, user.active AS user_active, 
user.reminder AS user_reminder, user.image AS user_image, user.rank AS user_rank, 
user.joined_date AS user_joined_date, user.last_login AS user_last_login, 
user.email_confirmed_at AS user_email_confirmed_at, user.api_use AS user_api_use, 
user.total_api_use AS user_total_api_use, user.daily_api_call AS user_daily_api_call 
FROM user 
WHERE user.id = %(pk_1)s]
[parameters: {'pk_1': '1'}]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

this is my flask app config :

class Config:
    DEBUG = False
    TESTING = False
    SESSION_COOKIE_SECURE = True
    SQLALCHEMY_DATABASE_URI = 
"mysql+pymysql://favorit1_root:ug%qdnd4[maV@localhost:3306/favorit1_database"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    MONGO_URI = 'mongodb+srv://********'
    FLASK_ADMIN_SWATCH = 'cerulean'
    BASIC_AUTH_USERNAME = '*****'
    BASIC_AUTH_PASSWORD = '*****'
    JSON_AS_ASCII = True
    JSON_SORT_KEYS = True
    PROPAGATE_EXCEPTIONS = True
    SQLALCHEMY_POOL_RECYCLE = 50 

    IJSONFY_MIMETYPE = 'application/json'

    SECRET_KEY = "b'\xf1L\xdb5D\x96M\xe5\xd1\x9f\x16+\xf4%A\x1e\xb9\xafm\xb8g\x1ct\x0e'"
    MAIL_SERVER = "*******"
    HOST_NAME = "*****"
    MAIL_USERNAME = "*******"
    MAIL_PASSWORD = "*******"
    MAIL_PORT = 465
    MAIL_DEBUG = False
    MAIL_USE_SSL = True
    MAIL_USE_TSL = False
    REMEMBER_COOKIE_DURATION = timedelta(days=1.0)
    UPLOADS_PATH_BLOG = join(dirname(realpath(__file__)), 'static/upload/blog/')
    UPLOADS_PATH_CAFE = join(dirname(realpath(__file__)), 'static/upload/cafe/')
    UPLOADS_PATH_USER = join(dirname(realpath(__file__)), 'static/upload/user/')
    NEWS_PATH = join(dirname(realpath(__file__)), 'static/news/')

what is my problem and how can fix this ?


Solution

  • after reading sqlalchemy Dcouments i slove my problem and i write it here maybe someoen in future face my problem : sqlalchemy doc

    Dealing with Disconnects:

    The connection pool has the ability to refresh individual connections as well as its entire set of connections, setting the previously pooled connections as “invalid”. A common use case is allow the connection pool to gracefully recover when the database server has been restarted, and all previously established connections are no longer functional. There are two approaches to this.

    i set :

    SQLALCHEMY_POOL_PRE_PING = True
    

    ion my config class of my flask app and this fix my problem