有没有办法知道MySQL表的数据是否被修改过?
You can query the INFORMATION_SCHEMA database, table TABLES
and there is a column "UPDATE_TIME". ( SHOW TABLES
is somewhat equivalent).
The other way is to give the PHP access to db files (if you can locate them) and check system date of file modification. EDIT read permission is enough for this operation.
In chat with @Voitcus, we figured out, that the information_schema
approach isn't available for InnoDb
tables. The column UPDATE_TIME
will being NULL
for such tables.
That's why we worked out a solution for InnoDB
tables that does not require read access to the db file on hard disk (what I think would be a security issue and no admin should allow this, also the db server could be on a different host)
Solution:
As you said, that you cannot use trigger functions for this, you'll have to fix this on application level, in PHP. Update : Note that with current versions of MySQL it wouldn't be stable to use triggers anyway as they won't get called on cascading foreign key actions.
First create a table, let's say stats
:
CREATE TABLE `yourdb`.`stats` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`table_name` VARCHAR( 255 ) NOT NULL ,
`last_update` DATETIME NOT NULL
) ENGINE = InnoDB;
Insert a row for every table in your application.
In PHP modify existing queries so that they use transactions and update the stats table. This is possible using mysqli
or PDO
for example. I'm using PDO
here, we deleting a row for example.
try {
$pdo = new PDO($host, $user, $pass, $db, array((
// this option is nice when working with transactions
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (PDOException $e) {
die('cannot connect to mysql');
}
try {
// start transaction
$pdo->beginTransaction();
// delete a row
$pdo->query('DELETE FROM `table1` WHERE `id` = 1');
// update stats
$pdo->query('UPDATE `stats` SET `last_update` = NOW() WHERE `table_name` = `table1`');
// commit both queries at once
$pdo->commit();
} catch (Exception $e) {
// rollback both queries if one of them failed
$pdo->rollback();
}
Now you can use the following query to obtain the last update time:
SELECT `last_updated` FROM `stats` WHERE `table_name` = 'table1'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.