Author Topic: Log Maintenance on MySQL affected tables in a log table  (Read 745 times)

0 Members and 1 Guest are viewing this topic.

Offline s.prema@yahoo.comTopic starter

  • Irregular
  • Posts: 25
    • View Profile
Log Maintenance on MySQL affected tables in a log table
« on: February 05, 2010, 01:29:00 AM »
Hello

How to maintain and update a log table often for a database????
Like storing the affected table name, column name, user, time...

Thanks in advance

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #1 on: February 05, 2010, 04:58:41 AM »
Triggers:
A trigger is defined to activate when an INSERT, DELETE, or UPDATE statement executes for the associated table. A trigger can be set to activate either before or after the triggering statement. For example, you can have a trigger activate before each row that is inserted into a table or after each row that is updated
(http://dev.mysql.com/doc/refman/5.0/en/triggers.html)

But you will have to define a trigger for every table you want to keep a log for..

(i think those guys here: http://www.hotscripts.com/forums/php/56694-log-maintenance-mysql-affected-tables-log-table.html will give the same answer...  ;) )
« Last Edit: February 05, 2010, 05:00:25 AM by jskywalker »

Offline s.prema@yahoo.comTopic starter

  • Irregular
  • Posts: 25
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #2 on: February 05, 2010, 05:08:50 AM »
Hmmm :) I knew intelligent people would find my posts on other forums easily
Thank u
But I m having having more than 100 tables............

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #3 on: February 05, 2010, 06:47:34 AM »
ok, i tried
1) create a table for the logging
Code: [Select]
CREATE TABLE `LOGGING` (
  `id` bigint(20) NOT NULL auto_increment,
  `table` varchar(20) default NULL,
  `username` varchar(20) default NULL,
  `logtime` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

2) create a script which will create the triggers (in this example i use my 'test' database...
Code: [Select]
#!/bin/bash
echo "DELIMITER ;;" >tmp.sql

for f in `mysqlshow test | sed -e 's/|//g' | grep -v '-'`;
do
echo 'CREATE TRIGGER t_'$f' AFTER INSERT ON '$f' FOR EACH ROW BEGIN insert into LOGGING (`table`, `username`, `logtime`) values ("'$f'",CURRENT_USER(), NOW()); end ;; ' >>tmp.sql ;
done
echo "DELIMITER ;" >>tmp.sql

3) its a quick and dirty script, so you have to delete some lines from the tmp.sql script...

at least the 1st three lines, and the line that whould create a trigger for the 'logging' table....

4) when you think its OK, you can execute this script to create the triggers in your database...

5) this works for INSERTS, you have to tweak this do the same for UPDATES and DELETES....
« Last Edit: February 05, 2010, 06:59:19 AM by jskywalker »

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #4 on: February 05, 2010, 07:12:55 AM »
for f in `mysqlshow test | sed -e 's/|//g' | grep -v '-'`;

Code: [Select]
for f in `mysqlshow test | sed '1,4d;s/|//g;$d' | grep -v 'LOGGING'`;
this might be code....
now you dont have to delete thos 1st three lines, and the line for the 'LOGGING'-table,  anymore.. ;-)

Offline s.prema@yahoo.comTopic starter

  • Irregular
  • Posts: 25
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #5 on: February 08, 2010, 02:37:58 AM »
Error occurring

<?php
#!/bin/bash

$hostname='localhost';
$un= 'root';
$pwd='';
$db='test';
$link=mysql_connect($hostname, $un, $pwd);
if(!$link)
  die('Could not connect: ' . mysql_error());
if(!mysql_select_db($db,$link))
  die('Could not link db: ' . mysql_error());
$f='ttt'; 
$d='DESCRIPTION';

echo "DELIMITER ;;" >tmp.sql ;

for f in "mysqlshow test | sed '1,4d;s/|//g;$d' | grep -v 'LOGGING'";
do
   echo "CREATE TRIGGER t_'$f' AFTER INSERT ON '$f' FOR EACH ROW BEGIN insert into LOGGING (table, username, logtime) values ('$f',CURRENT_USER(), NOW()); end ;; " >>tmp.sql ;
done
echo "DELIMITER ;" >>tmp.sql

?>

can u explain the mysqlshow command..
$f holds the table name whereas $d ??

giving error as Parse error: syntax error, unexpected T_STRING, expecting '(' in C:\xampp\htdocs\xampp\Prema\trig.php on line 16

ie., in the command..
expalin pls...


Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #6 on: February 08, 2010, 03:15:21 AM »
Thought about enabling query log and then parsing what's saved in it?
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #7 on: February 08, 2010, 06:11:39 AM »
the 1st line in my script says:
Code: [Select]
#!/bin/bash
this indicates that this is not a PHP-script, but a linux-BASH-script.
you are trying to run the linux-script, as i it was PHP-code

Code: [Select]
mysqlshow testThis is a linux command (it also comes with the windows-install of MySQL), which will show all tablenames in the database 'test'

Offline s.prema@yahoo.comTopic starter

  • Irregular
  • Posts: 25
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #8 on: February 08, 2010, 07:19:31 AM »
All ok...
Understood.  But php error  in that 'for' statement...
dont know how to correct.....
how to insert linux for statement in php??

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #10 on: February 08, 2010, 12:36:51 PM »
a solution in PHP might be better, because this is PHPfreaks...

Code: [Select]
select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='test' ;This MySQL statement returns all tablenames in the database 'test'.
You can write a script to loop over the result,
and create the statements to create the triggers for every name returned

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #11 on: February 08, 2010, 12:44:56 PM »
a solution in PHP might be better, because this is PHPfreaks...

Errr... what? Actually KISS rule is to be obeyed no matter how this site is called.

I can't see how dynamically creating dozens of triggers is better than querying one table...
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Log Maintenance on MySQL affected tables in a log table
« Reply #12 on: February 08, 2010, 12:47:27 PM »
true, but it also depends on what you want to do with this table..

and i did not say that i was in KISS-mode.... ::)

Offline Mchl

  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,582
  • Gender: Male
  • That's Largo in my avatar, not me.
    • View Profile
    • FlingBits
NetBeans fanatic | ExtJS masochist | C++ denier
PHP4 & MySQL4 are no longer supported.
PHPFreaks Tutorials | PHP Debugging: A Beginner's guide | PHP Security Tutorial || How To Ask Questions The Smart Way
Flingbits tutorials | Class Autoloading