How to connect to mysql server with SSL from a flask app

前端 未结 3 826
花落未央
花落未央 2020-12-20 19:26

I want to connect to a mysql server via flask and

app.config[\'SQLALCHEMY_DATABASE_URI\'] = \'mysql://your-username:your-password@localhost/schema\'
<         


        
3条回答
  •  死守一世寂寞
    2020-12-20 20:04

    Another solution is to use sqlalchemy.engine.url.URL to define the URL.

    sqlUrl = sqlalchemy.engine.url.URL(
        drivername="mysql+pymysql",
        username=db_user,
        password=db_pass,
        host=db_host,
        port=3306,
        database=db_name,
        query={"ssl_ca": "main_app/certs/BaltimoreCyberTrustRoot.crt.pem"},
    )
    create_engine(sqlUrl)
    

    You can include SSL parameters as a dictionary in the query argument.

    This approach is useful if you are using Flask to initialize the SqlAlchemy engine with a config parameter like SQLALCHEMY_DATABASE_URI rather than directly using create_engine.

提交回复
热议问题