<p>GAE应用程序和Google云SQL实例必须部署在同一个区域<a href="https://cloud.google.com/sql/docs/mysql/connect-app-engine#gaev1-csqlv1" rel="nofollow noreferrer">if you’re using MySQL First Generation</a>,否则,我验证了只要使用第二代MySQL,它们就可以位于不同的区域。在</p>
<p>我很难理解你想从哪里联系。我假设您希望使用代理和<a href="https://cloud.google.com/sql/docs/mysql/connect-admin-proxy#service-account" rel="nofollow noreferrer">Cloud SDK Credentials</a>从Google云Shell连接。根据有关<a href="https://cloud.google.com/sql/docs/mysql/sql-proxy" rel="nofollow noreferrer">Cloud SQL Proxy</a>的文件:</p>
<blockquote>
<p>The Cloud SQL Proxy provides secure access to your Cloud SQL Second
Generation instances without having to whitelist IP addresses or
configure SSL.</p>
<p>The Cloud SQL Proxy works by having a local client, called the proxy,
running in the local environment. Your application communicates with
the proxy with the standard database protocol used by your database.
The proxy uses a secure tunnel to communicate with its companion
process running on the server.</p>
</blockquote>
<p>请记住,由于您没有部署应用程序,所以它不会使用您在应用程序yaml. 因此,您必须自己导出并在本地计算机中设置它们:</p>
<pre><code>export CLOUDSQL_CONNECTION_NAME=your-connection-name
export CLOUDSQL_USER=root
export CLOUDSQL_PASSWORD=xxxxx
</code></pre>
<p>通过执行<code>echo $CLOUDSQL_CONNECTION_NAME</code>验证它们是否已设置。当您使用<code>gcloud app deploy</code>部署应用程序时,这是不必要的,因为GAE设置了在应用程序yaml. 在</p>
<p>在尝试按照<a href="https://cloud.google.com/sql/docs/mysql/connect-admin-proxy" rel="nofollow noreferrer">these</a>步骤建立连接之前,必须启动代理:</p>
<p>下载代理:</p>
^{pr2}$
<p>授予it执行权限:</p>
<pre><code>chmod +x cloud_sql_proxy
</code></pre>
<p>启动代理,将<code><INSTANCE_CONNECTION_NAME></code>替换为云SQL实例连接名称:</p>
<pre><code>./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
</code></pre>
<p>你应该在结尾看到类似的内容:</p>
<pre><code> 2018/11/09 13:24:32 Rlimits for file descriptors set to {&{8500 1048576}}
2018/11/09 13:24:35 Listening on 127.0.0.1:3306 for my-project:cloud-sql-region:cloud-sql-name
2018/11/09 13:24:35 Ready for new connections
</code></pre>
<p>此时,您可以从Google<a href="https://cloud.google.com/shell/docs/starting-cloud-shell" rel="nofollow noreferrer">Cloud Shell instance</a>连接到本地运行的代理服务器,该代理又将您连接到云SQL实例。在</p>
<p>打开另一个<a href="https://cloud.google.com/shell/docs/using-the-session-window#opening_multiple_sessions" rel="nofollow noreferrer">Cloud Shell session</a>(或制表符)并用<code>python myapp.py</code>启动代码。您将连接到本地运行的代理。您也可以通过运行<code>mysql -h 127.0.0.1 user=root -p</code>来测试连接。在</p>