After a few hours of running my express node application the server loses connection to the mysql server. I am using a connection pool so it shouldn't be disconnecting. Here is how I initialize the pool:
pool = mysql.createPool({
connectionLimit : 10,
host : creds.mysql_host,
port : creds.mysql_port,
database : creds.mysql_db,
user : creds.mysql_user,
password : creds.mysql_pass,
multipleStatements : true
});
To talk to the mysql server I do:
pool.query("statement", [inputs], callback);
Could the server be crashing and the pool cannot reconnect? Do I need to do connection.release() even when using connection.query? The error the server reports is:
Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
Related
Trying to connect to a SQL database using javascript but I keep getting this error:
Database connection failed: Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/Users/------/PhpstormProjects/------/node_modules/mysql/lib/Connection.js:409:13)
at Object.onceWrapper (events.js:421:28)
at Socket.emit (events.js:315:20)
at Socket._onTimeout (net.js:480:8)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
Have scoured the web but cannot find any solution. What am I doing wrong? Here is my code:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : "----.---------.-------.amazonaws.com",
user : "--------",
password : "--------"
});
connection.connect(function(err) {
connection.end();
if (err) {
console.error('Database connection failed: ' + err.stack);
return;
}
console.log('Connected to database.');
});
This one I actually found out myself. I was using phpstorm as an IDE and trying to connect to sql through my app.js file. I also use an aws ec2 instance to connect to my database which had already established a connection previously. I used SSH to connect to my ec2 instance, then created an app.js in my /var/www/html folder and connected from there without a problem.
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.
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..
I have hosted an application in gcloud which uses mysql. But when the function crashes (error/exceptions) I need to end the connection. When the function gets crashed the logs emits :
error: Function worker killed by signal: SIGTERM
So how can I close my mysql connection (connection.end()) before the function gets terminated ?
I don't know how you can end the connection when your app crashes. But maybe you can use mysql.createPool(...); rather then mysql.createConnection(...);
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
This way you can set pooling options for the connection and let the MySQL sort of manages the termination.
error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: connect ECONNREFUSED 127.0.0.1:3306
at afterwards (/root/sails/sailsUserApp/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /root/sails/sailsUserApp/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
at Handshake.Sequence.end (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Protocol.handleNetworkError (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:364:14)
at PoolConnection.Connection._handleNetworkError (/root/sails/sailsUserApp/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:421:18)
at emitOne (events.js:96:13)
at Socket.emit (events.js:189:7)
at emitErrorNT (net.js:1280:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
connection.js -
Mysql:
{
adapter: 'sails-mysql',
host: 'localhost',
port: '3306',
user: 'root', //optional
password: 'sipl#1234', //optional
database: 'sails_users' //optional
}
models.js - connection: 'Mysql',
I am unable to figure-out the issue. port: 3306 is already mentioned in the configuration.
The MySQL connection parameters will depend on the MySQL configuration. The configuration file location and default connection access control list will depend on the OS that MySQL is running on and the options that were passed to the installation.
It may be expecting a Unix Socket connection rather than a TCP connection. Review your MySQL configuration file to see which connections and users are allowed. If it is allowing only socket connections there will be a specific unix socket it is expecting to use.
With that information you can set the configuration parameters accordingly.
To use a socket connection the parameters would be:
Mysql:
{
adapter: 'sails-mysql',
socketPath: 'path-to-socket/mysql.sock',
user: 'mysql-user',
password: 'mysql-password',
database: 'mysql-database'
}