Vishal Soriya
Vishal Soriya

Reputation: 23

Connecting with mysql server running on Compute Engine instance from GAE

How to communicate with the mysql server running on Compute Engine instance from google app engine? We are using google app engine as frontend. We want to host our database on the mysql server running on Compute Engine. Is there any way to achieve this?

We have gone through this: https://cloud.google.com/solutions/mysql-remote-access

Code snippet:

if (os.getenv('SERVER_SOFTWARE') and
  os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
  db=MySQLdb.connect(host="InternalIP", port=3306, db='test_database', user='root',passwd="db_password")
else:
  db = MySQLdb.connect(host='127.0.0.1', port=3306, db='test_database', user='root', charset='utf8')

cursor = db.cursor()
logging.info("Hey there looks like I am connected")

Upvotes: 2

Views: 921

Answers (2)

E. Anderson
E. Anderson

Reputation: 3493

Igor's comment above hints at how to get this working; I've managed to produce a working app, with the following changes to the documented solution.

  1. Specify a public (external) IP address for the host parameter, rather than unix_socket.
  2. Replace MySQLdb with pymysql. In particular, you want to copy the pymysql directory from that GitHub repo into your application directory. Using pymysql also means that you don't need to add MySQLdb to the libraries: section of your app.yaml.
  3. In connections.py, around line 52, change the following line:

    if _py_version == (2, 7) and not IRONPYTHON
    

    to

    if _py_version == (2, 7) and not IRONPYTHON and not os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    
  4. You may need to change your MySQL permission grants in mysql to allow access from the App Engine source IP addresses.

The reason this workaround is needed is that the App Engine sockets library does not implement some of the async IO primitives used by the _socketio wrapper in pymysql. At the same time, the MySQLdb module shipped with App Engine wraps a C library which does not know how to use the App Engine sockets library (and probably does not have socket support compiled in).

I'll see if the latter combination can be addressed, but in the meantime, the above 3 steps should provide a workaround which can be used to connect to either your own MySQL or the version 2 of Cloud SQL.

Upvotes: 3

phucat
phucat

Reputation: 54

If you're running this code inside the Appengine then its not possible since appengine does not allow custom Socket connection. You need to use the Managed VM for this instance

Luckily, migrating from Appengine to Managed VM instance is quite simple. On the app.yaml, you just need to add vm:true

module: default

runtime: python27
api_version: 1
threadsafe: yes 
vm: true
default_expiration: 1h

handlers:

# Main script
- url: .*
  script: main.main_app
  login: required

Deploy it by using this command (from the Cloud SDK):

gcloud preview app deploy app.yaml

Upvotes: 0

Related Questions