从 GAE 连接 Compute Engine 实例上的 mysql 服务器 运行
Connecting with mysql server running on Compute Engine instance from GAE
如何从 google 应用引擎与 Compute Engine 实例上的 mysql 服务器 运行 通信?我们使用 google 应用引擎作为前端。我们想在 Compute Engine 上的 mysql 服务器 运行 上托管我们的数据库。有什么办法可以实现吗?
我们经历过这个:
https://cloud.google.com/solutions/mysql-remote-access
代码片段:
if (os.getenv('SERVER_SOFTWARE') and
os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db=MySQLdb.connect(host="InternalIP", port=3306, db='test_database', user='root',passwd="db_password")
else:
db = MySQLdb.connect(host='127.0.0.1', port=3306, db='test_database', user='root', charset='utf8')
cursor = db.cursor()
logging.info("Hey there looks like I am connected")
如果您 运行 此代码位于 Appengine 中,那么它是不可能的,因为 Appengine 不允许自定义套接字连接。您需要为此实例使用托管 VM
幸运的是,从 Appengine 迁移到托管 VM 实例非常简单。
在 app.yaml 上,您只需添加 vm:true
module: default
runtime: python27
api_version: 1
threadsafe: yes
vm: true
default_expiration: 1h
handlers:
# Main script
- url: .*
script: main.main_app
login: required
使用此命令部署它(来自 Cloud SDK):
gcloud preview app deploy app.yaml
伊戈尔的 comment above hints at how to get this working; I've managed to produce a working app, with the following changes to the documented solution.
- 为
host
参数指定 public(外部)IP 地址,而不是 unix_socket
。
- 将 MySQLdb 替换为
pymysql
。特别是,您想将 pymysql
目录从 GitHub 存储库复制到您的应用程序目录中。使用 pymysql
还意味着您不需要将 MySQLdb
添加到 app.yaml. 的 libraries:
部分
在 connections.py
中,围绕第 52 行,更改以下行:
if _py_version == (2, 7) and not IRONPYTHON
至
if _py_version == (2, 7) and not IRONPYTHON and not os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
您可能需要更改 mysql 中的 MySQL 权限授予,以允许来自 the App Engine source IP addresses.
的访问
需要此解决方法的原因是 App Engine 套接字库未实现 pymysql
中的 _socketio
包装器使用的某些异步 IO 原语。同时,App Engine 附带的 MySQLdb
模块包装了一个不知道如何使用 App Engine sockets
库的 C 库(并且可能没有编译套接字支持)。
我会看看后一种组合是否可以解决,但与此同时,上述 3 个步骤应该提供了一种解决方法,可用于连接到您自己的 MySQL 或 version 2 of Cloud SQL.
如何从 google 应用引擎与 Compute Engine 实例上的 mysql 服务器 运行 通信?我们使用 google 应用引擎作为前端。我们想在 Compute Engine 上的 mysql 服务器 运行 上托管我们的数据库。有什么办法可以实现吗?
我们经历过这个: https://cloud.google.com/solutions/mysql-remote-access
代码片段:
if (os.getenv('SERVER_SOFTWARE') and
os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db=MySQLdb.connect(host="InternalIP", port=3306, db='test_database', user='root',passwd="db_password")
else:
db = MySQLdb.connect(host='127.0.0.1', port=3306, db='test_database', user='root', charset='utf8')
cursor = db.cursor()
logging.info("Hey there looks like I am connected")
如果您 运行 此代码位于 Appengine 中,那么它是不可能的,因为 Appengine 不允许自定义套接字连接。您需要为此实例使用托管 VM
幸运的是,从 Appengine 迁移到托管 VM 实例非常简单。 在 app.yaml 上,您只需添加 vm:true
module: default
runtime: python27
api_version: 1
threadsafe: yes
vm: true
default_expiration: 1h
handlers:
# Main script
- url: .*
script: main.main_app
login: required
使用此命令部署它(来自 Cloud SDK):
gcloud preview app deploy app.yaml
伊戈尔的 comment above hints at how to get this working; I've managed to produce a working app, with the following changes to the documented solution.
- 为
host
参数指定 public(外部)IP 地址,而不是unix_socket
。 - 将 MySQLdb 替换为
pymysql
。特别是,您想将pymysql
目录从 GitHub 存储库复制到您的应用程序目录中。使用pymysql
还意味着您不需要将MySQLdb
添加到 app.yaml. 的 在
connections.py
中,围绕第 52 行,更改以下行:if _py_version == (2, 7) and not IRONPYTHON
至
if _py_version == (2, 7) and not IRONPYTHON and not os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
您可能需要更改 mysql 中的 MySQL 权限授予,以允许来自 the App Engine source IP addresses.
的访问
libraries:
部分
需要此解决方法的原因是 App Engine 套接字库未实现 pymysql
中的 _socketio
包装器使用的某些异步 IO 原语。同时,App Engine 附带的 MySQLdb
模块包装了一个不知道如何使用 App Engine sockets
库的 C 库(并且可能没有编译套接字支持)。
我会看看后一种组合是否可以解决,但与此同时,上述 3 个步骤应该提供了一种解决方法,可用于连接到您自己的 MySQL 或 version 2 of Cloud SQL.