我查看了 Google Cloud SQL 的文档和各种搜索,但我不知道是否可以将 SQLAlchemy 与 Google Cloud SQL 一起使用,如果可以,连接 URI 应该是什么.
I've looked over Google Cloud SQL's documentation and various searches, but I can't find out whether it is possible to use SQLAlchemy with Google Cloud SQL, and if so, what the connection URI should be.
我希望使用 Flask-SQLAlchemy 扩展并需要像这样的连接字符串:mysql://username:password@server/db
I'm looking to use the Flask-SQLAlchemy extension and need the connection string like so: mysql://username:password@server/db
我看到了 Django 示例,但似乎配置使用了与连接字符串不同的样式.developers.google/cloud-sql/docs/django
I saw the Django example, but it appears the configuration uses a different style than the connection string. developers.google/cloud-sql/docs/django
Google Cloud SQL 文档:developers.google/cloud-sql/docs/developers_guide_python
Google Cloud SQL documentation: developers.google/cloud-sql/docs/developers_guide_python
推荐答案更新
Google Cloud SQL 现在支持直接访问,因此现在可以使用 MySQLdb 方言.通过 mysql 方言的推荐连接使用 URL 格式:
Google Cloud SQL now supports direct access, so the MySQLdb dialect can now be used. The recommended connection via the mysql dialect is using the URL format:
mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>mysql+gaerdbms 自 1.0 版起在 SQLAlchemy 中已被弃用
我将在下面留下原始答案,以防其他人仍然觉得它有帮助.
I'm leaving the original answer below in case others still find it helpful.
对于那些稍后访问此问题(并且不想通读所有评论)的人,SQLAlchemy 现在支持使用连接字符串/方言的 Google Cloud SQL 版本 0.7.8(请参阅:文档):
For those who visit this question later (and don't want to read through all the comments), SQLAlchemy now supports Google Cloud SQL as of version 0.7.8 using the connection string / dialect (see: docs):
mysql+gaerdbms:///<dbname>例如:
create_engine('mysql+gaerdbms:///mydb', connect_args={"instance":"myinstance"})我已经向 mysql+gaerdmbs://提出了一个更新 方言支持 Google Cloud SQL API(rdbms_apiproxy 和 rdbms_googleapi)从非 Google App Engine 生产实例(例如您的开发工作站).更改还将通过将项目和实例作为字符串的一部分包含在内来稍微修改连接字符串,而不需要通过 connect_args 单独传递.
I have proposed an update to the mysql+gaerdmbs:// dialect to support both of Google Cloud SQL APIs (rdbms_apiproxy and rdbms_googleapi) for connecting to Cloud SQL from a non-Google App Engine production instance (ex. your development workstation). The change will also modify the connection string slightly by including the project and instance as part of the string, and not require being passed separately via connect_args.
例如
mysql+gaerdbms:///<dbname>?instance=<project:instance>这还可以让您更轻松地将 Cloud SQL 与 Flask-SQLAlchemy 或其他扩展程序一起使用,您无需明确地进行 create_engine() 调用.
This will also make it easier to use Cloud SQL with Flask-SQLAlchemy or other extension where you don't explicitly make the create_engine() call.
如果您在从开发工作站连接到 Google Cloud SQL 时遇到问题,您可能需要在这里查看我的回答 - stackoverflow/a/14287158/191902.
If you are having trouble connecting to Google Cloud SQL from your development workstation, you might want to take a look at my answer here - stackoverflow/a/14287158/191902.
更多推荐
SQLAlchemy 可以与 Google Cloud SQL 一起使用吗?
发布评论