简体   繁体   English

Google Cloud SQL-操作系统环境未设置为Google App Engine

[英]Google Cloud SQL - OS environment not set to Google App Engine

I am attempting to connected to a Google Cloud SQL instance in python and have gone through google's tutorial: https://cloud.google.com/appengine/docs/python/cloud-sql/ 我正在尝试使用python连接到Google Cloud SQL实例,并经历了Google教程: https : //cloud.google.com/appengine/docs/python/cloud-sql/

I am essentially cloning google's tutorial code and for some reason this line isn't working right for me: 我本质上是在克隆Google的教程代码,由于某种原因,此行对我而言不起作用:

if (os.getenv('SERVER_SOFTWARE') and
  os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):

This if statement is not being entered and I'm not sure why - it is then defaulting to accessing a local database based on the else statement. 如果没有输入if语句,我不确定为什么-然后默认基于else语句访问本地数据库。 How is the os server_software environment set? os server_software环境如何设置? I'm new to all of this but basically because that is not getting set, I am not able to access my google cloud sql instance. 我是所有这些的新手,但基本上是因为尚未设置,所以我无法访问我的Google Cloud sql实例。 How do I make sure this if statement is entered? 我如何确定是否输入了if语句?

SERVER_SOFTWARE is an environment variable that is automatically set by GAE. SERVER_SOFTWARE是由GAE自动设置的环境变量。 It could either be something like Google App Engine/xxxx when deployed or Development/xx when running locally. 部署时可能类似于Google App Engine/xxxx ,而在本地运行时可能类似于Development/xx

Basically the section of the code you're referring to checks whether your app is deployed and is running on GAE servers and if so - it will connect to a Google Cloud SQL instance, otherwise, if your app is running locally, it will attempt to connect to a local mysql instance. 基本上,您所指代的代码部分将检查您的应用是否已部署并正在GAE服务器上运行,如果可以,它将连接到Google Cloud SQL实例,否则,如果您的应用在本地运行,则将尝试连接到本地mysql实例。

It's done that way because you wouldn't normally want to mess with your production (deployed) data while developing & testing locally as many thing could go wrong. 之所以这样做,是因为在进行本地开发和测试时,通常您不希望弄乱您的生产(部署)数据,因为很多事情都可能出错。

Since you're stating that the if statement is not being entered - it's safe to assume that you are trying to run the program locally but are expecting it to connect to a Google Cloud SQL instance, for that, the next few lines in the example you provided are explaining how to do it: 由于您要说明的是未输入if语句,因此可以安全地假设您尝试在本地运行该程序,但希望该程序连接到Google Cloud SQL实例,为此,示例中的以下几行您提供的说明操作方法:

db = MySQLdb.connect(host='127.0.0.1', port=3306, db='guestbook', user='root', charset='utf8')
# Alternatively, connect to a Google Cloud SQL instance using:
# db = MySQLdb.connect(host='ip-address-of-google-cloud-sql-instance', port=3306, user='root', charset='utf8')

so what you need to do is comment out the first line (the one it attempts to connect to a localhost mysql server) and uncomment the one where it connects to the Google Cloud SQL instance (note that you will have to update several parameters that reflect the configuration that you have, ie the host parameter and possible others). 因此,您需要做的是注释掉第一行(它尝试连接到本地mysql服务器的那一行),并取消注释它连接到Google Cloud SQL实例的那一行(请注意,您将必须更新一些参数来反映您所拥有的配置,即host参数和其他可能的参数)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM