简体   繁体   English

Django:尝试编写只读数据库

[英]Django: Attempt to write a read-only database

I have just created a Django project with 我刚刚创建了一个Django项目

python manage.py startapp smartrecruitment

I then ran a db sync 然后我运行了db sync

 python manage.py syncdb
 Operations to perform:
 Apply all migrations: admin, contenttypes, auth, sessions
 Running migrations:
   Applying contenttypes.0001_initial... OK
   Applying auth.0001_initial... OK
   Applying admin.0001_initial... OK
   Applying sessions.0001_initial... OK

And added my superuser, but I cannot access /admin in the browser. 并添加了我的超级用户,但我无法在浏览器中访问/ admin。 I have tried doing the following commands to give apache permissions, but have had no luck. 我尝试过以下命令来授予apache权限,但没有运气。

sudo chown apache <folder_containing_db.sqlite3>
sudo chown apache db.sqlite3

change owner of the project directory and database file www-data 更改项目目录和数据库文件www-data所有者

chown www-data:www-data /home/username/Django    
chown www-data:www-data /home/username/Django/db.sqlite  

You have indeed user/group permission problems : you need to run your webserver with a user who has read / write access to your db file (when using sqlite3) 您确实存在用户/组权限问题:您需要使用对您的db文件具有读/写访问权限的用户运行您的Web服务器(使用sqlite3时)

Changing the owner and the group of your entire project is a bad idea since it would allow your web server user to write on your codebase, which is never a good practice . 更改整个项目的所有者和组是一个坏主意,因为它允许您的Web服务器用户在您的代码库上编写,这绝不是一个好习惯

A better idea is to use a real database in production instead of sqlite to avoid this. 更好的想法是在生产中使用真实数据库而不是sqlite来避免这种情况。

https://docs.djangoproject.com/en/1.9/ref/settings/#databases https://docs.djangoproject.com/en/1.9/ref/settings/#databases

If you want to stick with sqlite : place your sqlite file outside of your project repository and give it and the containing directory correct read/write accesses (for instance only www-data can write, but then you need to run your django commands as www-data) 如果你想坚持使用sqlite:将sqlite文件放在项目存储库之外并给它和包含目录正确的读/写访问(例如只有www-data可以写,但是你需要运行你的django命令作为www -数据)

some_dir [your_user:your_group]
--- your_django_project [github_user:github_user]
--- another_dir [www-data:www-data]
    |--- db.sqlite3 [www-data:www-data]

And your webserver (apache / nginx ) runs as www-data 您的网络服务器(apache / nginx)作为www-data运行

I was looking for the solution, as I followed CentOS tutorial by this link. 我正在寻找解决方案,因为我通过此链接关注了CentOS教程。 https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7 https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-centos-7

Here is the permission for that tutorial, 这是该教程的权限,

sudo usermod -a -G root apache
chmod 710 /root/myproject
chown apache:apache /root/myproject
chown apache:apache /root/myproject/db.sqlite

In short, it happens when the application which writes to the sqlite database does not have write permission. 简而言之,当写入sqlite数据库的应用程序没有写入权限时,就会发生这种情况。

This can be solved in three ways: 这可以通过三种方式解决:

  1. Granting ownership of db.sqlite3 file and its parent directory (thereby write access also) to the user using chown (Eg: chown username db.sqlite3 ) 使用chown向用户授予db.sqlite3文件及其父目录(从而也可以写访问权限)的所有权(例如: chown username db.sqlite3
  2. Running the webserver (often gunicorn) as root user (run the command sudo -i before you run gunicorn or django runserver ) 以root用户身份运行webserver(通常是gunicorn)(在运行gunicorn或django runserver之前运行命令sudo -i
  3. Allowing read and write access to all users by running command chmod 777 db.sqlite3 (Dangerous option) 允许通过运行命令chmod 777 db.sqlite3 (危险选项)对所有用户进行读写访问

Note : Never go for the third option unless you are running the webserver in a local machine or the data in the database is not at all important for you. 注意 :除非您在本地计算机上运行Web服务器,否则永远不要使用第三个选项,否则数据库中的数据对您来说并不重要。

Second option is also dangerous as @mateuszb said. 第二种选择也很危险,因为@mateuszb说。 Risk of code injection is very low in Django since Django is designed in such a way. Django的代码注入风险非常低,因为Django是以这种方式设计的。 In Django, capture of entire OS can happen only if the developer writes horribly vulnerable code using eval or exec . 在Django中,只有当开发人员使用evalexec编写可怕的易受攻击的代码时,才能捕获整个操作系统。 If you are not confident about your code quality or even don't know what code injection is, second option is not for you. 如果您对代码质量没有信心,甚至不知道代码注入是什么,则第二个选项不适合您。

Moreover, this error does not occur if you are using a database like mysql and Postgres. 此外,如果您使用的是像mysql和Postgres这样的数据库,则不会发生此错误。 Sqlite is not a good option for a webserver with a high traffic. 对于流量较大的网络服务器来说,Sqlite不是一个好选择。

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

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