Error: Cannot enqueue Query after fatal error in mysql node - mysql

I am using felixge/node-mysql. Also I am using express-myconnection which prevents mysql timeout and in turn prevents killing of node server. What I am doing is logging the activities in mysql. The scenario is I have a file upload functionality once the file is uploaded I am performing different operations on the file. During every stage of processing I am logging those activities in database. This works fine if the file is small. If the file is large say 100 MB it takes some time to load so in the mean time the mysql server reconnects and creates a new connection but the logging code still uses the old reference. Error: Cannot enqueue Query after fatal error. So, my question is is there a way that i can use the new connection reference instead of the old one. There is a single function in which all the different phases of activities regarding file takes place. Any help is greatly appreciated. thanks
Hi #paul, if you have seen the gist link you can see that I have the upload.on('begin', function (fileInfo, reqs, resp) { } where I have logged the activity that file upload process has begin. Once the file is uploaded upload.on('end', function (fileInfo,request,response) { } is triggered. I am also logging some activity here. As I said in my question, if the file is big the upload takes time. In the mean time a new MySql connection is created but the insert query in 'end' event still refers to the old myconnection. So, I wanted to know how can I use the new mysql connection reference in this scenario? I hope this has explained the scenario better.

Actually, I decided to google your error for you, and after reading this thread: https://github.com/felixge/node-mysql/issues/832 I realized that you're not releasing the connection after the first query completes, and so the pool never tries to issue you a new one. You were correct that the stale connection might be the problem. here's how you fix that if it is:
upload.on('begin', function (fileInfo, reqs, resp) {
var fileType = resp.req.fields.file_type;
var originalFileName = fileInfo.name;
var renamedFilename = file.fileRename(fileInfo,fileType);
/*renaming the file */
fileInfo.name = renamedFilename;
/*start: log the details in database;*/
var utcMoment = conf.moment.utc();
var UtcSCPDateTime = new Date( utcMoment.format() );
var activityData = {
activity_type : conf.LIST_UPLOAD_BEGIN,
username : test ,
message : 'test has started the upload process for the file',
activity_datetime : UtcSCPDateTime
};
reqs.params.activityData = activityData;
req.getConnection(function(err,connection) {
var dbData = req.params.activityData;
var activity_type = dbData.activity_type;
console.dir("[ Connection ID:- ] "+connection.threadId+' ] [ Activity type:- ] '+activity_type);
var insertQuery = connection.query("INSERT INTO tblListmanagerActivityLog SET ? ",dbData, function(err, result) {
if (err) {
console.log("Error inserting while performing insert for activity "+activity_type+" : %s ",err );
} else {
console.log('Insert successfull');
}
/// Here is the change:
connection.release();
});
});
/*end: log the details in database;*/
});

I fixed mine. I always suspect that when errors occur along with my queries it has something to do with the MySQL80 Service being stopped in the background. In case other solutions failed. Try going to the task manager, head to the services, find MySQL80 and check if it is stopped, when it is, click start or set it as automatic so that it will start at the moment desktop is running.

For pool connection to work we need to comment out https://github.com/pwalczyszyn/express-myconnection/blob/master/lib/express-myconnection.js#L84 this line. Hope it helps anyone facing the same issue.
Also we can use single connection option.

I had the same issue. Came across one solution -
Re-Install MySQl and while doing so, in the configuration step, select "legacy encryption" option instead and finish the installation.
Hope this helps!

Related

MySQL executes sleep command when UPDATE query is used

I have created a discord bot that interacts with a mysql database but when you run a command that uses the UPDATE query it doesnt execute the update query but executes sleep , meaning the data in the DB isnt chnaged.
(from comment)
#client.command()
async def SetJob(ctx, uid: str, rank: str):
disout = exec("UPDATE users SET 'job'='{0}' WHERE identifier='{1}'".format(rank,uid))
if ctx.message.author == client.user:
return
if ctx.message.author.id not in whitelisted:
await ctx.send(embed=discord.Embed(title="You are not authorized to use this bot", description='Please contact Not Soviet Bear to add you to the whitelisted members list', color=discord.Color.red()))
return
else:
await ctx.send(embed=discord.Embed(title="Job Change", description="Job changed to '{0}' for Identifier'{1}'".format(rank,uid), color=discord.Color.blue()))
I assume your "bot" is periodically doing SHOW PROCESSLIST? Well, the UPDATE probably finished so fast that it did not see the query.
The Sleep says that the connection is still sitting there, but doing nothing. (There is no "sleep command"; "Sleep" indicates that no query is running at the instant.)
So, perhaps the question is "why did my update not do anything?". In order to debug that (or get help from us),
Check for errors after running the update. (You should always do this.)
Figure out the exact text of the generated SQL. (Sometimes there is an obvious syntax error or failure to escape, say, quotes.)

Prestashop 1.4 - How to execute an initial MySQL query on every request

Kind of old version of Prestashop, I know, but I need to execute an initial MySQL query on every request.
Where do I need to put the logic to be always executed?
Sort of initialization point the application always executes, no matter what URL is requested.
Thanks in advance.
Finally found the solution (and smartly, I think).
And I also think this approach could be applicable to other versions of Prestashop (1.5, 1.6, 1.7...).
In classes/MySQL.php file, within the connect function, I add my query just before returning $this->_link:
public function connect()
{
if (!defined('_PS_DEBUG_SQL_'))
define('_PS_DEBUG_SQL_', false);
if ($this->_link = mysql_connect($this->_server, $this->_user, $this->_password))
{
if (!$this->set_db($this->_database))
die('The database selection cannot be made.');
}
else
die('Link to database cannot be established.');
/* UTF-8 support */
if (!mysql_query('SET NAMES \'utf8\'', $this->_link))
die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
// removed SET GLOBAL SQL_MODE : we can't do that (see PSCFI-1548)
/** MY QUERY IS INSERTED HERE, USING $this->_link BY THE WAY **/
mysql_query('...', $this->_link);
return $this->_link;
}
In this way, 2 advantages arise:
I do not have to maintain a copy of database credentials other than Prestashop's
The query is executed in every request, both shop frontoffice and backoffice user interfaces.
I hope this can help anyone.

Unable to change the serverId of a node client using #rodrigogs/mysql-events Zongji event listener

I am using #rodrigogs/mysql-events to listen to DB events on an mysql DB. So far works pretty well, except that I am getting the following error when I have more than one client listening to the DB:
A slave with the same server_uuid/server_id as this slave has connected to the master
Through some research I was told that each client are seen as replication servers by the core server, and as such must each present a different serverId in order to avoid collision.
As per the #rodrigogs/mysql-events documentation, I did pass the "serverID" option as shown below, but the errors continue to happen, and the binlog only reports connections from Server ID 2. What am I missing?
const instance = new MySQLEvents(DBcon, {
startAtEnd: true,
serverId: 5,
excludedSchemas: {
mysql: true,
}
});
So I found the solution. Turns out that the above configuration is correct. I had an error in some downstream code.
I also improved it so that each client uses a different serverId, depending on the host environment:
const instance = new MySQLEvents(DBcon, {
startAtEnd: true,
serverId: process.env.HOSTNAME == ProdHost ? 5 : 66,
excludedSchemas: {
mysql: true,
}
});

listen mysql change event using zongji module not working

I am trying to listen mysql change event in my app using mysql-event module. For that I got to know that, i need to make sure if zongji module works fine.
So here is what I have tried so far:
--my server.js
var ZongJi = require('zongji');
var zongji = new ZongJi({
host : 'db_host',
user : 'root',
password : 'kakaun',
insecureAuth: true,
debug: true
});
zongji.start({
includeEvents: ['tablemap', 'writerows', 'updaterows', 'deleterows']
});
zongji.on('binlog', function(evt) {
console.log("+++++Inside binlog event++++");
evt.dump();
});
process.on('SIGINT', function() {
console.log('Got SIGINT.');
zongji.stop();
process.exit();
});
(Example from https://github.com/nevill/zongji/blob/master/example.js)
I have perfectly configured the binlog for mysql and it is creating the log as well.
But I am not able to log anything for the above console for any change event in my node app.
When I switched on debug= true option, I can see that for every change in database log, I get this debug log:
<-- BinlogHeader
BinlogHeader {}
<-- BinlogHeader
BinlogHeader {}
<-- BinlogHeader
BinlogHeader {}
So, that means my app is succeffully able to contact binlog of mysql, but how I know which row has changed or what has changed? Because zongzi.on('binlog', [Function]) not printing anything.
So after a lot of debugging, I found the problem. So here is something which I had overlooked and was doing wrong. I would like to post my mistake, so that other user may avoid doing this.
By default, if you enable binlog for mysql, it will be events based logging, which zongji module could not understand.
https://dev.mysql.com/doc/internals/en/binary-log-overview.html
Hence you need to enable row based logging, to get useful information (to know what has changed).
row based logging can be enabled using this extra statement in my.cnf file of mysql configuration file:
binlog_format = row
After adding this, and restarting the mysql server, every thing started working fine.

Dealing with duplicate keys in codeigniter mysql insert

What I want to do is something like this:
function registerUser($username, $password){
$this->db->insert('tblUsers', array('username'=>$username, 'password'=>md5($password)));
if($this->db->_error_number()==1062){
return "DUPLICATE";
}
return true;
}
However, at the moment if there is a duplicate key then its not letting me get to the _error_number() bit. Its displaying an error like this:
How do I stop codeigniter from bailing with an error and passing the error number to me to deal with appropriately?
Thanks
You can access MySQL error messages in Codeigniter using:
$this->db->_error_message();
Apparently DB_DEBUG needs to be set to false in the database config file:
Ensure DB_DEBUG is set to FALSE in the database config file, or
execution will halt when a mysql error occurs (it does not get thrown,
it justs exits from the php interpreter)
Link: http://codeigniter.com/forums/viewthread/79950/#413830
Suggestion:
$orig_db_debug = $this->db->db_debug;
$this->db->db_debug = FALSE;
RUN QUERY HERE
$this->db->db_debug = $orig_db_debug;