【问题标题】:{ Error: connect ETIMEDOUT at Connection._handleConnectTimeout and wierd undefined messages{ 错误:在 Connection._handleConnectTimeout 处连接 ETIMEDOUT 和奇怪的未定义消息
【发布时间】:2020-07-11 00:01:16
【问题描述】:

当我尝试连接到我的谷歌云 mysql 数据库时,它向我显示此错误连接 ETIMEDOUT 加上未定义的消息。这个错误已经花费了很多天,我无法弄清楚这可能是什么解决方案。我过去没有这种语言的经验。

当我详细打开此错误时,它显示如下:

{ Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (/srv/node_modules/mysql/lib/Connection.js:409:13)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at Socket.emit (events.js:208:7)
    at Socket._onTimeout (net.js:422:8)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
    --------------------
    at Protocol._enqueue (/srv/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/srv/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/srv/node_modules/mysql/lib/Connection.js:116:18)
    at Promise (/srv/index.js:34:19)
    at new Promise (<anonymous>)
    at connectToDatabase (/srv/index.js:33:12)
    at handleReadFromMySQL (/srv/index.js:52:12)
    at WebhookClient.handleRequest (/srv/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/srv/index.js:104:9)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true }

请帮助我从我的数据库中检索数据。我指的是本教程https://www.youtube.com/watch?v=v7k5vckSzNo&t=892s 我也在 cmets 部分问了我的问题,但他们没有回复。

这是我的 index.js 文件

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const mysql = require('mysql');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }

function connectToDatabase(){
    const connection = mysql.createConnection({
      host     : '34.93.236.175',
      user     : 'root',
      password : '',
      database : 'django_db',
    });
    return new Promise((resolve,reject) => {
       connection.connect();
       resolve(connection);
    });
  }

  function queryDatabase(connection) {
    return new Promise((resolve,reject) => {
      connection.query('SELECT * from web_user',(error, results, fields) => {
        console.log("result console");
        console.log(results);
        console.log(fields);
        console.log(error);
        resolve(results);
      });
    });
  }

 function handleReadFromMySQL(agent){
    return connectToDatabase()
    .then(connection => {
      return queryDatabase(connection)
      .then(result => {
        console.log(result);
        connection.end();
      }).catch(error => {
        console.log("error");
        console.log(error);
        connection.end();
      });
    }).catch(error=>{
      console.log(error);
    });

 }
  // // Uncomment and edit to make your own intent handler
  // // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function yourFunctionHandler(agent) {
  //   agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
  //   agent.add(new Card({
  //       title: `Title: this is a card title`,
  //       imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
  //       text: `This is the body text of a card.  You can even use line\n  breaks and emoji! ????`,
  //       buttonText: 'This is a button',
  //       buttonUrl: 'https://assistant.google.com/'
  //     })
  //   );
  //   agent.add(new Suggestion(`Quick Reply`));
  //   agent.add(new Suggestion(`Suggestion`));
  //   agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
  // }

  // // Uncomment and edit to make your own Google Assistant intent handler
  // // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
  // // below to get this function to be run when a Dialogflow intent is matched
  // function googleAssistantHandler(agent) {
  //   let conv = agent.conv(); // Get Actions on Google library conv instance
  //   conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
  //   agent.add(conv); // Add Actions on Google library responses to your agent's response
  // }
  // // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
  // // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('getDataFromMySQL', handleReadFromMySQL);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

package.json 文件

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "8"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2",
    "dialogflow": "^0.6.0",
    "dialogflow-fulfillment": "^0.5.0",
    "mysql": "2.18.1",
    "firebase-tools" : "7.16.1"
  }
}

非常感谢您的帮助,这是我最后一年项目的一部分。此外,我必须对数据库执行插入、更新和删除操作。

【问题讨论】:

    标签: mysql node.js google-cloud-platform dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    确切的问题诊断有点困难,因为我们不知道您的 MySQL 服务器的配置,但从广义上讲,这个错误是因为网络中的某些配置阻止了连接。

    顺便说一句,我能想到几个可能的地方:

    • 听起来您的履行正在使用内联编辑器运行。这使用了 Google Cloud Functions,因此该功能中可能存在阻止网络连接的配置。
      • 请注意,以前版本的内联编辑器使用 Firebase Cloud Functions,这会阻止网络连接,除非您使用付费计划(即使您使用免费套餐)。我认为 Google Cloud Functions 没有同样的限制,但可能。
    • 您的 MySQL/cpanel 配置可能会限制您可以连接的位置。
    • 您的防火墙可能不允许连接 mysql 端口。
    • 同样,您的 MySQL 配置可能在不同于标准的端口上侦听。在这种情况下,您需要确保计算机上的端口已打开,您的防火墙将允许该端口通过,并且您的代码已配置为连接到该端口。

    【讨论】:

    • 有什么可以分享给你,让你知道我的MySQL服务器的配置。我不知道在哪里可以检查我在 Google 云 MySQL 面板中的连接限制。我机器的防火墙?那么我应该禁用我的防火墙吗?在谷歌云中,他们只是为我提供了公共 IP 地址,没有给出端口。我找不到解决这些连接问题的方法
    • 那么您是在使用 Google Cloud SQL 还是在 Compute Engine 实例上运行 MySQL? (例如,您是通过 Cloud Console 还是通过其他方法管理 MySQL 实例?)这是您最初的描述中并不清楚的重要一点。
    • 是的,我正在 Google Cloud SQL 中管理 MySQL 实例,抱歉,我忘了说清楚的解释。
    • 感谢先生花时间在我的查询和指导我。感谢您的努力。
    【解决方案2】:

    在 createConnection 方法中, 而不是主机

          host     : '34.93.236.175',
          user     : 'root',
          password : '',
          database : 'django_db'
    

    像这样使用套接字路径

    /cloudsql/[isntance-connection-name] 并连接到我的数据库

          socketPath: '/cloudsql/charbot2-ivjagp:asia-south1:auction-instance',
          user     : 'root',
          password : '',
          database : 'django_db'
    

    给您带来不便,抱歉 囚犯先生,我的朋友给我分享了这个解决方案,所以我在这里分享,以便其他人不会花时间在这些错误上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多