Alaukik Tandlekar
Alaukik Tandlekar

Reputation: 41

{ Error: connect ETIMEDOUT at Connection._handleConnectTimeout and wierd undefined messages

When I am trying to connect to my mysql database of google cloud it is showing me this error connect ETIMEDOUT plus undefined message. This error has taken so many days I can't figure out what could be the solution for this. I don't have past experience of this language. enter image description here

When i opened this error in detail it is displaying this:

{ 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 }

please help me to retrieve data from my database. I am referring this tutorial https://www.youtube.com/watch?v=v7k5vckSzNo&t=892s I also asked my issue in comments section but no reply came from them.

This is my index.js file

// 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 file

{
  "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"
  }
}

Your help is very much appreciated and this is part of my final year project. Further, I have to perform insert, update and delete operations on database.

Upvotes: 0

Views: 10353

Answers (3)

Zero
Zero

Reputation: 1

I had the same problem,solved it with this:

sudo iptables -A INPUT -p tcp -s [IP] --dport 3306 -j ACCEPT

Upvotes: 0

Alaukik Tandlekar
Alaukik Tandlekar

Reputation: 41

In createConnection method, Instead of host

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

use socketpath like this

/cloudsql/[isntance-connection-name] and it gets connected to my database

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

enter image description here Sorry for inconveniences Prisoner sir, my friend shared me this solution so I shared here so that others will not spend time on these errors

Upvotes: 2

Prisoner
Prisoner

Reputation: 50721

It is a little difficult to diagnose the exact problem, since we don't know the configuration of your MySQL server, but broadly speaking, this error is because there is some configuration in the network preventing the connection.

Offhand, I can think of a few possible places to look:

  • It sounds like your fulfillment is running using the Inline Editor. This uses Google Cloud Functions, so there may be a configuration in the function that prevents network connections.
    • Note that previous versions of the Inline Editor used Firebase Cloud Functions, which prevented network connections unless you were on a paid plan (even if you used the free tier). I don't think Google Cloud Functions has this same limitation, but it may.
  • Your MySQL/cpanel configuration may limit where you can connect from.
  • You may have a firewall in place that does not allow connections on the mysql port.
  • Similarly, you may have your MySQL configuration listening on a different port than standard. In this case, you need to make sure that port is open on the machine, that your firewall will let that port through, and that your code is configured to connect on that port.

Upvotes: 0

Related Questions