PHP SQLite PRAGMA journal_mode = wal 和只读用户

PHP SQLite PRAGMA journal_mode = wal and readonly users

我读到 WAL 模式允许用户用户在写入会话期间读取。
因此我应该能够让 3 个会话同时读取数据和一个写入数据,这听起来像对我的使用来说是一件好事,所以我想我应该定义会话类型,以便系统知道此会话是 reader 还是使用连接标志的编写器。
http://php.net/manual/en/sqlite3.open.php#refsect1-sqlite3.open-parameters
它在这里声明,如果会话没有完全关闭,则 -shm-wal 文件不会被删除
https://www.sqlite.org/tempfiles.html#write_ahead_log_wal_files
在读取会话后,临时文件不会被删除,这意味着尽管调用了关闭函数并返回 true,会话并没有完全关闭,那么为什么在使用 SQLITE3_OPEN_READONLY 标志时文件没有被删除?我什至应该使用标志吗?

您将 SQLite 的 'readers' 和 'writers' 概念与 PHP SQLite3 驱动程序以只读方式打开文件的能力混为一谈。

使用 SQLITE3_OPEN_READONLY 标志将导致 PHP 阻止所有写入操作并发出警告:

Warning: SQLite3::exec(): attempt to write a readonly database in ...

如果您想确保您的应用程序不会发生写入(可能作为一种安全措施),这将很有用。但它与 SQLite 的 readers 和编写器无关。

作为 all processes accessing the WAL mode database need write access. This is explicitly documented:

Also, if multiple processes are to access a WAL mode database, then all processes should run under user or group IDs that give them write access to the database files, the WAL file, the shared memory -shm file, and the containing directory.

并在该页顶部提到了缺点:

It is not possible to open read-only WAL databases. The opening process must have write privileges for "-shm" wal-index shared memory file associated with the database, if that file exists, or else write access on the directory containing the database file if the "-shm" file does not exist.

SQLite 本身在接收到要执行的命令时决定进程是 reader 还是 writer。例如,如果单个进程执行 SELECT,然后执行 INSERT,然后再次执行 SELECT,它会从 reader 变为 writer 到 reader。 SQLite 将自动处理锁定(可能阻塞其他进程)。 (请注意,这是一个简化的解释,不同的数据库操作模式可能会有很大不同。)

出于您的目的,您不应使用 SQLITE3_OPEN_READONLY 标志。