大网数据广告活动
服务器运维问答

SQLSTATE[HY000]: General error: 8 attempt to write a readonly database

· 52CE 我爱测

(maybe this question is suited for SO, but I don't think it's linked to my code). I'm using SQLite3 as a database engine for my Web app. I freshly deployed it on the production server, and it's not working as expected. I'm using …

问题原文

原帖链接 · LeRouteur · CC BY-SA 4.0

(maybe this question is suited for SO, but I don't think it's linked to my code).

I'm using SQLite3 as a database engine for my Web app.

I freshly deployed it on the production server, and it's not working as expected.

I'm using PDO (a PHP DB interface) to access this DB, but whenever I try to write something in it, I get the error:

SQLSTATE[HY000]: General error: 8 attempt to write a readonly database

The production server is a Centos 8 running Httpd (so Apache). I've set the rights to 777 for the whole folder (this is just a test, don't blame me), but still getting the error.

I've read many things about this error, but none of them worked for me.

Funny thing, when running:

php -r 'var_dump(
  $db=new PDO("sqlite:/var/www/myProjetFolder/db/myProjetDb.sqlite"),
  $q=$db->query("SELECT * FROM sqlite_master"),
  $q->fetchAll()); '

At the root of the Web server, I get the content of my database, so I guess the rights are good.

As a precision, my project is stored in /var/www/myProjectFolder/.

I did not create any vHost for the moment, I simply edited httpd.conf and changed the DocumentRoot.

Why is this happening?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

By default SELinux doesn't allow the web server to write any files (uploads, your SQLite database, etc). You need to tell it which files and directories should be writable by the web server.

To make a single file writable:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/myProjetFolder/db/myProjetDb.sqlite"

To make a whole directory writable:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/uploads(/.*)?"

Then reset the file contexts on the affected file or directory.

restorecon -rv /var/www/uploads
restorecon -v /var/www/myProjetFolder/db/myProjetDb.sqlite

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。