GAE 云 SQL 代理使用 TCP 套接字(而不是 UNIX)

GAE cloud SQL proxy use TCP sockets (instead of UNIX)

我们正在 GAE 上设置一个节点应用程序,而我们使用的库之一不能很好地与 unix 套接字配合使用。

GAE 中的云代理设置使用 unix 套接字,我已经 googled 搜索了 google 文档,但找不到有关配置在 GAE 中创建的云代理的信息(即在生产中)使用 TCP 套接字。

(我在测试时在本地完成,但无法弄清楚我需要在 app.yaml 中设置什么才能在生产中实现)

为了能够在您的应用中使用 TCP 套接字,您必须迁移到 Custom Runtime

您可以使用命令生成 Dockerfile

$ gcloud beta app gen-config --custom

为本次运行时间生成Dockerfile后,修改如下:

FROM gcr.io/google_appengine/nodejs
RUN /usr/local/bin/install_node '>=4.3.2'
RUN apt-get update
RUN apt-get install mysql-client -yy
RUN apt-get install wget -yy
COPY . /app/
RUN npm install --unsafe-perm || \
  ((if [ -f npm-debug.log ]; then \
      cat npm-debug.log; \
    fi) && false)
RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
RUN chmod +x cloud_sql_proxy
RUN echo -e “#!/bin/bash\n./cloud_sql_proxy -instances=YOUR-INSTANCE-DB=tcp:3306 &\nnode server.js” > script.sh
RUN chmod +x script.sh
CMD bash script.sh

这将在部署时生成容器,并且该容器将具有通过 TCP 进行通信的代理。

部署之前,在您的 app.yaml 文件中,您必须从 env_variables INSTANCE_CONNECTION_NAME 中删除,因为您已经在 Dockerfile 中指出了这一点。

别忘了enable the Cloud SQL API到运行这个。

我已经使用了例子in here,你可以看到server.js中的函数connect有一个if条件是这样的:

if (process.env.INSTANCE_CONNECTION_NAME && process.env.NODE_ENV === 'production') {
    config.socketPath =/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}
}

如果你有类似的东西,删除它,因为你现在没有提供,在环境变量中,INSTANCE_CONNECTION_NAME。

这是一种解决方法,但如果不是这样,我认为不可能。