Connecting to the instance from our application
To connect with Cloud SQL instances from our Python code, we use the MySQLdb
package, which is a MySQL driver that implements the Python Database API as described in the PEP 249 document. To install the package, we can use pip
; from the command line, we issue the following command:
pip install MySQL-python
We don't specify the -t
option as we did when installing GCS Client Library in Chapter 3, Storing and Processing Users' Data because the MySQLdb
package is included in App Engine Python Runtime Environment on the production servers and we don't need to upload it during deployment. Instead, we list the package in the libraries
section of the app.yaml
file:
libraries: - name: webapp2 version: "2.5.2" - name: jinja2 version: latest - name: MySQLdb version: latest
A simple test to check if the database connection is working correctly consists of retrieving and logging the Cloud SQL version number. We add a function...