回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我的目标是让一个正在工作的python2.7项目(MySQL+msword文件)在GCP工作。
我意识到我需要</p>
<ol>
<li>应用引擎-应用程序运行的位置(缩放等)。在</li>
<li>云SQL作为MySQL数据库工作。
为此,我遵循了<a href="https://cloud.google.com/appengine/docs/standard/python/cloud-sql/using-cloud-sql-mysql" rel="nofollow noreferrer">Cloud SQL for MySQL tut</a>和
<ul>
<li>云SQL实例是用<code>root</code>用户创建的。在</li>
<li>App Engine App和Cloud SQL实例都在同一个项目中。在</li>
</ul></li>
<li>云存储</li>
</ol>
<p>成功创建SQL第二代实例并设置<em>根</em>用户。在</p>
<p><strong>如何运行或部署</strong></p>
<p>我使用云Shell测试应用程序-<code>dev_appserver.py $PWD</code>,并从cloudshell-<code>gcloud app deploy</code>部署应用程序。<a href="https://coral-heuristic-215610.appspot.com/" rel="nofollow noreferrer">It works at appspot.com</a>直到我尝试在其中使用MySQL连接。在</p>
<p><strong>MySQL连接</strong></p>
<p>MySQL连接代码取自<a href="https://cloud.google.com/appengine/docs/standard/python/cloud-sql/using-cloud-sql-mysql#code_sample_overview" rel="nofollow noreferrer">here</a>:</p>
<pre><code>import MySQLdb
import webapp2
CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')
DB_NAME='test-db'
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='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD, db=DB_NAME)
return db
db = connect_to_cloudsql()
</code></pre>
<p>变量设置在<strong>应用程序yaml</strong>:</p>
^{pr2}$
<p>当应用程序通过<code>dev_appserver.py $PWD</code>在测试模式下运行并且我选择使用MySQL连接时,出现了一个错误:</p>
<pre><code>ERROR 2018-09-13 08:37:42,492 wsgi.py:263]
Traceback (most recent call last):
...
File "/home/.../mysqldb.py", line 35, in connect_to_cloudsql
host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD)
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, 'Can\'t connect to MySQL server on \'127.0.0.1\' (111 "Connection refused")')
</code></pre>
<p><strong>云SQL代理</strong></p>
<p>我已经下载并运行了Win-64(<a href="https://dl.google.com/cloudsql/cloud_sql_proxy_x64.exe" rel="nofollow noreferrer">https://dl.google.com/cloudsql/cloud_sql_proxy_x64.exe</a>)的云代理,但问题仍然存在。。。似乎代理后台应用程序只用于从本地机器连接到云SQL。在</p>
<blockquote>
<p>You do not need to use the proxy or configure SSL to connect to Cloud SQL from the App Engine standard or flexible environment. (<a href="https://cloud.google.com/sql/docs/mysql/sql-proxy#what_the_proxy_provides" rel="nofollow noreferrer">source</a>)</p>
</blockquote>
<p><strong>为什么连接被拒绝?</strong></p>
<p><strong>我是否应该使用第一代云sql实例来简化与App Engine的连接?</strong></p>
<h2>更新1</h2>
<p>我在云控制台编辑代码,到目前为止,云控制台运行良好。在</p>
<h2>更新2</h2>
<p>我已经通过云Shell成功连接到sql实例:</p>
<pre><code>(coral-heuristic-215610)$ gcloud sql connect db-basic-1 --user=root
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [root].Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 48841
Server version: 5.7.14-google-log (Google)
MySQL [(none)]>
</code></pre>
<h2>更新3</h2>
<p><a href="https://github.com/GoogleCloudPlatform/getting-started-python/issues/129#issuecomment-367348828" rel="nofollow noreferrer">comment on a similar issue</a>关系到<em>云SQL实例</em>和<em>App Engine App</em>应该所在的区域,即<strong>在同一个区域中</strong>。在</p>
<p>就我而言,我检查过:</p>
<ul>
<li>要连接到的云SQL实例:<strong>us-central1-a</strong></li>
<li>应用程序引擎应用程序:<strong>美国中央银行</strong></li>
</ul>
<p><strong>这些是同一地区的吗?</strong>-发现了一个区域的这些区域。在</p>
<h2>更新4</h2>
<p>我本可以打开数据库连接:</p>
<p><code>DB connection: <_mysql.connection open to '127.0.0.1' at 7f628c02bc00></code></p>
<p>但这似乎是在我用同一个项目打开另一个云Shell实例(<em>coral-heuristic-215610</em>)之后发生的。在那个实例中,我已经开始连接到SQL实例,并且成功了:</p>
<pre><code>(coral-heuristic-215610)$ gcloud sql connect db-basic-1 --user=root
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [root].Enter password:
</code></pre>
<p>我猜第一个云shell实例开始连接到db是因为<strong>第二个实例已经将我的IP列入了白名单,不是吗?在</p>