I have several mysql databases and tables that need to be “listened to”. I need to know what data changes and send the changes to remote servers that have local mirrors of the database.
How can I mirror changes in the mysql databases? I was thinking of setting up mysql triggers that write all changes to another table. This table has the database name, table name, and all of the columns. I’d then write custom code to transfer the changes and install them periodically on the remote mirrors. Will this accomplish my need?
Your plan is 100% correct.
That extra table is called an “audit” or “history” table (there are subtle distinctions but you shouldn’t much care – but you now have the “official” terms which you can use to do further research).
If the main table has columns A, B, C, then the audit would have 3 more: A, B, C, Operation, Changed_By, Change_DateTime (names are subject to your tastes and coding standards).
“Operation” column stores whether the change was an insert, delete, old value of update or new value of update (frequently it’s 3 characters wide and the operations as “INS”/”DEL”/”U_D” and “U_I”, but there are other approaches).
The data in the audit table is populated via a trigger on the main table.
Then make sure there’s an index on Change_DateTime column.
And to find a list of changes, you keep track of when you last polled, and then simply do