Reputation: 31
Recently I was going through the "Using Python App Engine with Google Cloud SQL" tutorial on Google Developers Academy website. However, I stumbled upon on the first part of the exercise "Building an application with a local MySQL instance". I could not connect the sample code (main.py) to my local MySQL instance. Wonder if anyone has found a solution to this problem.
It would be great if you could share with me how you set up your MySQL, configure it so the GAE's sandbox would be able to access the MySQL-python connecter.
Upvotes: 1
Views: 2328
Reputation: 31
Since I am a newbie to GAE, Python, MySQL and developing on Mac OS X with GAE, it really took me a while to figure out how to get the sample application working. Since I have found the solution, I would like to share it here so that you would just follow my procedure to get the sample application up and running in your local GAE SDK environment with your local MySQL instance.
Personally, I found this very useful and handy since it is very natural for a developer to develop their GAE applications on their own machine with a local MySQL instance before deploying them on Google App Engine PaaS.
Enough said, below is the procedure I would like to share:
Install a MySQL dmg from the mysql.com community site (my version is mysql-5.5.30-osx10.6-x86_64.dmg)
Modify your ~/.profile to add these environment variables (add these lines) export PATH=/usr/local/mysql/bin:$PATH (or to whatever your version of MySQL was installed) export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
Install Xcode from Apple's AppStore if you have not done so. Then make sure you also install the "Command Line Tools" from the preferences "downloads" section. This step is necessary since it will install a gcc compiler (located at my file system here: /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2) to build the MySQL-python connector in step 4 below.
Then run "sudo easy_install MySQL-python" (so you can connect your application to a local MySQL instance on your Mac)
Since Google App Engine SDK for Python is a sandbox environment, so you will need to add "import MySQLdb" in this file /usr/local/bin/dev_appserver.py to bridge the GAE's sandbox environment to a local MySQL instance. You will need to make the following changes in the [sample code][2] of the "main.py" file. As follows before you could run the sample code correctly:
def get_connection():
return MySQLdb.connect(host=CLOUDSQL_INSTANCE, db=DATABASE_NAME,
user=USER_NAME, passwd=PASSWORD, charset='utf8')
After all of the steps above are completed, you can now launch the sample application by executing this command : "dev_appserver.py ." in your application directory.
If you are using the default 8080 port, you can point your browser to http://localhost:8080
.
Enjoy and have fun.
Upvotes: 1
Reputation: 1418
I managed to do this some time back for a project. The procedure is as follows (for Windows):
In your app insert the following import line:
from google.appengine.api import rdbms
5 . What I did was create a function that returned the data connection like so:
def conn_to_db():
return rdbms.connect(instance='MySQL55', database='Your_db_name_here', user='root', password='whatever_password_you_chose')
Then to query the database:
conn = conn_to_db()
cursor = conn.cursor()
cursor.execute('SELECT * FROM foo where foos = %s', (bar))
for row in cursor.fetchall():
var1 = row[0]
var2 = row[1]
conn.close()
Upvotes: 1