Express app getting disconnected right after the connection - mysql

I created a rest api with Express. on my server.js the i have my mysql connection.. though the connection is made at first. It's working on my local machine properly but when i deployed on AWS EC2 service then i am facing this problem..
server.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'myusername',
password: 'mypassword',
database: 'mydatabasename'
});
connection.connect(function (err) {
if (err) throw err;
console.log('connected!');
});
module.exports = connection;
i tried pm 2 handledisconnect() following other project function none worked.getting following error...
restapi#0.0.0 start /home/bcs_quiz_console/bcs-preparation-backend/restapi
npm start
connected!
events.js:180
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:97:28)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:525:10)
at Socket.emit (events.js:208:15)
at endReadableNT (_stream_readable.js:1168:12)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event at:
at Connection._handleProtocolError (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:426:8)
at Protocol.emit (events.js:203:13)
at Protocol._delegateError (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket. (/home/bcs_quiz_console/bcs-preparation-backend/restapi/node_modules/mysql/lib/Connection.js:97:28)
[... lines matching original stack trace ...]
at processTicksAndRejections (internal/process/task_queues.js:77:11) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
Thank anyone who can help in advance..

Related

how do i connect to a remote mysql database using node?

i have a node application that is duplicated both on the server (digital ocean) as well as my local machine. im trying to configure the local version so that i can use it as a 'sandbox' environment to make changes and edits before i push the update to the live application on the server.
the databaseconnection.js file contains the following.
const mysql = require('mysql');
function getConnection() {
return mysql.createPool({
connectionLimit: 100,
host: 'ipaddress',
port: 3306,
user: 'forge',
password: 'password',
database: 'mydatabase',
multipleStatements: true
});
}
module.exports = getConnection();
when i access the node application that is stored on the DO server, everything run fine, however, when i try to run it from my localhost environment, i get the following error.
Error: connect ETIMEDOUT
at PoolConnection.Connection._handleConnectTimeout (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:409:13)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
--------------------
at Protocol._enqueue (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:48:16)
at Pool.query (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\mysql\lib\Pool.js:202:8)
at Strategy.passport.use.OutlookStrategy [as _verify] (C:\MAMP\htdocs\WIGHTcloudDEV\config\passport-setup.js:58:18)
at C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-oauth2\lib\strategy.js:202:24
at Request._callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\passport-outlook\lib\strategy.js:157:5)
at Request.self.callback (C:\MAMP\htdocs\WIGHTcloudDEV\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
i slightly remember doing something with either ssh2 or tunnel forwarder in the past that pertained to this issue but i cant find any of my old files nor can i find anything online that clearly shows what im missing.

Trouble connecting to mysql in NodeJs on Windows 10

I have tried to connect NodeJS to mysql on Windows 10, but continuously failed to.
The following is the code, which from https://www.w3schools.com/nodejs/nodejs_mysql_create_db.asp
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
and the following is the error message when I run code.
if(err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16)
--------------------
at Protocol._enqueue (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\tete6\NodeJS\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\Users\tete6\NodeJS\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\Users\tete6\NodeJS\demo_db_connection.js:9:5)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11 {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
To solve this error, I tried ...
change "localhost" -> "127.0.0.1", but failed.
add "port: 3306", but failed.
check if the port is available to be connected, and I found that it's never my port, fireWall problem.
My xampp's mysql works well.
Even mongoDB module triggers this same error message.
I gleaned some info that this econnrefused error message is mainly the problem of NodeJS, not of sql module.
Is anyone to show me silver shining from this?
Have you started your XAMPP Server?
connect ECONNREFUSED 127.0.0.1:3306 -- this error came, when you not started your XAMPP server.
At the first you should right install mysql into windows 10
and see this video link how to connect to mysql with node js

MagicMirror SQL connection gets ENOTFOUND error even with an IP address

I am using MagicMirror project that can be found on the Github. I have added the MMM-MysqlQuery module. And i have added the database config to the config file. but still gets ENOTFOUND error.
project : https://github.com/MichMich/MagicMirror.git
I have set up the code in a raspberry pi 2. and the mysql database is in the pi localhost.when I run the project using pi terminal i get the ENOTFOUND error.
config: {
connection: {
host: "http://192.168.8.100",
port: 80,
user: "root",
password: "123456789",
database: "mirror"
}
according to understanding this configurations are passed as parameters to the
MMM-MysqlQuery modules' config.js file. which has the db connection initialization.
var mysql = require("mysql");
var con = mysql.createConnection({
host: process.argv[2],
port: process.argv[3],
user: process.argv[4],
password: process.argv[5],
database: process.argv[6]
});
con.connect(function(err) {
if (err) throw err;
con.query(process.argv[7], function(err, result) {
if (err) throw err;
console.log(result);
});
});
And the error i get is like this
Whoops! There was an uncaught exception...
{ Error: getaddrinfo ENOTFOUND http://192.168.8.100
http://192.168.8.100:80
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
--------------------
at Protocol._enqueue (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/protocol/Protocol.js:144:48)
at Protocol.handshake (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/protocol/Protocol.js:51:23)
at Connection.connect (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_modules/mysql/lib/Connection.js:118:18)
at Class.socketNotificationReceived (/home/pi/MagicMirror/modules/MMM-MysqlQuery/node_helper.js:25:17)
at Socket.<anonymous> (/home/pi/MagicMirror/modules/node_modules/node_helper/index.js:113:11)
at emitTwo (events.js:126:13)
at Socket.emit (events.js:214:7)
at /home/pi/MagicMirror/node_modules/socket.io/lib/socket.js:513:12
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://192.168.8.100',
host: 'http://192.168.8.100',
port: 80,
fatal: true }
please let me know if the code is not enough. and thank you guys for any help I can get.
http://192.168.8.100 isn't a valid hostname, because it isn't a hostname at all -- it's a URL, and includes the scheme http://, which also isn't appropriate for a database connection.
Use simply host: "192.168.8.100".
The next error you will encounter is a side effect of port: 80 also being incorrect. Port 80 is the standard port for an HTTP (web) server... but you are connecting to a database, not a web server. For MySQL, the correct value would be port: 3306 unless your MySQL installation uses a custom port (which it shouldn't).

node.js Mysql connection error

I'm trying to connect a Node.js app with a MySQL database which works for another app really well on a remote server with this code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "www.<dnw>.com",
port: <correct port number>,
user: "<correct Username>",
password: "<correct password>",
database: "<correct databasename>"
});
con.connect(function(err) {
if (err)
{
throw err;
}
});
con.end();
On execution I get this as a error message
/home/akiku/node/dnwbot/main.js:49
throw err;
^
Error: connect ECONNREFUSED 85.25.34.68:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
--------------------
at Protocol._enqueue (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/akiku/node/dnwbot/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/akiku/node/dnwbot/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/akiku/node/dnwbot/main.js:46:9)
at promises.push.Promise (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:439:29)
at new Promise (<anonymous>)
at TeleBot.event (/home/akiku/node/dnwbot/node_modules/telebot/lib/telebot.js:432:32)
at promise.then (/home/akiku/node/dnwbot/node_modules/telebot/lib/updates.js:92:33)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
What I thought it could be:
I checked the port and the host multiple times via SQL Query in phpmyAdmin. They are correct.
And I enabled in Plesk that ANY host can access the database.
The username and password is also correct.
What it should do:
Obviously just connect to the database. Nothing more.
Do you habe any clues what it could be?
Check you have access to remote server database.Try to connect using workbench or any other tool
I finally found the problem: The serveradmin blocked the port on a deeper server level which can't be accessed by Plesk.

Sails JS installation and database connection error

I am new to sails.js and i am trying to make it work but unable to install it properly.
During installation, first it was giving MSB4132 issue
then i installed visual studio 2010 to resolve that issue but then is starts showing around 2 pages of red text error.
due to this issue sails new project is working fine but when i am trying to connect it to my sql then it is showing issue.
changes i made for connecting to mysql
In SailsTest/config/connection.js
TestServer: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root',
password: '',
port: 4343,
database: 'sailstest'
},
In SailsTest/config/model.js
connection: 'TestServer',
migrate: 'safe'
In SailsTest/api/controller/userController.js
module.exports = {
get:function(req, res){
User.find()
.exec(function(err, users){
if(err){
console.log(err);
return res.json(err);
}
return res.json(users);
})
}
};
In SailsTest/config/route.js
'/': {
view: 'homepage'
},
'/user/get':'UserController.get'
after all this on
http://localhost:1337/user/get it is showing following error
{
"error": "E_UNKNOWN",
"status": 500,
"summary": "Encountered an unexpected error",
"raw": {}
}
this is the error log coming.
Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: connect ECONNREFUSED 127.0.0.1:4343
at afterwards (C:\Users\Ashish\Desktop\SailsTest\node_modules\sails-mysql\lib\connections\spawn.js:72:13)
at C:\Users\Ashish\Desktop\SailsTest\node_modules\sails-mysql\lib\connections\spawn.js:40:7
at Handshake.onConnect (C:\Users\Ashish\Desktop\SailsTest\node_modules\mysql\lib\Pool.js:54:9)
at Handshake.Sequence.end (C:\Users\Ashish\Desktop\SailsTest\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Protocol.handleNetworkError (C:\Users\Ashish\Desktop\SailsTest\node_modules\mysql\lib\protocol\Protocol.js:358:14)
at PoolConnection.Connection._handleNetworkError (C:\Users\Ashish\Desktop\SailsTest\node_modules\mysql\lib\Connection.js:382:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at emitErrorNT (net.js:1256:8)
at nextTickCallbackWith2Args (node.js:455:9)
at process._tickDomainCallback (node.js:410:17)