mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.28k stars 2.52k forks source link

ETIMEDOUT while connecting to RDS #1388

Closed nizarp closed 8 years ago

nizarp commented 8 years ago

Hey

This is not an issue. But I need help to find out what I am doing wrong. I am trying to connect to my Amazon RDS instance. But I am getting "connect ETIMEDOUT" error.

At the same time I am able to connect from my local MySQL Workbench with the same details.

Here is my code:

var connection = mysql.createConnection({
        host     : 'xxxxxxxx.crhmtrscnbgt.us-east-1.rds.amazonaws.com',
        user     : 'my_user',
        password : 'my_passowrd',
        database : 'my_db',
        port     : '3306',
        debug    : true
});  

connection.connect(function(err) {
        if (err) {
                console.error('error connecting: ' + err.stack);
                return;
        }
        console.log('connected as id ' + connection.threadId);
});

var query = "SELECT * FROM users";            

connection.query(query, function(err, rows) {                
        if (err) throw err;                
        console.log(rows);
});

Output is:

2016-04-12T13:32:34.367Z    fa04401a-00b2-11e6-9a23-2dd70e33cc5d    error connecting: Error: connect ETIMEDOUT 
at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:412:13) 
at Socket.g (events.js:180:16) 
at Socket.emit (events.js:92:17) 
at Socket._onTimeout (net.js:327:8) 
at _makeTimerTimeout (timers.js:438:11) 
at Timer.unrefTimeout [as ontimeout] (timers.js:502:5) 
-------------------- 
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:141:48) 
at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol/Protocol.js:52:41) 
at Connection.connect (/var/task/node_modules/mysql/lib/Connection.js:123:18) 
at Object.Workout.save (/var/task/storage.js:32:24) 
at Object.storage.saveWorkout (/var/task/storage.js:62:28) 
at intentHandlers.NewExcerciseIntent (/var/task/intentHandlers.js:19:17) 
at AlexaSkill.eventHandlers.onIntent (/var/task/AlexaSkill.js:65:27) 
at AlexaSkill.requestHandlers.IntentRequest (/var/task/AlexaSkill.js:28:37) 
at AlexaSkill.execute (/var/task/AlexaSkill.js:105:24) 
at exports.handler (/var/task/index.js:9:19)
dougwilson commented 8 years ago

Hi @nizarp, there isn't much we can really help with here, as the error you have provided indicates that the TCP connection could not be established within 10 seconds to the RDS host. Typically this means you have some kind of firewall rule dropping traffic somewhere between your code and the RDS host. I hope that helps!

sandhibaljeet commented 8 years ago

@dougwilson , will you help me to solve this problem...

Error: connect ETIMEDOUT
    at Connection._handleConnectTimeout (/var/www/html/server1/node_modules/mysql/lib/Connection.js:425:13)
    at Socket.g (events.js:286:16)
    at emitNone (events.js:86:13)
    at Socket.emit (events.js:185:7)
    at Socket._onTimeout (net.js:333:8)
    at tryOnTimeout (timers.js:228:11)
    at Timer.listOnTimeout (timers.js:202:5)
    --------------------
    at Protocol._enqueue (/var/www/html/server1/node_modules/mysql/lib/protocol/Protocol.js:141:48)
    at Protocol.handshake (/var/www/html/server1/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/var/www/html/server1/node_modules/mysql/lib/Connection.js:136:18)
    at Object.<anonymous> (/var/www/html/server1/app1.js:35:16)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true

This is my code :-

var bodyParser = require('body-parser');
var express = require('express');
var config = require('./config/config');
var io = require('socket.io')();
var openDB = require('json-file-db');
var db = openDB('db.json');
var mysql = require('mysql');
var gcm = require('node-gcm-service');
var apn = require('apn');

var app = express();
app.listen(config.HTTP_PORT); // 3000
app.use(bodyParser.json());

io.listen(config.SOCKET_PORT);//7777

console.log("Server is up");
var connection = mysql.createConnection({  
    host : 'xx.xx.xxx.xxx',
    user : 'root',
    password : 'my_password',
    database : 'my_db'
    });

    connection.connect(function(error){
        if(!!error)
        {
            console.log(error);
            console.log('Error');
        }
        else
        {
            console.log('connected');
        }
    });
nizarp commented 8 years ago

@sandhibaljeet In my case, it was a Lambda function and I had to increase the Timeout setting for the Lambda function to fix this. Are you also trying this from a Lambda function. If so, try increasing the the Timeout.

sandhibaljeet commented 8 years ago

I am new o this, please tell me how to do settings

nizarp commented 8 years ago

I just updated my last comment. Please check.

sandhibaljeet commented 8 years ago

Will you please give me some code snippet that how to do settings. Because i don't know in which file or where to increase the time. I am totally new to this

nizarp commented 8 years ago

Did you try adding a timeout?

var connection = mysql.createConnection({
    host : 'xx.xx.xxx.xxx',
    user : 'root',
    password : 'my_password',
    database : 'my_db',
    timeout: 60000
});
sandhibaljeet commented 8 years ago

still that error

nizarp commented 8 years ago

@sandhibaljeet Were you able to connect to the database directly, (to make sure the DB works fine)? Are you available on google chat?

sandhibaljeet commented 8 years ago

@nizarp there is another error m facing right now, please help me, these are server logs .In mysql query i fetch all usernames and print them, but after fetching the first username there is error. connected Test server ping succes successfully executed john /var/www/html/server1/node_modules/mysql/lib/protocol/Parser.js:78 throw err; // Rethrow non-MySQL errors ^

RangeError: Invalid status code: 0 at ServerResponse.writeHead (_http_server.js:192:11) at ServerResponse._implicitHeader (_http_server.js:157:8) at ServerResponse.OutgoingMessage.end (_http_outgoing.js:558:10) at ServerResponse.send (/var/www/html/server1/node_modules/express/lib/response.js:205:10) at Query._callback (/var/www/html/server1/app1.js:80:30) at Query.Sequence.end (/var/www/html/server1/node_modules/mysql/lib/protocol/sequences/Sequence.js:85:24) at Query._handleFinalResultPacket (/var/www/html/server1/node_modules/mysql/lib/protocol/sequences/Query.js:144:8) at Query.EofPacket (/var/www/html/server1/node_modules/mysql/lib/protocol/sequences/Query.js:128:8) at Protocol._parsePacket (/var/www/html/server1/node_modules/mysql/lib/protocol/Protocol.js:280:23) at Parser.write (/var/www/html/server1/node_modules/mysql/lib/protocol/Parser.js:74:12) ~

nizarp commented 8 years ago

This is entirely a different issue. Let's not discuss this here.

cesarpachon commented 7 years ago

I am experiencing a similar issue. when connecting from a local express server, all works. but from lambda, I got the TIMEOUT error but only on INSERT operations.. the SELECT operations works fine!

nizarp commented 7 years ago

Did you try increasing the the Timeout for Lambda function?

forrest-akin commented 7 years ago

For AWS Lambda: 10 seconds didn't work for me, but increasing timeout to 15 seconds is working so far

leojle commented 6 years ago

Hey guys, the way I solved this issue was to update the security group in aws since RDS generates a default group with the local machine ip address. If you are trying to connect to the database from another app that is in another instance, you should add the ip address in the security group. selection_146 Hope it helps!

Bizbinus commented 6 years ago

@leojle Thank you! I'm new to lambda functions and I spent an entire day debugging trying to figure out why I couldn't connect. For others running into this problem and making it all the way down here without a fix:

On your Lambda function, specify your VPC, subnets, and Security Groups. Then, like @leojle mentioned, you need to add the Security Group that you selected on your Lambda function to the Security Group you have defined for your RDS( In my case they were both the same Security Group ). You have to add the Group Id to the Inbound rules( you can add an IP address or a Group Id to the rules set ).

ruchigaba commented 5 years ago

hii am getting the same error and i tried to add timeout?

var connection = mysql.createConnection({ host : 'xx.xx.xxx.xxx', user : 'root', password : 'my_password', database : 'my_db', timeout: 60000 }); please someone help me out

davidchang commented 5 years ago

@ruchigaba your problem is likely a security groups problem, as the comment right above yours suggests. hope you can figure it out, this stuff's tricky :|

dethfire-zz commented 4 years ago

I'm trying to connect to RDS from EC2 and still get this ETIMEOUT issue

agarhy commented 4 years ago

Solved by updating RDS security group inbound rule to accept from "0.0.0.0/0". If you need more secure config, attach Lambda to new SG and add that new lambda-SG in RDS-SG inbound rules.

davidsanchezr81 commented 3 years ago

I had the same issue and @agarhy was right. Set the security group inbound rule to accept from "0.0.0.0/0".