Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions appengine/standard/cloudsql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ Refer to the [App Engine Samples README](../README.md) for information on how to

1. You will need to create a [Cloud SQL instance](https://cloud.google.com/sql/docs/create-instance).

2. Edit the `CLOUDSQL_INSTANCE` and `CLOUDSQL_PROJECT` values in `main.py`.

3. To run locally, you will need to be running a local instance of MySQL. You may need to update the connection code in `main.py` with the appropriate local username and password.
2. Edit the update the `env_variables` section in `app.yaml` with your Cloud SQL configuration.
7 changes: 7 additions & 0 deletions appengine/standard/cloudsql/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ handlers:
libraries:
- name: MySQLdb
version: "latest"

# [START env_variables]
env_variables:
CLOUDSQL_CONNECTION_NAME: your-connection-name
CLOUDSQL_USER: root
CLOUDSQL_PASSWORD: your-cloudsql-user-password
# [END env_variables]
51 changes: 34 additions & 17 deletions appengine/standard/cloudsql/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,57 @@

"""
Sample App Engine application demonstrating how to connect to Google Cloud SQL
using App Engine's native unix socket.
using App Engine's native unix socket or using TCP when running locally.

For more information, see the README.md.
"""

# [START all]

import os

import MySQLdb
import webapp2


CLOUDSQL_PROJECT = '<your-project-id>'
CLOUDSQL_INSTANCE = '<your-cloud-sql-instance>'
# These environment variables are configured in app.yaml.
CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')


def connect_to_cloudsql():
# When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
# will be set to 'Google App Engine/version'.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
# Connect using the unix socket located at
# /cloudsql/cloudsql-connection-name.
cloudsql_unix_socket = os.path.join(
'/cloudsql', CLOUDSQL_CONNECTION_NAME)

db = MySQLdb.connect(
unix_socket=cloudsql_unix_socket,
user=CLOUDSQL_USER,
passwd=CLOUDSQL_PASSWORD)

# If the unix socket is unavailable, then try to connect using TCP. This
# will work if you're running a local MySQL server or using the Cloud SQL
# proxy, for example:
#
# $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
#
else:
db = MySQLdb.connect(
host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD)

return db


class MainPage(webapp2.RequestHandler):
def get(self):
"""Simple request handler that shows all of the MySQL variables."""
self.response.headers['Content-Type'] = 'text/plain'

# When running on Google App Engine, use the special unix socket
# to connect to Cloud SQL.
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
db = MySQLdb.connect(
unix_socket='/cloudsql/{}:{}'.format(
CLOUDSQL_PROJECT,
CLOUDSQL_INSTANCE),
user='root')
# When running locally, you can either connect to a local running
# MySQL instance, or connect to your Cloud SQL instance over TCP.
else:
db = MySQLdb.connect(host='localhost', user='root')

db = connect_to_cloudsql()
cursor = db.cursor()
cursor.execute('SHOW VARIABLES')

Expand Down
12 changes: 9 additions & 3 deletions appengine/standard/cloudsql/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
import pytest
import webtest

import main

@pytest.fixture
def main(monkeypatch):
monkeypatch.setenv('CLOUDSQL_USER', 'root')
monkeypatch.setenv('CLOUDSQL_PASSWORD', '')
import main
return main


@pytest.mark.skipif(
not os.path.exists('/var/run/mysqld/mysqld.sock'),
reason='MySQL server not available.')
def test_app():
reason='Local MySQL server not available.')
def test_app(main):
app = webtest.TestApp(main.app)
response = app.get('/')

Expand Down