SQLAlchemy可以与Google Cloud SQL一起使用吗? [英] Can SQLAlchemy be used with Google Cloud SQL?

查看:225
本文介绍了SQLAlchemy可以与Google Cloud SQL一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

Google Cloud SQL文档:
https://developers.google.com/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工作站,你可能想看看我的答案在这里 - https://stackoverflow.com/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. https://developers.google.com/cloud-sql/docs/django

Google Cloud SQL documentation: https://developers.google.com/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 - https://stackoverflow.com/a/14287158/191902.

这篇关于SQLAlchemy可以与Google Cloud SQL一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆