Skip to content

Commit c0006c8

Browse files
author
chenyumic
authored
1 parent b4f6649 commit c0006c8

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

appengine/standard_python37/cloudsql/app.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
runtime: python37
33

44
env_variables:
5-
CLOUDSQL_USERNAME: YOUR-USERNAME
6-
CLOUDSQL_PASSWORD: YOUR-PASSWORD
7-
CLOUDSQL_DATABASE_NAME: YOUR-DATABASE
8-
CLOUDSQL_CONNECTION_NAME: YOUR-CONNECTION-NAME
5+
CLOUD_SQL_USERNAME: YOUR-USERNAME
6+
CLOUD_SQL_PASSWORD: YOUR-PASSWORD
7+
CLOUD_SQL_DATABASE_NAME: YOUR-DATABASE
8+
CLOUD_SQL_CONNECTION_NAME: YOUR-CONNECTION-NAME
99
# [END gae_python37_cloudsql_config]

appengine/standard_python37/cloudsql/main_mysql.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ def main():
3232
# set to `standard`
3333
if os.environ.get('GAE_ENV') == 'standard':
3434
# If deployed, use the local socket interface for accessing Cloud SQL
35-
host = '/cloudsql/{}'.format(db_connection_name)
35+
unix_socket = '/cloudsql/{}'.format(db_connection_name)
36+
cnx = pymysql.connect(user=db_user, password=db_password,
37+
host=unix_socket, db=db_name)
3638
else:
3739
# If running locally, use the TCP connections instead
3840
# Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy)
3941
# so that your application can use 127.0.0.1:3306 to connect to your
4042
# Cloud SQL instance
4143
host = '127.0.0.1'
44+
cnx = pymysql.connect(user=db_user, password=db_password,
45+
host=host, db=db_name)
4246

43-
cnx = pymysql.connect(user=db_user, password=db_password,
44-
host=host, db=db_name)
4547
with cnx.cursor() as cursor:
4648
cursor.execute('SELECT NOW() as now;')
4749
result = cursor.fetchall()

appengine/standard_python37/cloudsql/main_mysql_pooling.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@
2727
# set to `standard`
2828
if os.environ.get('GAE_ENV') == 'standard':
2929
# If deployed, use the local socket interface for accessing Cloud SQL
30-
host = '/cloudsql/{}'.format(db_connection_name)
30+
unix_socket = '/cloudsql/{}'.format(db_connection_name)
31+
engine_url = 'mysql+pymysql://{}:{}@/{}?unix_socket={}'.format(
32+
db_user, db_password, db_name, unix_socket)
3133
else:
3234
# If running locally, use the TCP connections instead
3335
# Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy)
3436
# so that your application can use 127.0.0.1:3306 to connect to your
3537
# Cloud SQL instance
3638
host = '127.0.0.1'
39+
engine_url = 'mysql+pymysql://{}:{}@{}/{}'.format(
40+
db_user, db_password, host, db_name)
3741

3842
# The Engine object returned by create_engine() has a QueuePool integrated
3943
# See https://docs.sqlalchemy.org/en/latest/core/pooling.html for more
4044
# information
41-
engine = sqlalchemy.create_engine('mysql+pymysql://{}:{}@{}/{}'.format(
42-
db_user, db_password, host, db_name
43-
), pool_size=3)
45+
engine = sqlalchemy.create_engine(engine_url, pool_size=3)
4446

4547
app = Flask(__name__)
4648

appengine/standard_python37/cloudsql/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
flask==1.0.2
12
psycopg2==2.7.5
23
psycopg2-binary==2.7.5
34
PyMySQL==0.9.2

0 commit comments

Comments
 (0)