We are developing a new web application.
one of the most basic requirement is to audit all entities changes into a separate table.
We would like to use DB triggers for that purpose.
We use MySQL as our RDMBS.
The problem we now foresee is that whenever a trigger is pulled, and insert a new entry for the DB, it cant possibly know the (applicative) user that made the change. (all users have different ids, but spring uses a single user account for the db manipulations.)
Any ideas how to resolve this issue?
Best Answer
Unless you create a different database user for each application user -so it is available with the
user()
orcurrent_user()
functions:it is not possible to know this at MySQL level unless it is provided by the application.
You could use SQL comments on application code:
Which can later be processed using the slow log, if active, or some of the auditing plugins. Please note that the mysql command-line client application deletes comments before sending them to the server.
The only way you can log it using triggers with a single user that I can now think is setting a variable at the start of the connection: