简体   繁体   English

如何从 Node JS 中的 Google Cloud Function 中的云 SQL 中读取数据

[英]How to read data form cloud SQL in Google Cloud Function in Node JS

I am new in Google Cloud Function, Here am trying to load data from cloud SQL for that I have written a code, which has database connection as well as SQL query string however when am trying to test the function it is throwing either error or Object or sometime it says you are not returning any data.我是 Google Cloud Function 的新手,我在这里尝试从云 SQL 加载数据,因为我编写了一个代码,它具有数据库连接以及 SQL 查询字符串但是当我尝试测试 function 时,它抛出错误或 Object或者有时它说您没有返回任何数据。 I tried many ways to send data but still doesn't work for me.我尝试了很多发送数据的方法,但仍然对我不起作用。 Thanks in advance.提前致谢。

Code snippets,代码片段,

exports.welcome = async (req, res) => {
  const tag = req?.body?.fulfillmentInfo?.tag;
  let message = tag || req?.query?.message || req?.body?.message || 'Hello World!';

  const pool = mysql.createPool({
    connectionLimit: 1,
    host: '10.30.48.2',
    user: 'root',
    password: 'TEST@TEST',
    database: 'hospital_assist'
  });
  
  const jsonResponse = {
    fulfillment_response: {
      messages: [
        {
          text: {
            //fulfillment text response to be sent to the agent
            text: [`Welcome entry point ${message}`],
          },
        },
      ],
    },
  };

  let qResult = await pool.query('SELECT * FROM Patients;');
  // pool.query('SELECT * FROM Patints', (error, result) => {
  //   console.log(error);
  //   console.log(result);
  //   console.log('In loop');
  //   res.status(200).send(jsonResponse);  
  // });



  // await pool.query('select * from Patients;', (error, results) => {
  //       if (error) {
  //           console.log(error);
  //           res.status(200).send(jsonResponse);
  //       }
  //       qResult = results;
  //       // res.send(results);
  //   });

  console.log(qResult);
  console.log(`In loop ${qResult}`);
  res.status(200).send(JSON.stringify(qResult)); 

  // res.status(200).send(jsonResponse);
};

If you're trying to connect to your instance using public IP, you'll need to specify a Unix socket path, eg,如果您尝试使用公共 IP 连接到您的实例,则需要指定 Unix 套接字路径,例如,

mysql.createPool({
    user: process.env.DB_USER, // e.g. 'my-db-user'
    password: process.env.DB_PASS, // e.g. 'my-db-password'
    database: process.env.DB_NAME, // e.g. 'my-database'
    // If connecting via unix domain socket, specify the path
    socketPath: "/cloudsql/my-project:us-central1:my-instance",
    // Specify additional properties here.
    ...config,
});

See the docs for details.有关详细信息,请参阅文档

In your case, you have several solutions, 2 are specially recommended:对于您的情况,您有几种解决方案,特别推荐 2 种:

  • Use internal IP (your current code).使用内部 IP(您当前的代码)。 With that pattern, you can remove the public IP and hide totally the database from the inte.net.使用该模式,您可以删除公共 IP 并从 inte.net 中完全隐藏数据库。 To allow CLoud Functions accessing private IP, you have to bridge the serverless world (the server and the.network is managed for you by Google Cloud, and therefore you aren't in your VPC) with your project world (especially your VPC where your database is connected).要允许 CLoud Functions 访问私有 IP,您必须在无服务器世界(服务器和网络由 Google Cloud 为您管理,因此您不在您的 VPC 中)与您的项目世界(尤其是您的 VPC数据库已连接)。 A serverless VPC connector is done for that. 无服务器 VPC 连接器已为此完成。
  • With Cloud Functions you can use a built-in connector with Cloud SQL MySQL (only in version 8).借助 Cloud Functions,您可以将内置连接器与 Cloud SQL MySQL (仅在版本 8 中)一起使用。 It opens a linux socket and you have to use a driver/library that support that socket communication mode.它打开一个 linux 套接字,您必须使用支持该套接字通信模式的驱动程序/库。 The advantage here is the encryption of the database communication through the built-in connector.这里的优势是通过内置连接器对数据库通信进行加密。

Note: with a Cloud SQL public IP avoid to authorized.network on your Cloud SQL database to keep a good level of security注意:使用 Cloud SQL public IP 避免在您的 Cloud SQL 数据库上使用 authorized.network 以保持良好的安全级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Cloud Functions 读取存储在 Google Cloud Storage 中的 CSV 数据 - How to read CSV data stored in Google Cloud Storage with Cloud Functions 从 GKE pod 查询 Node.js 中的 Google 云 SQL 实例,云 sql 代理作为边车运行 - Query a Google cloud SQL instance in Node.js from a GKE pod with cloud sql proxy running as sidecar 如何在谷歌云构建上升级节点 js 版本 - How to upgrade node js version on google cloud build 使用 Firebase 云从 Cloud Firestore 读取数据 function - Read data from Cloud firestore with Firebase cloud function 谷歌云/应用引擎(节点 JS)“错误:EIO:i/o 错误,读取” - Google Cloud / App Engine (Node JS) "Error: EIO: i/o error, read" 使用 Python 和 SQLAlchemy 从谷歌云 Function 连接到云 SQL - Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy 使用 firebase 云 function 从云 Firestore 读取数据? - Read data from cloud firestore with firebase cloud function? 节点JS | 谷歌视觉 API | 将 base64 发送到 Google Cloud Vision 时出现“错误的图像数据” - Node JS | Google Vision API | "Bad image data" when sending base64 to Google Cloud Vision 无法将 Cloud Data Fusion 与 Google Cloud SQL 连接为 PostgreSQL - Can't connect Cloud Data Fusion with Google Cloud SQL for PostgreSQL 谷歌云 Function:如何继续超时 function - Google Cloud Function : how to continues timeout function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM