centos Django登录错误:“试图写入只读数据库”

xxls0lw8  于 2023-05-17  发布在  Go
关注(0)|答案(4)|浏览(261)

我试图在CentOS 6上设置Apache(2.4,从源代码构建)+Django(1.8.12),但当我尝试登录到我的网站或创建用户或其他写入数据库的东西时,我得到一个错误:
尝试写入只读数据库
我已经试过了:

  • 将db文件的所有者更改为apache用户(不起作用)
  • 更改权限:775、774、664给予与上述相同的错误; 666,776给予:

无法打开数据库文件

  • 与django项目的整个文件夹相同的操作

this教程
我的httpd.conf(我添加的部分):

LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /var/www/project/project/wsgi.py
WSGIPythonPath /var/www/project:/var/www/env/lib/python3.5/site-packages

<Directory /var/www/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

./manage createsuperuser工作正常,用户已添加到数据库。
对于服务媒体我使用Nginx(所有媒体和静态文件检索正确)。

UPD

完整追溯:

Environment:

Request Method: POST
Request URL: http://localhost:8080/accounts/login/

Django Version: 1.8.12
Python Version: 3.5.1
Installed Applications:
('bootstrap3',
 'django_admin_bootstrapped',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'macros',
 'django_ajax',
 'ckeditor',
 'accounts',
 'main',
 'tutor',
 'public_testing',
 'control_testing',
 'debug_toolbar',
 'django_extensions',
 'mmc')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')

Traceback:
File "/var/www/env/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/contrib/auth/views.py" in login
  51.             auth_login(request, form.get_user())
File "/var/www/env/lib/python3.5/site-packages/django/contrib/auth/__init__.py" in login
  110.         request.session.cycle_key()
File "/var/www/env/lib/python3.5/site-packages/django/contrib/sessions/backends/base.py" in cycle_key
  285.         self.create()
File "/var/www/env/lib/python3.5/site-packages/django/contrib/sessions/backends/db.py" in create
  41.                 self.save(must_create=True)
File "/var/www/env/lib/python3.5/site-packages/django/contrib/sessions/backends/db.py" in save
  65.                 obj.save(force_insert=must_create, using=using)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/base.py" in save
  734.                        force_update=force_update, update_fields=update_fields)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/base.py" in save_base
  762.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/base.py" in _save_table
  846.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/base.py" in _do_insert
  885.                                using=using, raw=raw)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/query.py" in _insert
  920.         return query.get_compiler(using=using).execute_sql(return_id)
File "/var/www/env/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  974.                 cursor.execute(sql, params)
File "/var/www/env/lib/python3.5/site-packages/debug_toolbar/panels/sql/tracking.py" in execute
  159.         return self._record(self.cursor.execute, sql, params)
File "/var/www/env/lib/python3.5/site-packages/debug_toolbar/panels/sql/tracking.py" in _record
  101.             return method(sql, params)
File "/var/www/env/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/var/www/env/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/var/www/env/lib/python3.5/site-packages/django/db/utils.py" in __exit__
  98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/var/www/env/lib/python3.5/site-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)
File "/var/www/env/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/var/www/env/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py" in execute
  318.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /accounts/login/
Exception Value: attempt to write a readonly database
  • UPD 2**

/tmp权限:

drwxrwxrwt   6 root root  4096 Apr 22 10:06 tmp

settings.py 数据库:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

错误页中的DATABASE部分:

DATABASES   {'default': {'ATOMIC_REQUESTS': False,
             'AUTOCOMMIT': True,
             'CONN_MAX_AGE': 0,
             'ENGINE': 'django.db.backends.sqlite3',
             'HOST': '',
             'NAME': '/var/www/project/db.sqlite3',
             'OPTIONS': {},
             'PASSWORD': '********************',
             'PORT': '',
             'TEST': {'CHARSET': None,
                      'COLLATION': None,
                      'MIRROR': None,
                      'NAME': None},
             'TIME_ZONE': 'UTC',
             'USER': ''}}
vwkv1x7d

vwkv1x7d1#

事实上,这实际上不是一个登录错误。当写入sqlite数据库的应用程序没有写权限时,就会发生这种情况。由于登录是第一次尝试写入数据库,因此您将其作为登录错误。
这可以通过三种方式来解决:
1.使用chown(例如:chown username db.sqlite3
1.以root用户身份运行web服务器(通常是gunicorn)(在运行gunicornapache或django runserver之前运行sudo -i命令)
1.通过运行chmod 777 db.sqlite3命令(危险选项)允许所有用户读写
永远不要选择第三个选项,除非你在本地机器上运行Web服务器,或者数据库中的数据对你来说根本不重要。
此外,如果您使用的是像mysql和Postgres这样的数据库,则不会发生此错误。对于高流量的Web服务器来说,Sqlite不是一个好的选择。

mwg9r5ms

mwg9r5ms2#

对于运行在Apache上的Django应用程序,您可以通过检查apache2/conf/httpd.conf文件来查找运行Apache的用户。
在Bitnami Django Stack的情况下,httpd.conf文件显示:

User daemon
Group daemon

这将显示哪个用户正在运行Apache。
现在,要解决“尝试写入只读数据库”的问题,只需运行以下命令:

sudo chown {apache_user}:{apache_user} /path/django_project
sudo chown {apache_user}:{apache_user} /path/django_project/db.sqlite3

chown命令将更改项目文件夹和数据库的所有权,允许服务器与之通信。

nuypyhwy

nuypyhwy3#

权限问题:将数据库和目录的所有者更改为apache解决了这个问题(centos 7)。

chown apache:apache /root/myproject/db.sqlite
chown apache:apache /root/myproject

请确认https://stackoverflow.com/a/45909954/1218179

fdx2calv

fdx2calv4#

看起来像权限问题。权限777对数据库文件应该工作-这样你给予所有用户的所有访问。但是...如果您希望编辑,则会创建额外的journal文件,因此用户需要编辑目录。Apache用户可以是apachenobody

相关问题