我有一个flask应用程序,我需要使用sshtunnel连接到一个遥远的mysqldb,就像我的config.py文件i init in my init.py文件中的sshtunnel:
sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0
server = sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com', 22),
ssh_password="mypassword",
ssh_username="myusername",
remote_bind_address=(myname.mysql.pythonanywhere-services.com', 3306))
server.start()
engine = create_engine('mysql+mysqldb://mynameb:dbpassword@127.0.0.1:%s/dbname' % server.local_bind_port)
连接似乎在工作,但我无法从迁移升级我的数据库(flask db upgrade),因为我没有使用sqlalchemy\u database\u uri连接到我的数据库。是否还有一种方法可以使数据库升级与数据库的ssh连接一起工作?
1条答案
按热度按时间rm5edbpk1#
alembic不必使用数据库uri连接到数据库来运行升级。在你的
alembic\env.py
有这样一个函数:重要的是变量
connectable
是一个engine
alembic调用时的示例connect()
在上面。因此,您可以这样做(这不是测试,但我自己也做类似的事情):
理想情况下,您应该将所有的连接逻辑放在一个可以从
alembic/env.py
在项目中只定义一次,然后就可以导入engine
直接进入env.py
,但你明白了。