flask部署到linux上无法访问sqlite3数据库
自己笔记本上(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 能正常读取数据库)
回答:
把数据库换成绝对路径可以了
虽然相对路径没错,但是必须是绝对路径才行
回答:
不是很清楚你创建app和config中的代码怎么写的,但是应该不是相对路径的问题。
我使用相对路径就可以的。
建议下次提问的时候把报错信息、相关代码全贴出来。
# config文件配置SQLALCHEMY_DATABASE_URI = 'sqlite+pysqlite:///./test.db'
以上是 flask部署到linux上无法访问sqlite3数据库 的全部内容, 来源链接: utcz.com/a/165089.html