简体   繁体   English

PHP SQLite PRAGMA journal_mode = wal和只读用户

[英]PHP SQLite PRAGMA journal_mode = wal and readonly users

I read that WAL mode allows a user users to read while during a write session. 我读到WAL模式允许用户在写会话期间进行阅读。
Therefore I should be able to have 3 sessions reading data and one writing data at the same time, This sounds like a good thing for my use, so I figured I should define the session type so the system knows if this session is a reader or a writer using the connection flag. 因此,我应该能够有3个会话同时读取数据和一个写入数据,这听起来对我来说是一件好事,所以我认为我应该定义会话类型,以便系统知道此会话是读取器还是读取器使用连接标志的作家。
http://php.net/manual/en/sqlite3.open.php#refsect1-sqlite3.open-parameters http://php.net/manual/zh/sqlite3.open.php#refsect1-sqlite3.open-parameters
It states here that if the session is not closed cleanly that the -shm and -wal files are not removed 它在此处指出,如果未完全关闭会话,则不会删除-shm-wal文件
https://www.sqlite.org/tempfiles.html#write_ahead_log_wal_files https://www.sqlite.org/tempfiles.html#write_ahead_log_wal_files
After a read session the temporary files are not removed, thus meaning the session was not cleanly closed despite calling the close function and it returning true , so why are the files not being deleted when using the SQLITE3_OPEN_READONLY flag? 在读取会话之后,不会删除临时文件,这意味着尽管调用了close函数仍未完全关闭该会话,但该会话返回true ,那么为什么在使用SQLITE3_OPEN_READONLY标志时未删除这些文件? Should I even use a flag at all? 我是否应该甚至使用标志?

You've conflated SQLite's concept of 'readers' and 'writers' with the ability of the PHP SQLite3 driver to open a file as read-only. 您已将SQLite的“读取器”和“写入器”概念与PHP SQLite3驱动程序以只读方式打开文件的能力混为一谈。

Using the SQLITE3_OPEN_READONLY flag will cause PHP to block all write operations with a warning: 使用SQLITE3_OPEN_READONLY标志将使PHP发出警告,阻止所有写操作:

Warning: SQLite3::exec(): attempt to write a readonly database in ... 警告:SQLite3 :: exec():尝试在...中写入只读数据库

This can be useful if you want to ensure no writes can happen from your application (perhaps as a security measure). 如果您想确保应用程序不会发生写操作(这可能是一种安全措施),则这很有用。 But it's not related to SQLite's readers and writers. 但这与SQLite的读写器无关。

As pointed out by CL all processes accessing the WAL mode database need write access. 正如CL指出的,所有访问WAL模式数据库的进程都需要写访问权限。 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. 同样,如果有多个进程要访问WAL模式数据库,则所有进程都应在用户或组ID下运行,以使他们能够对数据库文件,WAL文件,共享内存-shm文件以及包含目录进行写访问。

And mentioned as a disadvantage at the top of that page: 并在页面顶部提到了一个缺点:

It is not possible to open read-only WAL databases. 无法打开只读WAL数据库。 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. 如果与该数据库关联的“ -shm” wal-index共享内存文件存在,则打开过程必须具有写特权,否则,如果“ -shm”文件不存在,则对包含数据库文件的目录具有写访问权限。

SQLite itself decides if a process is a reader or writer when it receives a command to execute. 当SQLite接收到要执行的命令时,它自己决定一个进程是读取器还是写入器。 For example, if a single process executes a SELECT , then a INSERT , then a SELECT again it changes from reader to writer to reader. 例如,如果一个进程执行SELECT ,然后INSERT ,然后SELECT它再次从读者变成作家的读者。 SQLite will handle locking (potentially blocking other processes) automatically. SQLite将自动处理锁定(可能阻止其他进程)。 (Note that this is a simplified explanation that can be quite different with different modes of database operation.) (请注意,这是一个简化的说明,对于不同的数据库操作模式,可能会大不相同。)

For your purposes you shouldn't use the SQLITE3_OPEN_READONLY flag. 为了您的目的,您不应使用SQLITE3_OPEN_READONLY标志。

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

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