SQLAlchemy可以与Google Cloud SQL一起使用吗?

编程入门 行业动态 更新时间:2024-10-27 02:24:16
本文介绍了SQLAlchemy可以与Google Cloud SQL一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我查看了Google Cloud SQL的文档和各种搜索,但是我无法确定是否可以将SQLAlchemy与Google Cloud SQL配合使用,如果是这样,连接URI应该是什么。

我正在使用Flask-SQLAlchemy扩展并需要连接字符串,如下所示: mysql:// username:password @ server / db

我看到了Django示例,但看起来配置使用的是与连接字符串不同的样式。 developers.google/cloud-sql/docs/django

Google Cloud SQL文档: developers.google/cloud-sql/docs/developers_guide_python

解决方案

Update

Google Cloud SQL现在支持直接访问,所以 MySQLdb 方言现在可以用过的。通过mysql方言建议的连接使用URL格式:

mysql + mysqldb:// root @ /< dbname> ?unix_socket = / CLOUDSQL /<专案编号>:其中实例名>在SQLAlchemy中已弃用

mysql + gaerdbms 从1.0版开始

如果其他人仍然觉得有帮助,我会在下面留下原始答案。

对于稍后访问此问题的人(并且不想阅读所有注释),S​​QLAlchemy现在支持使用连接字符串/ dialect(版本0.7.8)的Google Cloud SQL请参阅:文档) :

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生产实例(例如开发工作站)连接到Cloud SQL。通过将项目和实例作为字符串的一部分包含进去,并且不需要通过 connect_args 单独传递,该更改也会稍微修改连接字符串。

Eg

mysql + gaerdbms:///< dbname>?instance =< project :实例>

这也将使您可以更轻松地将Cloud SQL与Flask-SQLAlchemy或其他扩展模块一起使用, t明确地调用 create_engine()。

如果您在开发过程中无法连接到Google Cloud SQL工作站,你可能想看看我的答案在这里 - stackoverflow/a/14287158/191902。

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.

I'm looking to use the Flask-SQLAlchemy extension and need the connection string like so: mysql://username:password@server/db

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 documentation: developers.google/cloud-sql/docs/developers_guide_python

解决方案

Update

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 has been deprecated in SQLAlchemy since version 1.0

I'm leaving the original answer below in case others still find it helpful.

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>

E.g.:

create_engine('mysql+gaerdbms:///mydb', connect_args={"instance":"myinstance"})

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.

E.g.

mysql+gaerdbms:///<dbname>?instance=<project:instance>

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.

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一起使用吗?

本文发布于:2023-10-16 11:40:48,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Google   SQLAlchemy   SQL   Cloud

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!