0

I designed a simple website using Flask and my goal was to deploy it on Google App engine. I started working on it locally and used google cloud sql for the database. 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:

I have a app.yaml file which I've defined my Global Variables in it:

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:

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.

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. I also used cloud_sql_proxy on that compute engine and I tried this syntax to connect to GC SQL instance:

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! can anyone help me out where the problem is?

1 Answers1

0

ALright! I solved the problem. I followed the Google cloud's documentation but I had problems.I added a simple '/' in:

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

instead of '/cloudsql' it should be '/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.

  • edit: actually, I can't reproduce the problem: it appears that `os.path.join` correctly adds the '/' for you: >>> os.path.join('/cloudsql', 'bananana:asdf:asdf') '/cloudsql/bananana:asdf:asdf' Are you sure that adding the '/' is the only thing you did to fix your issue? – Kevin Malachowski Jul 20 '17 at 18:57
  • I was thinking the same for a few days and I thought my problem is not in that part because `os.path.join` adds '/' at the end. but then I saw a project on github which they used `'/cloudsql/{}'.format(connection_name)`. I then thought maybe os.path.join is not adding '/' to the path. I added '/' and problem solved! I know it's kind of weird. – Amin Beirami Jul 20 '17 at 19:44