将 Google Cloud App Engine 上的 Node.js 应用程序连接到 Google Cloud SQL 实例 [英] Connecting Node.js app on Google Cloud App Engine to a Google Cloud SQL instance

查看:22
本文介绍了将 Google Cloud App Engine 上的 Node.js 应用程序连接到 Google Cloud SQL 实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 MySQL 的 Node 应用程序,通过配置 json 连接:

I have a Node app which uses MySQL, connecting via a config json:

{
    "client": "mysql",
    "connection": {
        "host": "something",
        "user": "something",
        "password": "something",
        "database": "daimonion-db",
        "debug": false
    }
}

我已经创建了一个 Google Cloud Platform SQL 实例.我看到的是 IP 地址和实例连接名称.

I've created a Google Cloud Platform SQL instance. I'm seeing an IP address and instance connection name.

我还在一个灵活的环境中将 Node 应用部署到了 Google Cloud App Engine.

I've also deployed the Node app to Google Cloud App Engine in a flexible environment.

如何将 Node 应用程序连接到 SQL 实例?我看到了这个解释:https://cloud.google.com/sql/docs/mysql/connect-app-engine 告诉我将设置字符串添加到我的 app.yaml 以连接到 Unix 域套接字或 TCP 连接,但是我如何从我的节点应用?

How do I connect the Node app to the SQL instance? I'm seeing this explanation: https://cloud.google.com/sql/docs/mysql/connect-app-engine which tells me to add a settings string to my app.yaml to connect with either a Unix domain socket or TCP connection, but how do I connect to these from my Node app?

推荐答案

include beta_settingsapp.yaml 以在生产中的实例上启用云代理,并指定config 中的 UNIX 套接字 socketPath,因此您的 flex 应用程序可以通过代理连接到实例.

include beta_settings to app.yaml to enable cloud proxy on the instance in production, and specify the UNIX socket socketPath in config, so your flex app can connect to the instance via proxy.

socketPath 仅当应用在 App Engine 的生产环境中运行时才应在 config 中.对于本地开发,TCP 套接字与代理客户端一起使用,您需要安装并使用以下命令启动:

socketPath should be in config only if the app is running in production on App Engine. For local development, the TCP socket is used with the proxy client, that you need to install and start with the following commands:

wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy

chmod +x cloud_sql_proxy

./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306

这是一个 Node 应用示例,它使用代理连接和查询 Cloud MySQL 实例.if 语句允许应用自动切换配置 dev-local/prod-appengine,使用环境变量.

here's an example of Node app that connects and queries a Cloud MySQL instance using the proxy. The if statement permits the app to switch configuration dev-local/prod-appengine automatically, using environment variables .

app.yaml

runtime: nodejs
env: flex
env_variables:
  SQL_USER: [SQL_USER]
  SQL_PASSWORD: [SQL_PASSWORD]
  SQL_DATABASE: [DATABASE_NAME]
  INSTANCE_CONNECTION_NAME: [INSTANCE_CONNECTION_NAME]
beta_settings:
  cloud_sql_instances: [INSTANCE_CONNECTION_NAME]

package.json

{
  "engines": {
    "node": "8.x.x"
  },
  "dependencies": {
    "express": "4.16.3",
    "mysql": "^2.15.0"
  },
  "scripts": {
    "start": "node server.js"
  }
}

server.js

const express = require('express');
const mysql = require('mysql');

const app = express();

var config = {
    user: process.env.SQL_USER,
    database: process.env.SQL_DATABASE,
    password: process.env.SQL_PASSWORD
}

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

var connection = mysql.createConnection(config);

connection.connect();

app.get('/', (req, res) => {
    connection.query(
        'SELECT * FROM entries', function(err, result, fields){
            if (err) throw err;
            res.send(result);
        }
    );
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

这篇关于将 Google Cloud App Engine 上的 Node.js 应用程序连接到 Google Cloud SQL 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆