flask部署到linux上无法访问sqlite3数据库

发布于 2022-09-06 15:09:34 字数 4269 浏览 19 评论 0

自己笔记本上(Windows 10)测试可以正常运行
部署到服务器上失败

报错信息

i Feb 09 20:42:22.808818 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] mod_wsgi (pid=2365): Exception occurred processing WSGI script '/data/wwwroot/default/OBlog/flask.wsgi'. [Fri
Feb 09 20:42:22.808861 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] Traceback (most recent call last): [Fri Feb 09 20:42:22.809462 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/usr/lib64/python3.4/site-packages/flask/app.py", line 1997, in
call [Fri Feb 09 20:42:22.809472 2018] [wsgi:error] [pid 2365] [client 223.91.36.119:26414] return self.wsgi_app(environ,
start_response) [Fri Feb 09 20:42:22.809502 2018] [wsgi:error] [pid
2365] [client 223.91.36.119:26414] File
"/usr/lib64/python3.4/site-packages/flask/app.py", line 1985, in
wsgi_app [Fri Feb 09 20:42:22.809507 2018] [wsgi:error] [pid 2365]
[client 223.91.36.119:26414] response = self.handle_exception(e)
[Fri Feb 09 20:42:22.809527 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/usr/lib64/python3.4/site-packages/flask/app.py", line 1540, in
handle_exception [Fri Feb 09 20:42:22.809531 2018] [wsgi:error] [pid
2365] [client 223.91.36.119:26414] reraise(exc_type, exc_value,
tb) [Fri Feb 09 20:42:22.809640 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/usr/lib64/python3.4/site-packages/flask/_compat.py", line 33, in
reraise [Fri Feb 09 20:42:22.809647 2018] [wsgi:error] [pid 2365]
[client 223.91.36.119:26414] raise value [Fri Feb 09
20:42:22.809671 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/usr/lib64/python3.4/site-packages/flask/app.py", line 1982, in
wsgi_app [Fri Feb 09 20:42:22.809675 2018] [wsgi:error] [pid 2365]
[client 223.91.36.119:26414] response =
self.full_dispatch_request() [Fri Feb 09 20:42:22.809693 2018]
[wsgi:error] [pid 2365] [client 223.91.36.119:26414] File
"/usr/lib64/python3.4/site-packages/flask/app.py", line 1614, in
full_dispatch_request [Fri Feb 09 20:42:22.809697 2018] [wsgi:error]
[pid 2365] [client 223.91.36.119:26414] rv =
self.handle_user_exception(e) [Fri Feb 09 20:42:22.809713 2018]
[wsgi:error] [pid 2365] [client 223.91.36.119:26414] File
"/usr/lib64/python3.4/site-packages/flask/app.py", line 1517, in
handle_user_exception [Fri Feb 09 20:42:22.809717 2018] [wsgi:error]
[pid 2365] [client 223.91.36.119:26414] reraise(exc_type,
exc_value, tb) [Fri Feb 09 20:42:22.809733 2018] [wsgi:error] [pid
2365] [client 223.91.36.119:26414] File
"/usr/lib64/python3.4/site-packages/flask/_compat.py", line 33, in
reraise [Fri Feb 09 20:42:22.809736 2018] [wsgi:error] [pid 2365]
[client 223.91.36.119:26414] raise value [Fri Feb 09
20:42:22.809752 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/usr/lib64/python3.4/site-packages/flask/app.py", line 1610, in
full_dispatch_request [Fri Feb 09 20:42:22.809755 2018] [wsgi:error]
[pid 2365] [client 223.91.36.119:26414] rv =
self.preprocess_request() [Fri Feb 09 20:42:22.809771 2018]
[wsgi:error] [pid 2365] [client 223.91.36.119:26414] File
"/usr/lib64/python3.4/site-packages/flask/app.py", line 1831, in
preprocess_request [Fri Feb 09 20:42:22.809774 2018] [wsgi:error] [pid
2365] [client 223.91.36.119:26414] rv = func() [Fri Feb 09
20:42:22.809885 2018] [wsgi:error] [pid 2365] [client
223.91.36.119:26414] File "/data/wwwroot/default/OBlog/OBlog/views/default.py", line 76, in
before_request [Fri Feb 09 20:42:22.809892 2018] [wsgi:error] [pid
2365] [client 223.91.36.119:26414] g.db =
sqlite3.connect(app.config['DATABASE']) [Fri Feb 09 20:42:22.809914
2018] [wsgi:error] [pid 2365] [client 223.91.36.119:26414]
sqlite3.OperationalError: unable to open database file

看了下,大概是sqlite3.OperationalError: unable to open database file访问数据库失败

但是我数据库和文件夹都给了777权限

图片描述

实在不知道为什么会这样(我直接ssh里运行python然后import sqlite3 能正常读取数据库)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪了杯 2022-09-13 15:09:34

把数据库换成绝对路径可以了
虽然相对路径没错,但是必须是绝对路径才行

初见你 2022-09-13 15:09:34

不是很清楚你创建app和config中的代码怎么写的,但是应该不是相对路径的问题。
我使用相对路径就可以的。
建议下次提问的时候把报错信息、相关代码全贴出来。

# config文件配置
SQLALCHEMY_DATABASE_URI = 'sqlite+pysqlite:///./test.db'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文