简体   繁体   English

连接到Google Cloud SQL时出现内部错误

[英]internal error when connecting to google cloud SQL

I designed a simple website using Flask and my goal was to deploy it on Google App engine. 我使用Flask设计了一个简单的网站,我的目标是将其部署在Google App引擎上。 I started working on it locally and used google cloud sql for the database. 我开始在本地进行处理,并将Google Cloud sql用于数据库。 I used google_cloud_proxy to open the port 3306 to interact with my GC SQL instance and it works fine locally... this is the way I'm connecting my application to GC SQL: 我使用google_cloud_proxy打开端口3306与我的GC SQL实例进行交互,并且在本地运行良好...这是将应用程序连接到GC SQL的方式:

I have a app.yaml file which I've defined my Global Variables in it: 我有一个app.yaml文件,其中定义了全局变量:

env_variables:
   CLOUDSQL_SERVER = '127.0.0.1'
   CLOUDSQL_CONNECTION_NAME = "myProjectName:us-central1:project"`
   CLOUDSQL_USER = "user"
   CLOUDSQL_PASSWORD = "myPassword"
   CLOUDSQL_PORT = 3306
   CLOUDSQL_DATABASE = "database"

and from my local machine I do: 从我的本地计算机执行以下操作:

db = MySQLdb.connect(CLOUDSQL_SERVER,CLOUDSQL_USER,CLOUDSQL_PASSWORD,CLOUDSQL_DATABASE,CLOUDSQL_PORT)

and if I want to get connected on App Engine, I do: 如果我想在App Engine上建立连接,请执行以下操作:

cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)
db = MySQLdb.connect(unix_socket=cloudsql_unix_socket,user=CLOUDSQL_USER,passwd=CLOUDSQL_PASSWORD,db=CLOUDSQL_DATABASE)

the static part of the website is running but for example, when I want to login with a username and password which is stored in GC SQL, I receive an internal error. 网站的静态部分正在运行,但是例如,当我想使用存储在GC SQL中的用户名和密码登录时,出现内部错误。

I tried another way... I started a compute engine, defined my global variables in config.py, installed flask, mysqldb and everything needed to start my application. 我尝试了另一种方法...我启动了计算引擎,在config.py中定义了全局变量,安装了flask,mysqldb以及启动应用程序所需的所有东西。 I also used cloud_sql_proxy on that compute engine and I tried this syntax to connect to GC SQL instance: 我还在该计算引擎上使用了cloud_sql_proxy,并尝试使用以下语法连接到GC SQL实例:

db = MySQLdb.connect(CLOUDSQL_SERVER,CLOUDSQL_USER,CLOUDSQL_PASSWORD,CLOUDSQL_DATABASE,CLOUDSQL_PORT)

but it had the same problem. 但是它有同样的问题。 I don't think that it's the permission issue as I defined my compute engine's ip address in the authorized network part of GC SQL and in I AM & ADMIN part, the myprojectname@appspot.gserviceaccount.com has the Editor role! 我认为这不是权限问题,因为我在GC SQL的授权网络部分以及I AM&ADMIN部分中定义了计算引擎的ip地址,而myprojectname@appspot.gserviceaccount.com具有编辑者角色! can anyone help me out where the problem is? 谁能帮助我解决问题所在?

ALright! 好的! I solved the problem. 我解决了问题。 I followed the Google cloud's documentation but I had problems.I added a simple '/' in: 我遵循了Google云的文档,但是遇到了问题,我在以下位置添加了一个简单的'/':

cloudsql_unix_socket = os.path.join('/cloudsql', CLOUDSQL_CONNECTION_NAME)

instead of '/cloudsql' it should be '/cloudsql/' 而不是'/cloudsql'它应该是'/cloudsql/'

I know it's weird because os.path.join must add '/' to the path but for strange reasons which I don't know, it wasn't doing so. 我知道这很奇怪,因为os.path.join必须在路径中添加“ /”,但是出于我不知道的奇怪原因,它没有这样做。

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

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