从头开始创建sqlite时出现无法创建django_migrations表(数据库已锁定)错误

xqk2d5yq  于 2023-01-31  发布在  SQLite
关注(0)|答案(2)|浏览(250)

我有一个在Django开发服务器上运行的项目,所以我把它上传到GitHub,然后下载到我的预生产环境,并尝试用Apache2部署。但是我一直收到数据库锁定错误。不需要任何当前数据,我想我只需要创建一个新的SQLite数据库。
在删除了数据库文件和所有的迁移文件之后,我想我可以重新开始了。但是当我运行manage.py migrate时,我得到了下面的错误。再次说数据库被锁定了。

Unable to create the django_migrations table (database is locked)

我的网站settings.py

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

完整错误消息:

Operations to perform:
  Apply all migrations: account, admin, auth, contenttypes, people, sessions, sites, socialaccount
Running migrations:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
sqlite3.OperationalError: database is locked

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/recorder.py", line 55, in ensure_schema
    editor.create_model(self.Migration)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/schema.py", line 28, in __exit__
    super().__exit__(exc_type, exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 92, in __exit__
    self.atomic.__exit__(exc_type, exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py", line 212, in __exit__
    connection.commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 261, in commit
    self._commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.OperationalError: database is locked

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 91, in migrate
    self.recorder.ensure_schema()
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/recorder.py", line 57, in ensure_schema
    raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (database is locked)
    • 问题**:如何锁定数据库,更重要的是如何创建新的数据库?

谢谢

inb24sb2

inb24sb21#

原因:

“数据库已锁定”错误¶
SQLite是一个轻量级数据库,因此不能支持高级别的并发。OperationalError:数据库已锁定错误表示您的应用程序遇到的并发数超过了sqlite在默认配置下可以处理的并发数。此错误表示一个线程或进程对数据库连接具有排他锁,而另一个线程在等待释放锁时超时。
Python的SQLite Package 器有一个默认的超时值,它决定了在第二个线程超时并引发OperationalError之前,允许它在锁上等待多长时间:数据库被锁定错误。
如果您遇到此错误,可以通过以下方法解决:
切换到另一个数据库后端。在某个点SQLite变得太“轻”的现实世界的应用程序,这些并发错误表明你已经达到了这一点。
重写代码以减少并发并确保数据库事务是短期的。
通过设置timeout数据库选项增加默认超时值:

'OPTIONS': {
        # ...
        'timeout': 20,
        # ...
    }

这只会让SQLite在抛出“数据库被锁定”错误之前等待更长的时间;它对解决这些问题没有任何帮助。

解决方法:

1.删除db1.sqlite3文件。同时关闭SQLite浏览器(如果为db1.sqlite3打开)。然后再次尝试migrate
1.迁移到其他数据库,因为SQLite3不适合生产环境。将来需要您这样做,所以现在就试试吧。(首选:Mysql)连接应用程序与mysql的链接

wyyhbhjk

wyyhbhjk2#

如果使用SMB/CIFS,通过向cifs smb mount添加nobrl选项解决了问题。

相关问题