'Can't connect MOD_WSGI .conf file with my DATABASE (Exception: (1046, 'No database selected'))
the thing is that i created a web app in python with flask, connected to an a mysql server. Im trying to run it on a apache2 server with mod wsgi. The web app in apache is working, after install all packages required, created an env with python3.9.. but when i'm trying to do the login, the apache error log shows an error:
[Mon May 23 11:08:42.395088 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50084] Exception: (1046, 'No database selected')
when i change the user on /etc/.../sites-available/app.conf file, the user that is trying to connect database is the same that the user is in the .../sites-available/app.conf file
FILE APP.CONF
<VirtualHost *:80>
ServerAdmin apipython@localhost
ServerName apipython.asix
ServerAlias www.apipython.asix
DocumentRoot /var/www/apipython
WSGIDaemonProcess apipython user=www-data threads=10
WSGIScriptAlias / /var/www/apipython/app.wsgi
<Directory /var/www/apipython>
WSGIProcessGroup apipython
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
when change the user www-data, is that the user trying to connect database I had to create a user on mysql server name www-data
i dont know how to specify a database on app.conf file. im showing my error log, and my app.wsgi file..
ERROR LOG:
[Mon May 23 11:21:51.515747 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] [2022-05-23 11:21:51,515] ERROR in app: Exception on /login [POST]
[Mon May 23 11:21:51.515859 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Traceback (most recent call last):
[Mon May 23 11:21:51.515889 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/models/ModelUser.py", line 47, in getAccount
[Mon May 23 11:21:51.515911 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] cursor.execute(sql)
[Mon May 23 11:21:51.515936 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/cursors.py", line 206, in execute
[Mon May 23 11:21:51.515958 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] res = self._query(query)
[Mon May 23 11:21:51.515978 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/cursors.py", line 319, in _query
[Mon May 23 11:21:51.516027 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] db.query(q)
[Mon May 23 11:21:51.516052 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/MySQLdb/connections.py", line 254, in query
[Mon May 23 11:21:51.516072 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] _mysql.connection.query(self, query)
[Mon May 23 11:21:51.516092 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] MySQLdb._exceptions.OperationalError: (1046, 'No database selected')
[Mon May 23 11:21:51.516112 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226]
[Mon May 23 11:21:51.516133 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] During handling of the above exception, another exception occurred:
[Mon May 23 11:21:51.516153 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226]
[Mon May 23 11:21:51.516173 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Traceback (most recent call last):
[Mon May 23 11:21:51.516192 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 2077, in wsgi_app
[Mon May 23 11:21:51.516212 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] response = self.full_dispatch_request()
[Mon May 23 11:21:51.516234 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1525, in full_dispatch_request
[Mon May 23 11:21:51.516254 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] rv = self.handle_user_exception(e)
[Mon May 23 11:21:51.516273 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1523, in full_dispatch_request
[Mon May 23 11:21:51.516295 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] rv = self.dispatch_request()
[Mon May 23 11:21:51.516315 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/lib/python3.8/site-packages/flask/app.py", line 1509, in dispatch_request
[Mon May 23 11:21:51.516334 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
[Mon May 23 11:21:51.516354 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/app.py", line 83, in login
[Mon May 23 11:21:51.516373 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] accounts = ModelUser.getAccount(db,logged_user)
[Mon May 23 11:21:51.516395 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] File "/var/www/apipython/models/ModelUser.py", line 58, in getAccount
[Mon May 23 11:21:51.516415 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] raise Exception(ex)
[Mon May 23 11:21:51.516443 2022] [wsgi:error] [pid 3529] [remote 127.0.0.1:50226] Exception: (1046, 'No database selected')
APP.WSGI FILE:
#from app import app as applicationimport sys
import sys
sys.path.insert(0, '/var/www/apipython/')
sys.path.append('anaconda3/envs/iaw/lib/python3.9/site-packages/')
activate_this = '/home/alumne/.local/share/virtualenvs/anaconda3-iy5Unvg4-PFT_Zdiy/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
from app import app as application
PD: THE DATABASE IS ALREADY CONFIGURE IN THE CONF.PY FILE THAT I CREATED WHILE I WAS DEVELOPING THE APP, AND IS IMPORTED ON MY APP.PY
CONFIG.PY:
class Config:
SECRET_KEY = 'B!1w8NAt1T^%kvhUI*S^'
class DevelopmentConfig(Config):
#DEBUG = True
#MYSQL_HOST = '192.168.127.62'
#MYSQL_USER = 'webserver'
#MYSQL_PASSWORD = 'alumne'
#MYSQL_DB = 'inmibank_bbdd'
DEBUG = True
MYSQL_HOST = 'localhost'
MYSQL_USER = 'root'
MYSQL_PASSWORD = 'alumne'
MYSQL_DB = 'proba2'
config = {
'development': DevelopmentConfig
}
web is working fine in development server, served by python in visualcode if u guys have any idea let me know please.
Thanks BTW
Solution 1:[1]
OMG, I BEEN AT LEAST 2 DAYS TRIYING TO SOLVE THIS THE ONLY THING THAT I DID IS CHANGE THE USER AND GROUP FROM MY APP.CONF FILE I SHOW YOU GUYS
<VirtualHost *:80>
ServerAdmin apipython@localhost
ServerName apipython.asix
ServerAlias www.apipython.asix
DocumentRoot /var/www/apipython
WSGIDaemonProcess apipython user=alumne threads=5 group=alumne
WSGIScriptAlias / /var/www/apipython/app.wsgi
<Directory /var/www/apipython>
WSGIProcessGroup apipython
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
NOW IS WORKING FOR MEEE!!!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frandys Roque |
