我是将MySQL数据库与App Engine一起使用的新手,我希望有人可以帮助我解决我遇到的问题。我有一个函数connect_to_cloudsql()
可以与本地或云数据库建立连接,这是我从 Google Academy 教程中获得的。代码如下:
# These environment variables are configured in app.yaml.
CLOUDSQL_CONNECTION_NAME = os.environ.get(<something here>)
CLOUDSQL_USER = os.environ.get(<something here>)
CLOUDSQL_PASSWORD = os.environ.get(<something here>)
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='localhost', port=3310, user='root', passwd='something here', db='DB1')
return db
然后,我使用类中的connect_to_cloudsql()
连接到数据库。代码在本地运行良好,但是当我部署并尝试使用它时,出现以下错误:
File "/base/data/home/apps/s~reliance-group/1.401176020169136394/sql/tests.py", line 70, in get
con = connect_to_cloudsql()
File "/base/data/home/apps/s~reliance-group/1.401176020169136394/sql/tests.py", line 45, in connect_to_cloudsql
'/cloudsql', CLOUDSQL_CONNECTION_NAME)
File "/base/data/home/runtimes/python27_experiment/python27_dist/lib/python2.7/posixpath.py", line 68, in join
if b.startswith('/'):
AttributeError: 'NoneType' object has no attribute 'startswith'
这里的错误是
CLOUDSQL_CONNECTION_NAME
None
.可能是您没有为此设置环境变量。