全局禁用所有 MySQL 写入 [英] Globally disable all MySQL writes
问题描述
我使用 PHP 的 PDO 和 MySQL 来创建 Web 应用程序.我希望将 Web 应用程序置于演示"模式.这意味着所有写入(更新、插入、删除)都被禁用,并且适用的继续执行而不会出现任何错误.为了论证,如果 $_SESSION['demo_mode']=1,则应用程序处于演示模式.如果没有在每个 write 语句周围放置条件,如何做到这一点.
I am using PHP's PDO with MySQL to create a web application. I wish to put the web application in "demo" mode. What this means is all writes (updates, inserts, deletes) are disabled, and the applicable continues with out any errors. For arguments sake, the application is in demo mode if $_SESSION['demo_mode']=1. Without putting conditionals around each write statement, how can this be done.
$dbh->exec("INSERT INTO t1(c1, c2) VALUES ('v1', 'v2')");
$dbh->lastInsertId();
$q=$dbh->query("SELECT c1 FROM t2 WHERE c2=123");
$v=$q->fetchColumn();
推荐答案
您可以修改应用程序连接的用户的权限,使其仅获得 SELECT
和类似的权限.
You can modify the rights of the user the application connects as, so that it only gets SELECT
and similar rights.
这是个人权利的综合列表可以授予用户.
示例(当然要根据您的需要进行调整):
Example (to be adapted to your need, of course):
REVOKE ALL PRIVILEGES ON yourdatabase.* FROM 'application_user'@'host';
GRANT SELECT ON yourdatabase.* TO 'application_user'@'host';
显然,当您的应用程序尝试写入任何内容时,这会产生错误.如果您打算将其用于演示,您的应用应该准备好失败;)
Obviously, this will produce errors when your application tries to write anything. Your app should be prepared to fail graciously if you are planning to use this for a demo ;)
这篇关于全局禁用所有 MySQL 写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!