我使用Google App Engine和Cloud sql构建了一个应用程序,通过Google获得了数据库服务器主机。 当我使用MySQLdb程序包连接到数据库时,该应用程序运行正常。 尝试部署应用程序时,出现内部服务器错误。我发现是因为 与数据库的连接失败。 (当我未连接到数据库时,该应用程序可以在部署中使用) 有人知道导致问题的原因吗,以及为什么在部署应用程序时无法连接到数据库?谢谢
# define a class MainPage which inherits from the class webapp2.RequestHandler
# The class displays the main page of our application
class MainPage(webapp2.RequestHandler):
# define the method get in our class MainPage - to handle http get requests
# creates a response "Hello World!"
def get(self):
g.db_conn = MySQLdb.connect(
host='the host ip num',
user='my username',
passwd='my password',
db='the db name')
cursor = g.db_conn.cursor()
query = """ select * from manager107.air_crew """
cursor.execute(query)
rows = str(cursor.fetchall())
cursor.close()
g.db_conn.close()
# the response will be in a text/plain format
self.response.headers['Content-Type'] = 'text/plain'
# Add the text 'Hello World !' to the response
self.response.write('Hello world!', rows)
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
app.yaml
# -----------------------------------------
# programming language
# -----------------------------------------
runtime: python27
# -----------------------------------------
# python sub version
# -----------------------------------------
api_version: 1
# ---------------------------------------------------------------------
# threadsafe - to prevent collision between calls to the application
# from different clients
# ---------------------------------------------------------------------
threadsafe: true
# ---------------------------------------------------------------------
# handlers - define an application file for each URL we get
# ---------------------------------------------------------------------
handlers:
- url: /
script: helloworld.app
libraries:
- name: MySQLdb
version: latest
这些是我仅有的文件!请帮助
答案 0 :(得分:1)
您没有使用正确的方法连接到Cloud SQL。为了从App Engine连接到Cloud SQL,您需要具有一个JSON文件,其中包含要使用的服务帐户的凭据,并且您必须使用可以在Cloud Console中看到的Cloud SQL连接名称。用户界面。
所需的所有步骤以及一些示例都可以在official documentation和GitHub存储库中找到。