All the Solutions I found for this Problem don't work for me.
I'm just loading one Dataset and after approximate 150 Request I get this Error:
Error: Packets out of order. Got: 1 Expected: 0
at Parser._tryReadPacketHeader (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Parser.js:470:15)
at Parser.write (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Parser.js:33:29)
at Protocol.write (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:223:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead (internal/stream_base_commons.js:181:23)
--------------------
at Protocol._enqueue (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\dev\node\webkoll\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\dev\node\webkoll\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\dev\node\webkoll\node_modules\mysql\lib\Pool.js:48:16)
at C:\dev\node\webkoll\dbHelper.js:35:22
at new Promise (<anonymous>)
at dbHelper.execQueryWithParams (C:\dev\node\webkoll\dbHelper.js:34:16)
at dbHelper.loadFinishedResultFlag (C:\dev\node\webkoll\dbHelper.js:68:21)
at C:\dev\node\webkoll\index.js:321:30
at Layer.handle [as handle_request] (C:\dev\node\webkoll\node_modules\express\lib\router\layer.js:95:5) { code: PROTOCOL_PACKETS_OUT_OF_ORDER', fatal: true}
I'm using node v12.14.1 and the npm package mysql v2.18.1.
I also set the max_allowed_packet to 1G but it did not help.
Here the Code i use to get the data:
class dbHelper {
constructor() {
const { host, user, password, database, connectionLimit } = config.db;
this.con = mysql.createPool({
connectionLimit,
host,
user,
password,
database
});
}
async execQueryWithParams(query, params) {
return new Promise((resolve, reject) => {
this.con.getConnection((err, connect) => {
if (err) {
console.log(err);
return reject(err)
}
connect.query(query, [params], (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
})
})
});
}
I found the Problem.
After using the npm package mysql2 I got the error message: "Too many connections".
I dumbly initialized my class way to often, so after using just one instance everything worked fine.
Try to increase the MAX_Packet_Allowed Memory - it works in my case
It can be - your pool connection is lost. Try checking wether it is still up.
You are doing some minor mistake for define variable and function definition on proper ways..
const mysql = require('mysql');
const con = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});
exports.handler = (event, context, callback) => {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
const sql = "select * from user_status";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
callback(null, result)
});
};
I am using the above code and now I m getting data from my RDS.🙂🙂
Note: you need to define your mysql credentials for host,user,password,port,database.
Related
I am trying to implement this solution to my code. However, I am having trouble trying to call a function in a callback function.
In my index.js file, I have this
const db = require('./models/db.js');
db.connectDb();
In my db.js file, I have the following:
const mysql = require('mysql');
const database = {
//Connects to the DB
connectDb : function () {
// Create connection
this.db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'example'
});
this.db.connect(function(err){
if(err){
console.log("An error has occured when trying to connect to db.");
throw err;
}
console.log("MySQL Connected...");
});
this.db.on('error', function (err) {
var currentdate = new Date();
consoleoccurredn error has occured # ', currentdate,' with error:', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
throw err;
}
});
},
handleDisconnect: function () {
this.connectDb();
}
}
module.exports = database;
The server starts up and MySQL connection has been successfully created. Since my intention is to handle the error connection lost, I restarted the server, and as expected, this.db.on('error', function (err) is executed, as reflected in the terminal.
MySQL Connected...
An error has occurred # [datetime] with error: Error: Connection lost: The server closed the connection.
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
However, I get this error:
/home/folder/models/db.js:27
this.handleDisconnect();
^
TypeError: this.handleDisconnect is not a function
at Connection.<anonymous> (/home/folder/models/db.js:27:22)
at Connection.emit (node:events:526:28)
at Connection._handleProtocolError (/home/folder/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (node:events:526:28)
at Protocol._delegateError (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
[nodemon] app crashed - waiting for file changes before starting...
My question: Does this error have to do with trying to call a function outside of a callback function? Is it possible to call a function outside of a callback function, and that callback function is inside an object's function? Or is the problem something else?
For your reference, here is what is outputted in the terminal.
MySQL Connected...
An error has occurred # [datetime] with error: Error: Connection lost: The server closed the connection.
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
fatal: true,
code: 'PROTOCOL_CONNECTION_LOST'
}
/home/folder/models/db.js:27
this.handleDisconnect();
^
TypeError: this.handleDisconnect is not a function
at Connection.<anonymous> (/home/folder/models/db.js:27:22)
at Connection.emit (node:events:526:28)
at Connection._handleProtocolError (/home/folder/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (node:events:526:28)
at Protocol._delegateError (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.end (/home/folder/node_modules/mysql/lib/protocol/Protocol.js:116:8)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:94:28)
at Socket.<anonymous> (/home/folder/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
[nodemon] app crashed - waiting for file changes before starting...
The problem calling this.handleDisconnect() in this code:
this.db.on('error', function (err) {
var currentdate = new Date();
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
throw err;
}
});
is that the value of this is not what you need it to be and that's why you get the error that this.handleDisconnect is not a function (it's probably undefined). A plain function callback will set its own value of this according to how the caller (which is your database) calls it. Instead, you can change it to an arrow function and it will retain the desired value of this. This is the main reason that arrow functions were invented (to preserve the lexical value of this):
this.db.on('error', (err) => {
var currentdate = new Date();
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
throw err;
}
});
Another problem here is that throw err in this context will not be useful. You will throwing back into some asynchronous context in your database and that's not anywhere that you can catch or handle that error or do anything useful with it. So, you will need a more useful way to process that error.
i have created express app with mysql database.
when i call mysql query it works fine and then i call my view. loading view take few minute(2-3 min) and app crashed with bellow error.
events.js:287
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (events.js:310:20)
at Protocol.EventEmitter.emit (domain.js:482:12)
at Protocol._delegateError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (events.js:310:20)
at Socket.EventEmitter.emit (domain.js:482:12)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
[nodemon] app crashed - waiting for file changes before starting...
i already spend 8- 10 hours.
please help me to resolve this issue.
thanks
Update:
const options = {
user: config.get('MYSQL_USER'),
password: config.get('MYSQL_PASSWORD'),
database:config.get('DATABASE'),
host: config.get('HOST'),
port: 3306
}
const connection = mysql.createConnection(options);
connection.connect( function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:"); throw err;
} else {
console.log("Connection established.");
}
});
use below
const options = { connectionLimit :10, user:config.get('MYSQL_USER'), password: config.get('MYSQL_PASSWORD'), database:config.get('DATABASE'), host: config.get('HOST'), port: 3306 }
const connection_pool = mysql.createPool(options);
connection_pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
provide connectionLimit according to you use this is pool size of connection
I just recently started seeing a bunch of Too many connections errors within my server. Our application uses Nodejs as the backend and MySql for our database. I'm honestly not sure as to where these errors are coming from and/or how this began propagating, but it's becoming quite a big issue lately since my application starts becoming unresponsive after a certain period of time. The code that I'm about to share was here before me, and I don't really quite understand if it was written properly or not. My guess is that whoever wrote it isn't closing connections to the database correctly, and a bunch of them are just staying open and never closing, which is what I assume is causing this error.
This is our pool.js file
import mysql from 'promise-mysql';
import env from '../../../env.config.json';
const db = async (sql, descriptor, serializedParameters = []) => {
return new Promise( async (resolve, reject) => {
try {
const connection = await mysql.createConnection({
//const connection = mysql.createPool({
host: env.DB.HOST,
user: env.DB.USER,
password: env.DB.PASSWORD,
database: env.DB.NAME,
port: env.DB.PORT
})
if (connection && env.ENV === "development") {
//console.log(/*"There is a connection to the db for: ", descriptor*/);
}
let result;
if(serializedParameters.length > 0) {
result = await connection.query(sql, serializedParameters)
} else result = await connection.query(sql);
connection.end();
resolve(result);
} catch (e) {
console.log("ERROR pool.db: " + e);
reject(e);
};
});
}
export default db;
We use sockets to call a db sequence like this one
//SaveMonthsTransactions
export const SaveMonthsTransactions = (obj) => {
return new Promise(async (resolve, reject) => {
//console.log(JSON.stringify(paperChanges))
try {
const callFunction = `CALL SaveMonthsTransactions('${obj}')`;
//console.log(callFunction);
const response = await db(callFunction, "SaveMonthsTransactions");
//console.log(response);
resolve(response[0]);
} catch (e) {
console.log("ERROR createJob.SaveMonthsTransactions: " + e);
reject(e);
}
});
};
Here are the errors I'm receiving
Server is up on port 3000.
(node:5316) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.
ERROR pool.db: Error: ER_CON_COUNT_ERROR: Too many connections
ERROR tableMNG.selectAllTechCallLogs: Error: ER_CON_COUNT_ERROR: Too many connections
(node:5316) UnhandledPromiseRejectionWarning: Error: ER_CON_COUNT_ERROR: Too many connections
at Handshake.Sequence._packetToError (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
at Protocol._parsePacket (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\Connection.js:103:28)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:442:20)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
--------------------
at Protocol._enqueue (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (C:\Users\micha\Dispatch-Council\node_modules\mysql\lib\Connection.js:130:18)
at connect (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:18:33)
at C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:52:9
at Promise._execute (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\debuggability.js:303:9)
at Promise._resolveFromExecutor (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Users\micha\Dispatch-Council\node_modules\bluebird\js\release\promise.js:79:10)
at new connection (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\lib\connection.js:51:12)
at Object.exports.createConnection (C:\Users\micha\Dispatch-Council\node_modules\promise-mysql\index.js:6:12)
at _callee$ (C:/Users/micha/Dispatch-Council/src/imports/API/pool.js:6:44)
at tryCatch (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:65:40)
at Generator.invoke [as _invoke] (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:303:22)
at Generator.prototype.(anonymous function) [as next] (C:\Users\micha\Dispatch-Council\node_modules\babel-polyfill\node_modules\regenerator-runtime\runtime.js:117:21)
at step (C:\Users\micha\Dispatch-Council\src\imports\API\pool.js:17:191)
at C:\Users\micha\Dispatch-Council\src\imports\API\pool.js:17:437
(node:5316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
These errors start occurring as soon as my application loads. I see that it's trying to call some db methods but it looks like they are being rejected, because there are too many connections to it.
I'm really in a rut here, so if anyone could please help me understand what needs to be done so that these connections can get closed and stop breaking my application that would be awesome! Thanks.
I have a lot of lambda invocations and I am always querying database so I want my lambda to be able to reusing connection, so I am establishing connection in constructor, like this:
let mysqlConnection = mysql.createConnection({
host: process.env.HOST,
user: process.env.DB_USER,
password: process.env.BDB_PWD,
port: process.env.DB_PORT,
database: process.env.DB_NAME
});
dbConn.connect(function(err) {
if (err) {
throw new Error('Error during connecting to db');
} else {
console.log("Database is connected");
}
});
And I have callbackWaitsForEmptyEventLoop set to false in my handler:
exports.handler = (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
event.Records.forEach(record => {
processEvent(JSON.parse(record.body), context);
});
};
When mysql connection fails I am destroying mysql connection, my query in provessEvent function looks like this:
mysqlConnection.query(sql, values, function (err, rows) {
if (err) {
mysqlConnection.destroy();
context.fail();
}
}
But I am getting a lambda errrors from time to time. I guess sometimes lambda wants to reuse a connection which is not available.
"Error: Error during connecting to db",
" at Handshake.<anonymous> (/var/task/lambda.js:34:13)",
" at Handshake.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:525:10)",
" at Handshake._callback (/var/task/node_modules/mysql/lib/Connection.js:491:16)",
" at Handshake.Sequence.end (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)",
" at Protocol.handleNetworkError (/var/task/node_modules/mysql/lib/protocol/Protocol.js:369:14)",
" at Connection._handleNetworkError (/var/task/node_modules/mysql/lib/Connection.js:421:18)",
" at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:417:8)",
" at Object.onceWrapper (events.js:286:20)",
" at Socket.emit (events.js:198:13)",
" at Socket.EventEmitter.emit (domain.js:448:20)"
Is there anything could I do to prevent this situation? Or should I destroy connection and connect once again?
I found this useful tool that could help you, https://github.com/jeremydaly/serverless-mysql you need just to install a npm module and configure it. Check also the #connection-backoff section.
I do not use MySQL but Mongodb in some projects. callbackWaitsForEmptyEventLoop was help.
'use strict';
const mysql = require('mysql'); // require mysql
// If 'client' variable doesn't exist
if (typeof client === 'undefined') {
// Connect to the MySQL database
var client = mysql.createConnection({
// your connection info
});
client.connect()
}
module.exports.handler = (event, context, callback) => {
// This will allow us to freeze open connections to a database
context.callbackWaitsForEmptyEventLoop = false;
client.query('SELECT * FROM `books`', function (error, results) {
callback(null, results)
});
}
Refer to AWS document and this tutorial
the mysql/node.js appears to connect then i "think" there may be a problem with the query event logic. Here's the code and error. I also have a separate/similar problem with postreSQL. I install npmed both in the same directory.
ryan#\Ryan:~/Desktop/node/datastores$ node MySQL
connection::connected
/home/ryan/Desktop/node/datastores/MySQL.js:20
if (err) throw err;
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/ryan/Desktop/node/node_modules/mysql/lib/Connection.js:123:18)
at Object.<anonymous> (/home/ryan/Desktop/node/datastores/MySQL.js:15:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
this is my server/mysql logic
var mysql = require('mysql');
var connectionConfig = {
host: 'localhost',
user: 'root',
password: '',
database: 'sakila'
};
var connection = mysql.createConnection( connectionConfig);
connection.connect(function(err) {
console.log('connection::connected');
});
connection.query('SELECT * FROM actor', function(err, rows, fields) {
if (err) throw err;
rows.forEach(function(row) {
console.log(row.first_name, row.last_name);
});
});
var actor = { first_name: 'Wil', last_name: 'Wheaton' };
connection.query('INSERT INTO actor SET ?', actor, function(err, results) {
if (err) throw err;
console.log(results);
});
connection.end(function(err) {
console.log('connection::end');
});
heres postre logic
var pg = require('pg');
var connectionString = 'postgres://ryan#localhost/postgres';
var client = new pg.Client(connectionString);
client.connect(function(err) {
if (err) throw err;
client.query('SELECT NOW() AS "THE TIME"', function( err, result ) {
if (err) throw err;
console.log(result.rows[0]);
client.end();
});
});
heres error
ryan#\Ryan:~/Desktop/node/datastores$ node postreSQL
/home/ryan/Desktop/node/datastores/postreSQL.js:8
if (err) throw err;
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
Your password seems to be empty in both connections.
You need to get the connection details for the database from your devOps / SysAdmin / whoever created it.
Ask for the hostname, port, user and password, and schema name; then put those in your init object:
// MySQL:
var connectionConfig = {
host: "", // Hostname + ":" + port (no need for :port if 3360)
user: "", // User
password: "", // Password
database: "" // Schema
};
// PostgreSQL:
var connectionString = 'postgres://user:password#hostname:port';