Audit Trail in Mysql

This script is based in the code published in the bolg A little noise http://thenoyes.com/littlenoise/?p=43, it adds some functionality:

  1. Adds triggers for insert and delete writing all the values inserted / deleted to the auditlog table
  2. It takes the primary key from information schema instead of grabbing it as  a parameter
  3. Stores the primary key as a varchar with the description (pkdesk saves the fields, and pkvalue the value of the primary key)
  4. Added the mysql username
  5. The tablename works with a like clause so if you send ‘%’ as table name you will get the script for all the tables in the current database.

Considerations:

  • If the script file already exists you will get an error
  • Tables that doesn’t have a primary key won’t script out.
  • If you change something in the script, don’t forget to comment out the table drop/creation part so you wont loose your previous data.

Hope this come handy for everyone, it took me some time to write it down, so I would like to share it for everyone.

Finally, the script: Download

Esta entrada fue publicada en Mysql. Guarda el enlace permanente.

19 respuestas a Audit Trail in Mysql

  1. Scott dice:

    “The table engine should be InnoDB, because is the only one that accept triggers”

    Since when?

  2. Panxpress dice:

    Can you please give an example as to how to use this. Somehow not able to use it.
    Thanks.

    • Javier Leis dice:

      Hi, just run the script, then call the stored procuedure
      addLogTrigger with two parameters:
      1st the table you wan to add the audit trail to (‘%’ for all tables)
      2nd the script file name (Null if you want the result on the screen)

      This will output another script that you have to run to get the audit trail working. Take into account that if you change a table structure you will need to run the stored procedure again for that specific table.

      Hope it helps…

      Javier

      • Panxpress dice:

        Thanks. I was able to get this working. One thing though. When I ran the procedure, it gave a warning.

        +——-+——+————————–+
        | Level | Code | Message |
        +——-+——+————————–+
        | Note | 1051 | Unknown table ‘temTable’ |
        +——-+——+————————–+

        After I ran the generated trigger, it did ran the 13 queries but gave warning for 3 of them. Not sure if it should be of any concern. I could not get the warning messages.

        This is a very useful script. Thanks for sharing this.

      • Nodin dice:

        That’s way more cvleer than I was expecting. Thanks!

    • Bobby dice:

      e8cXag IJWTS wow! Why can’t I think of thngis like that?

    • Kailee dice:

      Way to go on this essay, hpeeld a ton.

  3. David dice:

    Thanks alot pal.

    Youve saved may day :)

  4. Essence dice:

    What an awesome way to explain this?now I know eevrthying!

  5. javier dice:

    useful job for bringing something new to the internet!

  6. profiled dice:

    ome genuinely nice stuff on this website , I enjoy it.

  7. niceprof dice:

    I stumbled on your website by accident while searching Yahoo and I’m glad I did. Your post is a bit different and I enjoyed reading it. I will in turn make a comment about it on my blog and point my visitors your way. Thanks.

  8. profiled dice:

    This is very helpful, thanks for revealing. I will be confident others will view items this similar style

  9. Thankyou for sharing Audit Trail in Mysql | JEL Soluciones Informáticas with us keep update bro love your article about Audit Trail in Mysql | JEL Soluciones Informáticas .

  10. vasilich dice:

    Thanks for the interesting information! I would really like to read more of this! Keep up the good writing and I will come back soon! Thanks

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

*


- 7 = two

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>