简体   繁体   English

create_engine问题mysql 5.7和sqlalchemy

[英]create_engine problems mysql 5.7 and sqlalchemy

I've inherited an application making use of python & sqlalchemy to interact with a mysql database. 我继承了一个使用python和sqlalchemy与mysql数据库交互的应用程序。 When I issue: 当我发出:

mysql_engine = sqlalchemy.create_engine('mysql://uname:pwd@192.168.xx.xx:3306/testdb', connect_args={'use_unicode':True,'charset':'utf8', 'init_command':'SET NAMES UTF8'}, poolclass=NullPool)

, at startup, an exception is thrown: ,在启动时,抛出异常:

cmd = unicode("USE testdb")    
with mysql_engine.begin() as conn:
      conn.execute(cmd)

sqlalchemy.exc.OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '192.168.xx.xx' (101)") None None

However, using IDLE I can do: 但是,使用IDLE,我可以这样做:

>>> import MySQLdb
>>> Con = MySQLdb.Connect(host="192.168.xx.xx", port=3306, user="uname", passwd="pwd", db="testdb")
>>> Cursor = Con.cursor()
>>> sql = "USE testdb"
>>> Cursor.execute(sql)

The application at this point defaults to using an onboard sqlite database. 此时应用程序默认使用板载sqlite数据库。 After this I can quite happily switch to the MySQL database using the create_engine statement above. 在此之后,我可以使用上面的create_engine语句切换到MySQL数据库。 However, on reboot the MySQL database connection will fail again, defaulting to the onboard sqlite db, etc, etc. 但是,在重新启动时,MySQL数据库连接将再次失败,默认为板载sqlite db等,等等。

Has anyone got any suggestions as to how this could be happening? 有没有人有任何关于如何发生这种情况的建议?

Just thought I would update this - the problem still occurs exactly as described above. 只是想我会更新这个 - 问题仍然完全如上所述。 I've updated the app so that the user can manually connect to the MySQL db by selecting a menu option. 我已经更新了应用程序,以便用户可以通过选择菜单选项手动连接到MySQL数据库。 This calls the identical code which exceptions when the app is starting, but works just fine once the app is up and running. 这会调用应用程序启动时异常的相同代码,但是一旦应用程序启动并运行,它就可以正常工作。

The MySQL instance is completely separate from the app and running throughout, so it should be available to receive connections at all times. MySQL实例完全独立于应用程序并在整个运行中运行,因此它应始终可用于接收连接。

I guess the fundamental question i'm grappling with is how can the same connect code work when the app is up and running, but throw an exception when it is starting? 我想我正在努力解决的基本问题是,当应用程序启动并运行时,相同的连接代码如何工作,但在启动时抛出异常?

Is there any artifact of SQLAlchemy that can cause it to fail to create usable connections that isn't dependant on the connection parameters or the remote database? 是否有任何SQLAlchemy工件可能导致它无法创建不依赖于连接参数或远程数据库的可用连接?

Ahhh, it all seems so obvious now... 啊,现在这一切似乎都很明显......

The reason for the exception on startup was because the network interface hadn't finished configuring when the application would make its first request to the remote database. 启动时出现异常的原因是因为当应用程序向远程数据库发出第一个请求时,网络接口尚未完成配置。 (Which is why the same thing would be successful when attempted at a later time). (这就是为什么在以后尝试同样的事情会成功的原因)。

As communication with the remote database is a prerequisite for the application, I now do something like this: 由于与远程数据库的通信是应用程序的先决条件,我现在做这样的事情:

if grep -Fxq "mysql" /path/to/my/db/config.config
then
    while ! ip a | grep inet.*wlan0 ; do sleep 1; echo "waiting for network..."; done;
fi

... in the startup script for my application - ensuring that the network interface has finished configuring before the application can run. ...在我的应用程序的启动脚本中 - 确保在应用程序运行之前网络接口已完成配置。

Of course, the application will never run if the interface doesn't configure, so it still needs some finessing to allow it to timeout and default to using a local database... 当然,如果接口没有配置,应用程序将永远不会运行,因此它仍然需要一些功能来允许它超时并默认使用本地数据库...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 google cloud function - mysql 和 sqlalchemy create_engine 配置中的良好实践 - google cloud function - mysql and sqlalchemy good practice in create_engine configuration 传递给 mysql AWS RDS 的 sqlalchemy create_engine() 的正确连接字符串 - Proper connection string to pass to sqlalchemy create_engine() for mysql AWS RDS SQLAlchemy create_engine 在 Google Cloud Function 中不起作用 - SQLAlchemy create_engine not working in Google Cloud Function 如何在SQLAlchemy的`create_engine`中使用`charset`和`encoding`(创建pandas数据帧)? - how to use `charset` and `encoding` in `create_engine` of SQLAlchemy (to create pandas dataframe)? kaggle notebook 上的 sqlalchemy 库函数 create_engine().execute() 出错 - Error on the function create_engine().execute() from sqlalchemy library on kaggle notebook 为什么SQLAlchemy create_engine与charset = utf8返回python类型 <str> 而不是打字 <unicode> ? - Why does SQLAlchemy create_engine with charset=utf8 return python type <str> and not type <unicode>? 在MYSQL 5.7中创建表时出现问题 - Problems Creating tables in MYSQL 5.7 带有 Mysql 5.7 SqlAlchemy 的 Flask 格式错误的数据包 - Flask Malformed packet with Mysql 5.7 SqlAlchemy NameError:未定义全局名称&#39;create_engine&#39;[尝试创建SQLAlchemyJobStore时] - NameError: global name 'create_engine' is not defined [when trying to create a SQLAlchemyJobStore] 无法在 MySql 5.7 中创建程序 - Cant create procedure in MySql 5.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM