我是使用App Engine的MySQL数据库的新手,我希望有人可以帮我解决我遇到的问题。我有一个函数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'
答案 0 :(得分:0)
这里的错误是
CLOUDSQL_CONNECTION_NAME
是None
。可能你没有为此设置环境变量。