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

查看:98
本文介绍了将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?

推荐答案

包括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才应位于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

这是一个使用代理连接和查询Cloud MySQL实例的Node应用程序示例. 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天全站免登陆