Is it possible to use Mysql with SqlAlchemy and Flask if my mysql socket isn't in /tmp?

后端 未结 3 1793
遇见更好的自我
遇见更好的自我 2020-12-16 13:07

The location for mysql.sock on my system is /usr/local/mysql5/mysqld.sock

thrilllap-2:tmp reuven$ mysqld --print-defaults
mysqld would have          


        
相关标签:
3条回答
  • 2020-12-16 13:48

    You could create the conexión using

    sqlalchemy.create_engine(
            mysql_str = sqlalchemy.engine.url.URL(
                drivername='mysql+pymysql',
                username="db_user",
                password="db_pass",
                database=db_name,
                query={
                    'unix_socket': '/usr/local/mysql5/mysqld.sock'
                }
            )
    )
    
    0 讨论(0)
  • 2020-12-16 14:00

    You'll have to dig up the exact syntax, but for MySQL I think they use a unix_socket query opt. Something like:

    mysql:///dbname?unix_socket=/opt/mysql/mysql.sock'
    

    Should be your connect URI for SQLAlchemy.

    0 讨论(0)
  • 2020-12-16 14:10

    Yes! Sean was right

    app.config['SQLALCHEMY_DATABASE_URI'] = ''mysql://dayenu:secret.word@localhost/dayenu?unix_socket=/usr/local/mysql5/mysqld.sock
    db = SQLAlchemy(app)
    

    works fine! I think this parameter is used by pyodbc, which is what SQLAlchemy uses to talk to mysql, but I couldn't find this parameter anywhere in the pyodbc documentation.

    0 讨论(0)
提交回复
热议问题