detecting changes in a mysql database - mysql

I have someone else's code and also a db. To understand the code, I want to see that If I run a particular php process, which tables are updated in the db. Something like I start a listner and then carry out the process. After that the listner will show me a list of changes that happened in the database. Is this possible?
Note: As I understand, mysql TRIGGER does something similar but it is for a specific table and also on trigger adds rows to another table. I just want a change log sort of a thing. Thanks in advance.

You could use logging - turn it on, run the code, turn it off, then review the log file.
This article talks you through how to do this:
http://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/

Related

Where to find mysql logs regarding user changes in phpmyadmin

I want to know, is there a log that saves all the changes made by users to a database just like in git where all the commits can be viewed by each user so that if any error occur we know who did it.If there is one how to get it? or how to create one that will do it?
Problem I'm facing is that a table's data has been altered by one of the developers but there is no way to find when and who did it and also I am struggling to find all the changes that has been done to the table.
Databases typically do not provide auditing as standard. I typically implement it within my application. However, for a faster result, there are some plugins for mySQL which you could try.

Clean, low-overhead way to send an email when a mysql/mariadb table is updated?

I need to send an email when a record is added to a table.
A bunch of googling has left me with the impression that the only choices are "bad" and "really bad" and was wondering if anybody had any clean, solid, reliable suggestions.
So far I've found:
Use a mysql plugin that sends the mail. I'd rather not do this because I have a perfectly good mail server and the database wasn't designed to send mail.
Poll the table periodically from an external program, look for changes and send the mail if appropriate. This is almost OK, but I'd rather skip the dead time between the record being added and the next poll.
I had considered using SELECT ... OUTFILE, however this is really limited because it won't overwrite the output file and the only way to change the filename is by building the query with dynamic SQL, which can't be used inside a trigger.
I could write a socket listener and have Mysql open the socket and tell the mail app there are records waiting, however there doesn't seem to be a way to open a socket from mysql.
It feels like I must be missing something here.
All I want is to run an external application when a record is added.
Has anybody run into a clean, low overhead way to do this?
Modify the code that is adding the record and have it do the notifications. If you put it in a try/catch block you will know for sure whether or not the record was added successfully.
Trigger on table(s) of interest to insert into other table (email queue).
Create a scheduled process to process the other table.

Making MySQL ignore an error

Disclaimer: I'm trying to do something very bad and if you think I shouldn't be doing this, that's wonderful but I'm still going to do it because I'm being told explicitly to either do this or show that it can't be done.
I have a piece of code that I have absolutely 0.00% control over. This piece of code cannot be changed or edited by me in any way, I don't have the source and have no way of obtaining it. I only have control over the MySQL db itself. The code I am working with connects to the MySQL db and attempts to do an update on a table I do not want updated. The update is pointless and destructive and should not happen but the code will not proceed unless the update query goes through.
I need MySQL to lie about the update. Is there any setting or option or anything to make it so when something connects to MySQL with an account that doesn't have write access to a table tries to do an update... MySQL will just tell the client "Yeah sure no problem, update complete." and then just not do it?

How do you write a Rails cron job Whenever gem?

The whenever gem is installed. I see all kinds of cron and whenever tutorials about scheduling tasks with minutes, hours, days etc. I watched the railscast video on cron/whenever.
But I've yet to find any examples about how to write a job itself, other than rake tasks.
I want to schedule a task that checks the database for changes. The idea is that the database will have tags that tell you which particular row has changed. Whenever should poll the database periodically to check for these changes. Then it hopefully can push, or let the client know it needs to update the page dynamically using ajax.
If I were doing this manually, I'd use commands like:
rails dbconsole
select blah from blah;
Is there a way to write mysql commands in whenever? Is this the correct/best way to poll the database for changes?
I know there are ways to poll a database from mysql itself, but I've been specifically told to do it from the rails side.
I'm a newbie to all of these technologies (Rails, databases, ajax) so that's probably why the answer isn't clear to me.
On the client end, I have buttons that use jquery to add/delete/change row data, just to assure myself I know how to change things in the table once I can get stuff from the database. Those buttons will eventually be removed.
Right now, the page uses ajax to refresh the entire html table. But they would like just a row refresh/update through ajax.
Look at the RailsCast for cron/whenever again. You'll notice an example line of code like this:
runner "MyModel.some_process"
The code in the strings is evaluated and run. So whatever you want whenever to run, just write that code yourself and have a way for it to be called.
So maybe you create a class named DatabaseWatcher and store it in lib which has a class level method named .run, you'd do the following:
runner "DatabaseWatcher.run"
And that's it. In your .run method is where you'd put your logic. As for how to actually write that code, that depends on your requirements. Are you looking for if the updated_at time is within 1 minute of now? Do you store a time when you last checked the DB, and then you can see if the updated_at time is greater than that? Do you have a table that stores every time the model is changed? That all depends on you.

Is there any event which is called whenever there is a change in the MYSQL database data?

Is there any event which detects changes in the database tables? Im trying to do a chat box. It works fine. Except that it needs to be refreshed at regular seconds of interval to get the data. But, this would eat up lot of server load. SO, i was thinking of a event which triggers any changes in the database data. Suggest me the best solution.
You could use triggers as presented in the comments. Problem with that: a trigger can only be triggered to execute an sql statemnt. So you could us a trigger, and create some kind of "activity" table with proper indexes. Problem: you still need to check this activity table! To reduce load, you should write a server component to your chat program that notifeis all clients. This way, only the server has to check to database regularly and not each client.
Without more information what language you are writing in, we can not help you more I guess.