How to monitor data changes in the database?
Needs to implement real-time push messages. The front-end uses websocket, and the back-end uses php and node.js. (Recommended learning: PHP video tutorial)
The current problem is that the front-end monitors the data changes in a certain data table on the server side. Both php socket and nodejs socket.io only listen to events on the socket port, but how to obtain changes in the database?
This data table may be updated by the user on the socket side, or may not be on the socket side. Sometimes, the data may be changed by the administrator. So, how does the socket on the server side listen for data change events and then push it back to the front end? !
The database may be mysql or mongo.
Solution:
At the application level, generally speaking, interceptors and other methods are used to implement change monitoring, but this method cannot solve the problem of direct modification on the database side. .
There are generally three methods at the database level:
1. Similar to commercial databases such as Oracle, it provides an event monitoring mechanism that can proactively notify the connecting party of data changes;
2. Use triggers, set triggers on tables that need to be monitored, and then call php functions to trigger external mechanisms;
3. Use triggers, set triggers on tables that need to be monitored, but only Write to the log table, the external still needs to poll the log table;
4. Monitor or scan the database log to discover every change in the database.
The above is the detailed content of Can php monitor database changes?. For more information, please follow other related articles on the PHP Chinese website!