mysql custom query logging for inter-software data exchange Some 2-3 years I had written some triggers (one per table) in SQL server allowing me to log queries in our main software.
ALTER TRIGGER [dbo].[SUIVI_REQUETE_CAT_CATBRUT] ON [dbo].[CAT_CATBRUT] AFTER INSERT, DELETE, UPDATE

AS
DECLARE
  @date as nvarchar(10),
  @heure as nvarchar(15),
  @nomUtilisateur as nvarchar(50),
  @poste as nvarchar(50),
  @requete as nvarchar(MAX)
BEGIN
  set @requete = 'DBCC INPUTBUFFER(' + str(@@SPID) + ')'
  
  CREATE TABLE temporaire (EventType nvarchar(30), Parameters int, EventInfo nvarchar(MAX))
  insert into temporaire exec(@requete)
  
  set @requete = (SELECT EventInfo FROM temporaire)
  drop table temporaire
  
  set @poste = (SELECT HOST_NAME() AS HostName)
  set @nomUtilisateur = (SELECT SUSER_NAME() LoggedInUser)
  set @date =(SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 111) AS [YYYY/MM/DD])
  set @heure =(select CONVERT(VARCHAR,getdate(),108))
  
  INSERT INTO INF_LOG_REQUETE (REQUETE, POSTE, NOM_UTILISATEUR, [DATE], HEURE) VALUES (LEFT(@requete, 4000), @poste, @nomUtilisateur, @date, @heure)
End

I'm now at the head of a new web division and one of our goal is allow my web-software to share data with our main software. I would like to save some key rows in the tables (customer, vehicle, employee, calendar, etc.) we want to share between both softwares.
I have three questions regarding my problem:

I would like to log the query only for a specific database user (we do not want to get stuck in an infinite loop of sending back and forth the same modification). In SQL server I used that to detect the user:
set @nomUtilisateur = (SELECT SUSER_NAME() LoggedInUser)


Is it possible to detect the user that executed the query in MySQL?

In SQL server, I was able to use the same trigger on insert, update and delete actions.

Is it possible to do the same in MySQL or do I have to write three different trigger from every table?

If the same trigger can be used for multiple action (insert, update, delete), is it possible to detect which action (insert, update, delete) launched the trigger?

Additional note: I want to record only specific fields (containing the key data to share) in the table. I really want to write a trigger for every table so a PHP function can automatically parse the field with data and build an XML file. By example, I want a field in my log table containing:
{table}:[vehicle], {action}:[insert], {field}:[make], {value}:[acura], {field}:[model], {value}:[S7], ...

And produce XML like the following:
&lt;LIST&gt;
    &lt;QUERY&gt;
        &lt;TABLE&gt;vehicle&lt;/TABLE&gt;
        &lt;ACTION&gt;insert&lt;ACTION&gt;
        &lt;FIELD&gt;make&lt;/FIELD&gt;
        &lt;VALUE&gt;acura&lt;/VALUE&gt;
        &lt;FIELD&gt;model&lt;/FIELD&gt;
        &lt;VALUE&gt;S7&lt;/VALUE&gt;
        ...
    &lt;/QUERY&gt;
    &lt;QUERY&gt;
        ...
    &lt;/QUERY&gt;
    &lt;QUERY&gt;
        ...
    &lt;/QUERY&gt;
&lt;/LIST&gt;

Thank you
Ok, after some research, here's the answers I found.

Q1: Is it possible to detect the user that executed the query in MySQL?

It is possible to detect with which user by using: select user();

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_user

Beware: In triggers and events current_user() returns the user who defined the object (in SP and views unless if defined with the SQL SECURITY INVOKER). You really need to use the user() function to return the invoker in those contexts.

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_current-user

Q2: Is it possible to fire more than one action (INSERT, UPDATE, DELETE) on the same trigger?

Most unfortunately, at the current time (2016/02/01), it is not possible to fire two actions on the same time in MySQL. The best we can do is to regroup the common coding within a stored procedure.

The following procedure is one possible way to work around this problem. It expects in input:


callingTable: The name of the table for which we want to record the query (must be in the white list).
action: Either 'DELETE', 'INSERT' or 'UPDATE'.
fieldsValues: a string containing a valid pair of field/value in XML. By example: &lt;field&gt;ID&lt;/field&gt;&lt;value&gt;1616&lt;/value&gt;

CREATE PROCEDURE `dataLinkRecorder` (
IN callingTable VARCHAR (25),
IN action ENUM('DELETE', 'INSERT', 'UPDATE'),
IN fieldsValues TEXT
)
BEGIN
DECLARE callingUser VARCHAR(25) DEFAULT '';
DECLARE fieldCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE fieldCCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE valueCount SMALLINT UNSIGNED DEFAULT 0;
DECLARE valueCCount SMALLINT UNSIGNED DEFAULT 0;

IF (TRIM(fieldsValues) &lt;&gt; '') THEN

    /* the calling table must be one in the white list */
    SELECT CASE 
        WHEN UPPER(callingTable) IN (
            'CATEGORY',
            'CUSTOMER',
            'MAKE',
            'MODEL',
            'PRODUCT',
            'VEHICLE'
        )
        THEN TRIM(UPPER(callingTable))
        ELSE '' 
        END AS myTable
        INTO callingTable;

     SELECT TRIM(
     UPPER(
        IFNULL(USER(), 'CLIENT')
     )
     ) AS myUser
INTO callingUser;

/* do not record modifications from tier software */
IF (callingTable &lt;&gt; '' AND INSTR(callingUser, 'CLIENT') &lt; 1) THEN

    SELECT IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;field&gt;', ''))) / 7)
    , 0) AS fCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;/field&gt;', ''))) / 8)
    , 0) AS fCCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;value&gt;', ''))) / 7)
    , 0) AS vCount,
    IFNULL(
        ((CHAR_LENGTH(fieldsValues) - CHAR_LENGTH(REPLACE(fieldsValues, '&lt;/value&gt;', ''))) / 8)
    , 0) AS vCCount
    INTO fieldCount, fieldCCount, valueCount, valueCCount;

    /* validate the field/value XML pair */
    IF (fieldCount &gt; 0 AND
        valueCount &gt; 0 AND
        fieldCount = fieldCCount AND
        valueCount = valueCCount AND
        fieldCount = valueCCount
    ) THEN

        INSERT INTO `pc_datalink` 
            (`table_name`, `query_xml`) 
        VALUES(
            callingTable,
            CONCAT(
                '&lt;query&gt;',
                    '&lt;table&gt;', callingTable, '&lt;/table&gt;',
                    '&lt;action&gt;', action, '&lt;/action&gt;',
                    fieldsValues,
                '&lt;/query&gt;'
            )
        );
    END IF;
END IF;
END IF;
END //



Will insert values in the following table:

CREATE TABLE `pc_datalink` (
    `id` int(10) UNSIGNED NOT NULL,
    `table_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
    `query_xml` text COLLATE utf8_unicode_ci NOT NULL,
    `date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `AK_DATE_ADDED` (`date_added`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


By example, a very simple trigger like this one:

CREATE TRIGGER `dataLinkDeleteVehicle` AFTER DELETE ON `pc_vehicle`
FOR EACH ROW
BEGIN
IF (OLD.original_id &lt;&gt; '') THEN

    CALL dataLinkRecorder(
        'VEHICLE',
        'DELETE',
        CONCAT(
            '&lt;field&gt;ID&lt;/field&gt;&lt;value&gt;',
            OLD.original_id,
            '&lt;/value&gt;'
        )
    );
END IF; 
END //


Will generate a line in the table like this one:



Don't hesitate to ask for details if you still have questions,

Good success everyone