简体   繁体   English

只读数据库上的PDO :: exec()不返回

[英]PDO::exec() on read-only database doesn't return

I'm investigating the effect of running MySQL in read-only mode on my code.I'm running MySQL 5.5.49.0 combined with PHP 5.5.9.1 (on Ubuntu 14.04). 我正在研究在我的代码上以只读模式运行MySQL的效果。我正在运行MySQL 5.5.49.0和PHP 5.5.9.1(在Ubuntu 14.04上)。 I set the database to read-only using the following commands: 我使用以下命令将数据库设置为只读:

flush tables with read lock;
set global read_only = 1;

If I connect via CLI and attempt an insert I get: 如果我通过CLI连接并尝试插入,我得到:

ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock

If I connect with the same user via PDO: 如果我通过PDO与同一用户连接:

$pdo = new PDO('mysql:dbname=database;host=host', 'username', 'password', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
try {
    $result = $pdo->exec('insert ***');
    die(var_dump(__LINE__, $result));
} catch (Exception $e) {
    die(var_dump(__LINE__, $e->getMessage()));
}

The browser keeps loading infinitly. 浏览器无限加载。 I have tried a simular construction with good old mysql_connect and mysql_query and in that case the read-only situation is ignored and 0 is returned. 我尝试了一个具有良好旧的mysql_connect和mysql_query的模拟结构,在这种情况下,只读情境被忽略,返回0。 Also not a workable solution. 也不是一个可行的解决方案。

I would rather not check pre-emptive if the database is running in read-only mode, because this is usually not the case and thus I can save an extra query every request. 如果数据库以只读模式运行,我宁愿不检查先发制人,因为通常情况并非如此,因此我可以在每次请求时保存额外的查询。

So my question: how do I get an Exception or warning in any other form from Mysql/PDO? 所以我的问题是:如何从Mysql / PDO获得任何其他形式的异常或警告?

It turns out I was connecting to my local development database (with my Vagrant instance) using a user with SUPER privileges. 事实证明我使用具有SUPER权限的用户连接到我的本地开发数据库(使用我的Vagrant实例)。 Users with SUPER privileges are not affected by the read_only option. 具有SUPER权限的用户不受read_only选项的影响。

When I switched to a normal user I did get an Exception. 当我切换到普通用户时,我确实得到了一个例外。

Another option you can consider is to use super_read_only instead of normal read_only. 您可以考虑的另一个选项是使用super_read_only而不是普通的read_only。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM