解决django.db.utils.OperationalError: attempt to write a readonly database错误

打开浏览器访问learning_log登录页面,出现如下错误:

执行如下命令:

root@baoguo learning_log# ll db.sqlite3

-rwxrwxrwx. 1 zhang zhang 147456 Oct 2 16:11 db.sqlite3

root@baoguo learning_log# ll -d .

drwxrwxrwx. 12 zhang zhang 4096 Oct 2 16:11 .

显示文件和目录权限为777,任何用户有写入的权限,问题不出在这儿。

cat /var/log/httpd/error_log

Thu Oct 02 14:31:43.373497 2025 wsgi:error pid 7466:tid 7626 client 192.168.1.44:55482 django.db.utils.OperationalError: attempt to write a readonly database, referer: http://192.168.1.101/accounts/register/

apache日志文件显示写只读数据库被拒绝

root@baoguo accounts# ausearch -m AVC -ts recent

<no matches>

搜索审计信息显示找不到selinux访问拒绝记录

root@baoguo learning_log# man semodule

EXAMPLE

Turn on all AVC Messages for which SELinux currently is "dontaudit"ing.

$ semodule -DB
root@baoguo learning_log# semodule -DB
该步是关键:-DB选项打开AVC审计信息开关

root@baoguo learning_log# ausearch -m AVC -ts recent

type=AVC msg=audit(1759392419.488:4923): avc: denied { setattr } for pid=8104 comm="rpm" name="rpmdb.sqlite-shm" dev="dm-0" ino=67240075 scontext=system_u:system_r:setroubleshootd_t:s0 tcontext=system_u:object_r:rpm_var_lib_t:s0 tclass=file permissive=0

有AVC记录了

root@baoguo learning_log# ausearch -m AVC -ts recent | audit2why

type=AVC msg=audit(1759392468.280:4934): avc: denied { net_admin } for pid=7462 comm="httpd" capability=12 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:system_r:httpd_t:s0 tclass=capability permissive=0

Was caused by:

Missing type enforcement (TE) allow rule.

You can use audit2allow to generate a loadable module to allow this access.

从AVC记录分析拒绝原因和解决方法

root@baoguo learning_log# grep httpd /var/log/audit/audit.log | audit2allow -M net_admin

******************** IMPORTANT ***********************

To make this policy package active, execute:

semodule -i net_admin.pp

生成允许访问策略

root@baoguo learning_log# semodule -i net_admin.pp

安装策略模块

root@baoguo learning_log# cat net_admin.te

module net_admin 1.0;

require {

type user_home_t;

type httpd_t;

class capability net_admin;

class dir write;

}

#============= httpd_t ==============

allow httpd_t self:capability net_admin;

allow httpd_t user_home_t:dir write;

显示.te(type enforement)文件定义的具体规则

解决该问题的关键步骤是执行semodule -DB命令以打开AVC审计开关

我的操作系统平台是centos stream 9

相关推荐
LRL_12 小时前
7x24小时不停机:基于 Apache SeaTunnel 实现 Oracle to Oracle 实时 CDC 同步全实战
数据库·oracle·apache
G.E.M.小白13 小时前
【SQLite3 数据库】从安装、SQL 操作到 C/C++ API 实战
数据库·oracle·sqlite3
大模型丫丫1 天前
工业级 RAG 为什么需要 Elasticsearch?
elasticsearch·django·jenkins
Mr数据杨1 天前
【Codex】接入音频转录服务实现课堂语音结构化处理
django·音视频·codex·项目开发
数智启示录1 天前
Apache Kafka Consumer 扩到 40 个仍不提速:Partition 上限锁死有效并行度 【Kafka合集】
数据库·经验分享·分布式·缓存·面试·kafka·apache
数智启示录1 天前
Apache Kafka 幂等 Producer 的边界:Exactly-Once 到了 MySQL 为什么失效 【Kafka合集】
数据库·经验分享·分布式·mysql·面试·kafka·apache
数智启示录2 天前
Apache Kafka 不只是消息队列:日志与 Offset 分离如何让事件可重放 【Kafka合集】
经验分享·分布式·面试·kafka·apache
数智启示录2 天前
Apache Kafka 同 Key 仍会乱序:从分区 Offset 到业务可见的四道边界 【Kafka 合集】
大数据·数据库·经验分享·分布式·面试·kafka·apache
吴声子夜歌2 天前
ApacheCommons——commons-lang3(Java 基础语言增强)(二)
java·开发语言·apache
2601_967019512 天前
企业网站SSL证书选型实战,少走弯路
apache