简体   繁体   English

在 Centos7 上使用 SQLite3 与 Django 2.2 和 Python 3.6.7

[英]Using SQLite3 with Django 2.2 and Python 3.6.7 on Centos7

I am moving my Django code from 2.1.7 directly to the new Django 2.2.我将我的 Django 代码从 2.1.7 直接移动到新的 Django 2.2。 The only problem I encountered in my Centos7 development environment was that my local development database (sqlite3) version was incompatible using my Python 3.6.7.我在 Centos7 开发环境中遇到的唯一问题是我的本地开发数据库(sqlite3)版本与我的 Python 3.6.7 不兼容。

The error I was getting from "manage.py runserver" was:我从“manage.py runserver”得到的错误是:

django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later

I am unable to use another version of Python because this is the maximum supported by AWS elasticbeanstalk.我无法使用其他版本的 Python,因为这是 AWS elasticbeanstalk 支持的最大值。 The Python 3.6.7 seems to come with sqlite module of version: Python 3.6.7 似乎带有版本的 sqlite 模块:

>>> import sqlite3
>>> sqlite3.version
'2.6.0'
>>> sqlite3.sqlite_version
'3.7.17'
>>> 

I use a seperate development account on my local Centos7 workstation and issue pipenv shell to begin my code development and IDE.我在本地 Centos7 工作站上使用单独的开发帐户并发出pipenv shell开始我的代码开发和 IDE。

The only workaround I've found is to manually download SQLite3 autoconf version 3.27.2 and manually compile into that development account home folder using the following commands:我发现的唯一解决方法是手动下载 SQLite3 autoconf 版本 3.27.2 并使用以下命令手动编译到该开发帐户主文件夹中:

wget https://www.sqlite.org/2019/sqlite-autoconf-3270200.tar.gz
gzip -d sqlite-autoconf-3270200.tar.gz
tar -xvf sqlite-autoconf-3270200.tar
cd sqlite-autoconf-3270200/
./configure --prefix=/home/devuser/opt/
make
make install

Following that, I have modified my .bashrc to reflect the following:之后,我修改了我的 .bashrc 以反映以下内容:

export LD_LIBRARY_PATH="${HOME}/opt/lib"

This seems to do the trick when I log back into my devuser account.当我重新登录我的 devuser 帐户时,这似乎可以解决问题。 My app seems to run correctly using my local development database.我的应用程序似乎使用我的本地开发数据库正确运行。

Python 3.6.7 (default, Dec  5 2018, 15:02:05)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
>>>import sqlite3
>>> sqlite3.version
'2.6.0'
>>> sqlite3.sqlite_version
'3.27.2'

My local development database is SQLite, but my settings.py does not load any SQLite3 database backend when it senses it's in production on AWS (uses Mysql production database as backend when environment variable flag PRODUCTION is checked).我的本地开发数据库是 SQLite,但是当我的 settings.py 在 AWS 上检测到它在生产中时,它不会加载任何 SQLite3 数据库后端(当环境变量标志 PRODUCTION 被选中时,使用 Mysql 生产数据库作为后端)。

Is my understanding of the problem correct and is my approach and implementation acceptable?我对问题的理解是否正确,我的方法和实施是否可以接受?

I felt that recompiling python was a huge waste of time, and to be honest it may have been faster to stand up a local mysql version and stop wasting time with sqlite... but it's so nice to just copy or dump a file, migrate, and loaddata for a fresh start.我觉得重新编译 python 是一个巨大的浪费时间,老实说,它可能会更快地站起来一个本地 mysql 版本并停止在 sqlite 上浪费时间......但只是复制或转储文件,迁移真是太好了,并加载数据以重新开始。

I am using Centos7 with python36.我正在使用 Centos7 和 python36。

[shmakovpn@localhost ~]$ ldd /usr/lib64/python3.6/lib-dynload/_sqlite3.cpython-36m-x86_64-linux-gnu.so
linux-vdso.so.1 =>  (0x00007ffcafdf6000)
libsqlite3.so.0 => /lib64/libsqlite3.so.0 (0x00007f0adf439000)
libpython3.6m.so.1.0 => /lib64/libpython3.6m.so.1.0 (0x00007f0adef10000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0adecf4000)
libc.so.6 => /lib64/libc.so.6 (0x00007f0ade927000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f0ade723000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f0ade520000)
libm.so.6 => /lib64/libm.so.6 (0x00007f0ade21e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0adf903000)

This means that python36 uses /lib64/libsqlite3.so.0 In python console it looks like this这意味着 python36 使用 /lib64/libsqlite3.so.0 在 python 控制台中它看起来像这样

>>> import sqlite3
>>> sqlite3.version
'2.6.0'
>>> sqlite3.sqlite_version
'3.7.17'

Needs to replace the library, so we must have a new version of it.需要更换库,所以我们必须有它的新版本。 There are one of way how to do this.有一种方法可以做到这一点。

wget http://www6.atomicorp.com/channels/atomic/centos/7/x86_64/RPMS/atomic-sqlite-sqlite-3.8.5-3.el7.art.x86_64.rpm
sudo yum localinstall atomic-sqlite-sqlite-3.8.5-3.el7.art.x86_64.rpm
sudo mv /lib64/libsqlite3.so.0.8.6{,-3.17}
sudo cp /opt/atomic/atomic-sqlite/root/usr/lib64/libsqlite3.so.0.8.6 /lib64

Go to python console once again再次进入 python 控制台

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.8.5'

Now it looks much better.现在看起来好多了。 Let's try to create Django project and apply migrations让我们尝试创建 Django 项目并应用迁移

django-admin startproject sqlite3test
cd sqlite3test/
python manage.py migrate
    Operations to perform:
        Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
        Applying contenttypes.0001_initial... OK
        ... and so on
ls -al | grep db
    -rw-r--r--.  1 shmakovpn shmakovpn 40960 апр 19 01:02 db.sqlite3

Sqlite3 database successfully created! Sqlite3 数据库创建成功!

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

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