How to connect to google cloud sql with flexible env using python 3.5 on windows after app deployment?

86 views
Skip to first unread message

Rajan Subramanian

unread,
Mar 16, 2018, 4:34:57 PM3/16/18
to Google App Engine
Hello, I asked this question in stack overflow, but haven't gotten any replies although it had 40 views.  I rephrased the question as simple as I possibly could.  I deployed an app on google app engine.  This is my requirement.  A user comes to the url in app spot: project_id.appspot.com/search_form, and types his information into the input boxes i created using html files.  Once this information is entered, a query occurs on google cloud sql that would return the required info.  The required info is posted into a new url: ... /search/  In my case, for example, the required information,  lets say is historical product demand.  I created python functions that successfully query the required info using a Django web framework.  The functions are stored in views.py.  One function is get_cust_info(), that takes the input given by the user when he enters to my website.  The error I see occurring after deploying my app is: 



OperationalError at /search/

    (20033), "Can't connect to MySQL server on 127.0.0.1 (timed out))

    Request Method:  GET

    Django Version: 2.0.3
    Exception Type: OperationalError
    Exception Location: /env/lib/python3.5/site-packages/pymysql/connections.py in connect, line 967
    Python Path:
    ['/home/vmagent/app',
     '/env/bin',
     '/env/lib/python35.zip',
     '/env/lib/python3.5',
     '/env/lib/python3.5/plat-linux',
     '/env/lib/python3.5/lib-dynload',
     '/opt/python3.5/lib/python3.5',
     '/opt/python3.5/lib/python3.5/plat-linux',
     '/env/lib/python3.5/site-packages']
    
    HTTP_ACCEPT
    'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    HTTP_ACCEPT_ENCODING
    'br, gzip, deflate'
    HTTP_ACCEPT_LANGUAGE
    'en-us'
    HTTP_HOST
    HTTP_USER_AGENT
      ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6       (KHTML, ''like Gecko) Version/11.0.3 Safari/604.5.6')
    HTTP_VIA
    '1.1 google'
    HTTP_X_APPENGINE_CITY
    'mount laurel'
    HTTP_X_APPENGINE_CITYLATLONG
    '39.934002,-74.890999'
    HTTP_X_APPENGINE_COUNTRY
    'US'
    HTTP_X_APPENGINE_REGION
    'nj'
    HTTP_X_CLOUD_TRACE_CONTEXT
    '9b87c05701b0b57a935ad234a137d9b3/6687249234366022806'
    HTTP_X_FORWARDED_FOR
    '50.254.95.49, 172.217.3.116'
    HTTP_X_FORWARDED_PROTO
    'https'
    PATH_INFO
    '/search/'
    QUERY_STRING
    'fname=james&mname=g&lname=stahl&street_num=1045&street_name=beck&street_type=rd&state=OH&zip=48277'
    RAW_URI
    '/search/?

fname=james&mname=g&lname=stahl&street_num=1045&street_name=beck&street_typ    e=rd&state=OH&zip=48277'
    REMOTE_ADDR
    '172.17.0.3'
    REMOTE_PORT
    '55440'
    REQUEST_METHOD
    'GET'
    SCRIPT_NAME
    ''
    SERVER_NAME
    '0.0.0.0'
    SERVER_PORT
    '8080'
    SERVER_PROTOCOL
    'HTTP/1.1'
    SERVER_SOFTWARE
    'gunicorn/19.6.0'
      gunicorn.socket
       <socket.socket fd=10, family=AddressFamily.AF_INET type=SocketKind.SOCK_STREAM, proto=0, laddr=('172.17.0.6', 8080), raddr= ('172.17.0.3', 55440)>
    wsgi.errors
    <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f51b0bbfcf8>
    wsgi.file_wrapper
    ''
    wsgi.input
    <gunicorn.http.body.Body object at 0x7f51b0bbfda0>
    wsgi.multiprocess
    False
    wsgi.multithread
    False
    wsgi.run_once
    False
    wsgi.url_scheme
    'http'
    wsgi.version
    (1, 0)

To repeat, I deployed an app successfully following this link: 


After deployment, I am having trouble connecting to the cloud sql.  In my ipython notebook, before I deploy my app, I can use the following statement to connect to my cloud instance using google sdk: 

    cloud_sql_proxy.exe -instances="scoviileapp:us-east1:retail18"=tcp:3306

After entering above, i get a notification in my google cloud shell 

    "listening on 127.0.0.1:3306 for scoviileapp:us-east1:retail18 
    ready for new connections"

I then use my ipython notebook to test connection (password and user changed for example purposes):

    host = '127.0.0.1' also changed to 35.196.etc
    user = 'me'
    password = 'pwd'

    conn1 = pymysql.connect(host=host,
                             user=user,
                             password=password,
                             db='retail_data')
    cur1 = conn1.cursor()



Hence, I am able to connect to google cloud sql.  I ran python manage.py run server to make sure it works on my end, and it does.  Then i typed:

    gcloud app deploy

it successfully deploys the app.  But upon, going to my app, and typing in names into the input field, it takes me to a new url and i get the error


Im using windows, python 3, django framework.  Is gunicorn even needed?  I want to be able to query the databases in google cloud databases.  

Katayoon (Cloud Platform Support)

unread,
Mar 19, 2018, 6:20:46 PM3/19/18
to Google App Engine

Connecting to Cloud SQL from External Applications, you may follow the steps here and set the invocation statement as this for Python.


Most web applications use a WSGI server such as Gunicorn, uWSGI or Waitress. Gunicorn is the recommended WSGI server, but it's completely possible to use any other WSGI server. Before you can use one of these servers, you must add them as a dependency in your application's requirements.txt. The runtime ensures that all dependencies are installed before your entrypoint is called.


- Note that Google Groups is reserved for general product discussions and not for technical support. For specific technical questions that do not relate to a bug or a feature request, I recommend that you continue the posts you have already opened in Stack Overflow and Serverfault.


Reply all
Reply to author
Forward
0 new messages