Connecting remote mysql via local machine : node expressjs - mysql

I am trying to connect to remote mysql(hosted on digitalocean server) and using the db for code in local machine.
When I start the node server and request for data, I get the following error.
{"ERRORMSG":"Something went wrong.{\"code\":\"ENOTFOUND\",\"errno\":\"ENOTFOUND\",\"syscall\":\"getaddrinfo\",\"hostname\":\"http://ip.of.remote.server/\",\"host\":\"http://ip.of.remote.server/\",\"port\":3306,\"fatal\":true}"}
After the first attempt, if I refresh again I get different error
{\"code\":\"PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR\",\"fatal\":false}
Connection is made like this:
connection = mysql.createConnection({
connectionLimit: 10000, //important
host: 'http://ip.of.remote.server/',
user: 'user',
password: 'password',
database: 'db',
multipleStatements: true,
connectionLimit : 100,
waitForConnections : true,
queueLimit :0,
debug : true,
wait_timeout : 28800,
connect_timeout :10
});
Call is made like this
app.get('/WSgetdata', function (req, res) {
var q = "CALL sp_get_data();"
var obj = {};
connection.connect();
connection.query(q, function (err, rows) {
if (err) {
obj.ERRORMSG = JSON.stringify(err);
}
else if((rows[rows.length-1])[0].ERRORSTATUS == 0){
//do something
}
res.end(JSON.stringify(obj))
});
});
What is wrong. It works fine when code and db are on same machine.
Also I have grant access to db from desired IP.
TRACE
Trace
at Query._callback (d:/project/config\webservice.js:397:25)
at Query.Sequence.end (d:/project/node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at d:/project/node_modules\mysql\lib\protocol\Protocol.js:390:18
at Array.forEach (native)
at d:/project/node_modules\mysql\lib\protocol\Protocol.js:389:13
at doNTCallback0 (node.js:407:9)
at process._tickCallback (node.js:336:13)
{ [Error: getaddrinfo ENOTFOUND http://ip.of.remote.server/ http://ip.of.remote.server/:3306]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://ip.of.remote.server/',
host: 'http://ip.of.remote.server/',
port: 3306,
fatal: true }
Error: getaddrinfo ENOTFOUND http://ip.of.remote.server/ http://ip.of.remote.server/:3306
at errnoException (dns.js:25:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
--------------------
at Protocol._enqueue (d:/project/node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (d:/project/node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (d:/project/node_modules\mysql\lib\Connection.js:119:18)
at d:/project/config\webservice.js:387:20
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
at next (d:/project/node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (d:/project/node_modules\express\lib\router\route.js:91:3)
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
at d:/project/node_modules\express\lib\router\index.js:267:22
at Function.proto.process_params (d:/project/node_modules\express\lib\router\index.js:321:12)
Trace
at Query._callback (d:/project/config\webservice.js:397:25)
at Query.Sequence.end (d:/project/node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Protocol._validateEnqueue (d:/project/node_modules\mysql\lib\protocol\Protocol.js:218:6)
at Protocol._enqueue (d:/project/node_modules\mysql\lib\protocol\Protocol.js:129:13)
at Connection.query (d:/project/node_modules\mysql\lib\Connection.js:197:25)
at d:/project/config\webservice.js:388:20
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
at next (d:/project/node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (d:/project/node_modules\express\lib\router\route.js:91:3)
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }
Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (d:/project/node_modules\mysql\lib\protocol\Protocol.js:193:16)
at Protocol._enqueue (d:/project/node_modules\mysql\lib\protocol\Protocol.js:129:13)
at Connection.query (d:/project/node_modules\mysql\lib\Connection.js:197:25)
at d:/project/config\webservice.js:388:20
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
at next (d:/project/node_modules\express\lib\router\route.js:110:13)
at Route.dispatch (d:/project/node_modules\express\lib\router\route.js:91:3)
at Layer.handle [as handle_request] (d:/project/node_modules\express\lib\router\layer.js:82:5)
at d:/project/node_modules\express\lib\router\index.js:267:22
at Function.proto.process_params (d:/project/node_modules\express\lib\router\index.js:321:12)

Replace
host: 'http://ip.of.remote.server/',
with
host: 'ip.of.remote.server',

Related

About node.js with mysql

I'm using node.js with mysql database.
I'm using the config like this:
var mysql = require('mysql');
var client = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'java1234',
port: '3306',
database: 'sample'
});
and then I get these error messages:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user
'root'#'localhost' (using password: NO)
at Handshake.Sequence._packetToError (C:\node\test11_users\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket (C:\node\test11_users\node_modules\mysql\lib\protocol\sequences\Handshake.js:124:18)
at Protocol._parsePacket (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (C:\node\test11_users\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket. (C:\node\test11_users\node_modules\mysql\lib\Connection.js:91:28)
at Socket. (C:\node\test11_users\node_modules\mysql\lib\Connection.js:502:10)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
--------------------
at Protocol._enqueue (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\node\test11_users\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\node\test11_users\node_modules\mysql\lib\Connection.js:118:18)
at Connection._implyConnect (C:\node\test11_users\node_modules\mysql\lib\Connection.js:453:10)
at Connection.query (C:\node\test11_users\node_modules\mysql\lib\Connection.js:198:8)
at C:\node\test11_users\routes\users.js:26:10
at Layer.handle [as handle_request] (C:\node\test11_users\node_modules\express\lib\router\layer.js:95:5)
at next (C:\node\test11_users\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\node\test11_users\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\node\test11_users\node_modules\express\lib\router\layer.js:95:5)
but I can't solve this problem.
thanks for your explain easily.
Remove port from connection options and try
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'java1234',
database : 'sample'
});
connection.connect();

Error in connecting to DB: Error: Handshake inactivity timeout

I am running a node.js server which is using mysql DB. I was working fine but suddenly it has started throwing following error.
Below is the stacktrace of the error:
Error in connecting to DB: Error: Handshake inactivity timeout
at Handshake.<anonymous> (/home/ubuntu/backend/node_modules/mysql/lib/protocol/Protocol.js:164:17)
at emitNone (events.js:86:13)
at Handshake.emit (events.js:185:7)
at Handshake._onTimeout (/home/ubuntu/backend/node_modules/mysql/lib/protocol/sequences/Sequence.js:129:8)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
--------------------
at Protocol._enqueue (/home/ubuntu/backend/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/ubuntu/backend/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at PoolConnection.connect (/home/ubuntu/backend/node_modules/mysql/lib/Connection.js:130:18)
at Pool.getConnection (/home/ubuntu/backend/node_modules/mysql/lib/Pool.js:48:16)
at /home/ubuntu/backend/followcopserver.js:232:11
at Layer.handle [as handle_request] (/home/ubuntu/backend/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/backend/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/ubuntu/backend/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/backend/node_modules/express/lib/router/layer.js:95:5)
at
Any help is really appreciated.

sequelize: not able to login via localhost?

My question is kind of similar to this one: Cant connect to localhost database with Sequelize (nodejs), but it has some difference and I couldn't use the answer provided.
I am trying to access mysql server from docker container. The access is provided via mapping mysqld.sock. The basic connection is verified. For example, inside container I can run the command as below.
# mysql --host=localhost --user=root --password=...
However, I cannot run using "127.0.0.1" as host. This might be something to look for regarding what I need to do next.
Then I try to verify mysql access using a nodejs program based on Sequelize as below.
var Sequelize = require('sequelize')
var sequelize = new Sequelize('mysql', 'root', '...', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
},
});
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
})
.catch(function (err) {
console.log('Unable to connect to the database:', err);
});
When I run it, it reports connection refused.
Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
at Handshake._callback (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:80:20)
at Handshake.Sequence.end (/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
at Protocol.handleNetworkError (/node_modules/mysql/lib/protocol/Protocol.js:364:14)
at Connection._handleNetworkError (/node_modules/mysql/lib/Connection.js:428:18)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1278:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'SequelizeConnectionRefusedError',
message: 'connect ECONNREFUSED 127.0.0.1:3306',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
--------------------
at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/node_modules/mysql/lib/Connection.js:130:18)
at /node_modules/sequelize/lib/dialects/mysql/connection-manager.js:75:16
at Promise._execute (/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/node_modules/bluebird/js/release/promise.js:77:14)
at ConnectionManager.connect (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:55:10)
at ConnectionManager.<anonymous> (/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:247:43)
at ConnectionManager.tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:143:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
--------------------
at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/node_modules/mysql/lib/Connection.js:130:18)
at /node_modules/sequelize/lib/dialects/mysql/connection-manager.js:75:16
at Promise._execute (/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/node_modules/bluebird/js/release/promise.js:77:14)
at ConnectionManager.connect (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:55:10)
at ConnectionManager.<anonymous> (/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:247:43)
at ConnectionManager.tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:143:10)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true } }
I suspect the problem is Sequelize tries to translate "localhost" which is given in the program to "127.0.0.1", and accessing mysql via 127.0.0.1 does not work inside container.
Thanks for the help.
solved (well, in a way), using dialectOptions
dialectOptions: {
socketPath: '/var/run/mysqld/mysqld.sock',
...
}

PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR on node.js server using mysql DB running on Ubuntu machine

I am running a server on AWS EC2 instance on ubuntu using node.js and MYSQL.It was working fine for 3 days, suddenly it stopped working.
On Googling i found that was due to lost connection in MYSQL DB. But could not find the exact cause of the fatal.
I am using following code for MYSQL connection
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : process.env.DB_PASSWORD
});
connection.query('USE MY_DB');
And for querying like this
app.get('/user', function(req, res){
connection.query('SELECT * FROM movie', function(err, rows){
var users = {"users" : rows};
res.send(users);
});
});
And log for the fatal:
2017-01-20T00:22:23.721Z - [31merror[39m: GET /users : Error: ER_SERVER_SHUTDOWN: Server shutdown in progress
at Query.Sequence._packetToError (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Query.ErrorPacket (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)
at Protocol._parsePacket (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Parser.js:74:12)
at Protocol.write (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:109:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Connection.query (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:214:25)
at /home/ubuntu/myserver/myserver.js:125:14
at Layer.handle [as handle_request] (/home/ubuntu/myserver/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/myserver/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/myserver/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/myserver/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/myserver/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/myserver/node_modules/express/lib/router/index.js:330:12)
at next (/home/ubuntu/myserver/node_modules/express/lib/router/index.js:271:10)
one more log:
2017-01-20T00:22:24.189Z - [31merror[39m: deleteRecords : Error: connect ECONNREFUSED 127.0.0.1:3306
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
--------------------
at Protocol._enqueue (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:136:18)
at Connection._implyConnect (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:467:10)
at Connection.query (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:212:8)
at Object.<anonymous> (/home/ubuntu/myserver/myserver.js:21:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
2017-01-20T00:22:28.135Z - [31merror[39m: GET /users : Error: Cannot enqueue Query after fatal error.
at Protocol._validateEnqueue (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:199:16)
at Protocol._enqueue (/home/ubuntu/myserver/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Connection.query (/home/ubuntu/myserver/node_modules/mysql/lib/Connection.js:214:25)
at /home/ubuntu/myserver/myserver.js:125:14
at Layer.handle [as handle_request] (/home/ubuntu/myserver/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ubuntu/myserver/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/ubuntu/myserver/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ubuntu/myserver/node_modules/express/lib/router/layer.js:95:5)
at /home/ubuntu/myserver/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/ubuntu/myserver/node_modules/express/lib/router/index.js:330:12)
I want to know the exact issue and the solution. Any help is appreciated.

Error while performing the database query: Error: Handshake inactivity timeout

I am using mysql in Node.js 6.7.0 to get data from database
connection.query('SELECT * FROM table', function(err, results){
if(err){
console.log('Error while performing the database query:', err);
return;
}else{
console.log('Query results:', results);
}
});
I was normally opening the page (which was previously working) when I got an error:
Error while performing the database query: Error: Handshake inactivity
timeout
The server didn't stop running, I refreshed the page and everything is ok...
It never happened before, what does this mean?
This is the full error that I got in the log:
Error while performing the database query: { Error: Handshake inactivity timeout
at Handshake.<anonymous> (/Users/johndoe/myproject/node_modules/mysql/lib/protocol/Protocol.js:160:17)
at emitNone (events.js:86:13)
at Handshake.emit (events.js:185:7)
at Handshake._onTimeout (/Users/johndoe/myproject/node_modules/mysql/lib/protocol/sequences/Sequence.js:127:8)
at tryOnTimeout (timers.js:232:11)
at Timer.listOnTimeout (timers.js:202:5)
--------------------
at Protocol._enqueue (/Users/johndoe/myproject/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/johndoe/myproject/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at PoolConnection.connect (/Users/johndoe/myproject/node_modules/mysql/lib/Connection.js:136:18)
at Pool.getConnection (/Users/johndoe/myproject/node_modules/mysql/lib/Pool.js:48:16)
at Pool.query (/Users/johndoe/myproject/node_modules/mysql/lib/Pool.js:200:8)
at /Users/johndoe/myproject/myscript.js:92:13
at Layer.handle [as handle_request] (/Users/johndoe/myproject/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/johndoe/myproject/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/johndoe/myproject/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/johndoe/myproject/node_modules/express/lib/router/layer.js:95:5)
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: 10000 }