ConnectionFailure when connecting to MongoHQ URI via Flask-PyMongo on httpd - python

I have a Flask app with a Mongo backend working perfectly in my local dev environment. I moved it to an AWS-hosted RHEL instance and got the Flask app working via httpd/mod_wsgi. However, connecting to a DB on MongoHQ continues to fail.
The code for the connection is this:
from flask import Flask
from flask.ext.pymongo import PyMongo
app = Flask(__name__)
app.config["MONGO_URI"] = 'mongodb://myusername:mypasswd#myhost.mongohq.com:myport/mydb'
mongo = PyMongo(app)
A sample query:
#app.route('/books')
def books()
all_books = mongo.db.listings.distinct("bookinfo")
return all_books
The error message from Apache (edited for readability):
mod_wsgi (pid=5116, process='myProcess', application=''): Loading WSGI script '/route/to/myapp.wsgi'.
mod_wsgi (pid=5116): Target WSGI script '/route/to/myapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=5116): Exception occurred processing WSGI script '/route/to/myapp.wsgi'.
Traceback (most recent call last):
File "/route/to/myapp.wsgi", line 10, in <module>
from myApp import app as application
File "/route/to/myapp.py", line 8, in <module>
mongo = PyMongo(app)
File "/route/to/my/venv/lib/python2.6/site-packages/flask_pymongo/__init__.py", line 98, in __init__
self.init_app(app, config_prefix)
File "/route/to/my/venv/python2.6/site-packages/flask_pymongo/__init__.py", line 230, in init_app
cx = connection_cls(*args, **kwargs)
File "/route/to/my/venv/python2.6/site-packages/pymongo/mongo_client.py", line 352, in __init__
raise ConnectionFailure(str(e))
ConnectionFailure: could not connect to myhost.mongohq.com:myport: [Errno 13] Permission denied
My thoughts/investigation:
The app runs fine when I remove the DB Connection: So it's not a
mod_wsgi, Apache, or permissions issue?
I can connect to the mongo uri via the Mongo shell and Python shell but not through the app: So it's not a firewall issue?
MongoHQ's ultraresponsive customer service (seriously, they're awesome) believes it's an issue with the Flask-PyMongo driver, but I have no idea how to debug that.
Any help/hints/possibilities would be appreciated.

SELinux was the root cause for this. I'd disabled the firewall but never turned off SELinux by editing the /etc/sysconfig/selinux file. After doing so, the app still had issues, but this logging code helped tremendously:
import logging
file_handler = logging.FileHandler(filename='/var/www/myapp/flaskerror.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)

Related

Python:: Use pymysql.connect() to Connect to MySQL Docker Container

Trying to build off this post and this post and this tutorial, I'm attempting to write a Python script that can connect to my MySQL Docker container. I'd like to use the pymysql library and then the pymysql.connect() command for ease of use. FYI, the host machine is Ubuntu 16.04.7, Docker version is 20.10.7.
Okay: Here's the docker-compose.yml section spinning up my MySQL container:
MySQL_DB:
container_name: MyMYSQL
image: 667ee8fb158e
ports:
- "52000:3306"
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: password123
command: mysqld --general-log=1 --general-log-file=/var/lib/mysql/general-log.log
volumes:
- ./logs/mysql.log:/var/lib/mysql/general-log.log
I can't remember where I got this template, but the container is up and running just fine. Note that I'm exposing the container's TCP ports; all other SO posts mentioned that was required for remote connections.
Okay, here's the script I'm using:
# From:
# https://www.geeksforgeeks.org/connect-to-mysql-using-pymysql-in-python/
import pymysql
def mysqlconnect():
# To connect MySQL database
conn = pymysql.connect(host='172.20.0.2',user='me123',password="password123",db='DB01',port=3306)
# To close the connection
conn.close()
# Driver Code
if __name__ == "__main__" :
mysqlconnect()
My Docker-Compose instance assigned the IP address of "127.20.0.2," and I can ping it from the host machine (and within the container).
Running the code generates this error:
me123#ubuntu01/home/me123$ sudo /usr/bin/python3 ./pythonScript.py
Traceback (most recent call last):
File "./pythonScript.py", line 27, in <module>
mysqlconnect()
File "./pythonScript.py", line 14, in mysqlconnect
conn = pymysql.connect(host='172.20.0.2',user='me123',password="password123",db='DB01',port=3306)
File "/usr/lib/python3/dist-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pymysql/connections.py", line 699, in __init__
self.connect()
File "/usr/lib/python3/dist-packages/pymysql/connections.py", line 936, in connect
self._request_authentication()
File "/usr/lib/python3/dist-packages/pymysql/connections.py", line 1165, in _request_authentication
auth_packet = self._process_auth(plugin_name, auth_packet)
File "/usr/lib/python3/dist-packages/pymysql/connections.py", line 1227, in _process_auth
raise err.OperationalError(2059, "Authentication plugin '%s' not configured" % plugin_name)
pymysql.err.OperationalError: (2059, "Authentication plugin 'b'caching_sha2_password'' not configured")
me123#ubuntu01/home/me123$
"Authentication plugin '%s' not configured" strongly suggests that when I run the script, my container is denying the connection. Sadly, there is nothing in the log to explain why this is. Google searches on pymysql.connect() pull up information on how to configure this command, but little to troubleshoot it. Does anyone see what I'm doing wrong?

Can't host my django website on shared hosting

I am trying to deploy my django project on shared hosting. I am trying from couple of hours but still now I can't host my django website and getting "Internal Server Error Error 500".
this is my root passenger_wsgi.py
from my_root_folder_name.wsgi import application
#setting.py
ALLOWED_HOSTS = ['mydomain.com','www.mydomain.com']
I also installed python app and django on my server. Now I am hopeless. I am trying from last five hours for host an website but still now I can't. Where I am doing mistake???
I am seeing those error from error log:
File "/home/sellvqed/virtualenvass/dfdf/3.8/lib/python3.8/site-packages/django/apps/registry.py", line 83, in populate
raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant
Finally I solved my problems after delete all apps and virtual folder from my hosting account. T

I can't migrate on heroku. Using Django MySQL

I can't migrate on Heroku. Using Django and MySQL.
I don't what wrong with it.
There are some wrongs on setting_mysql.py?
I got an error like this.
(base) mypc#mypc website % heroku run python manage.py migrate
Running python manage.py migrate on ⬢ myapp... up, run.3308 (Free)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/MySQLdb/__init__.py", line 130, in Connect
return Connection(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
super().__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
You have provided the wrong database access credentials, because if I'm not mistaken - you can't connect to the database via socket.
Try official solution:
On Heroku, sensitive credentials are stored in the environment as
config vars. This includes database connection information (named
DATABASE_URL), which is traditionally hardcoded in Django
applications.
The django-heroku package automatically configures your Django
application to work on Heroku. It is compatible with Django 2.0
applications.
It provides many niceties, including the reading of DATABASE_URL,
logging configuration, a Heroku CI–compatible TestRunner, and
automatically configures ‘staticfiles’ to “just work”.
Installing django-heroku:
pip install django-heroku
Be sure to add django-heroku to your requirements.txt file as well.
Add the following import statement to the top of settings.py:
import django_heroku
Then add the following to the bottom of settings.py:
Activate Django-Heroku:
django_heroku.settings(locals())
Deploy, and you should be good to go!
I'm using this solution and it works for me.
Another option is to copy the database access data in the database panel and manually put it into settings.py

How to Host Python, Django, PostgreSQL Application on IIS 10

I am trying to host my Django application on Windows 2016 server IIS server. I had used Python, Django, pipenv as virtual environment and PostgreSQL as Database.
How to host is using a Domain Name ?
I had tried almost tried everything available on the internet but till now I am not successful.
Maybe I have not got the perfect tutorials or find the correct one to host the Django Application.
Please help me host the Django Application on IIS 10.
I will be really grateful for the help
Thanks in advance.
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "c:\users\sachin kumar\.virtualenvs\login-zru7l_54\lib\site-packages\wfastcgi.py", line 586, in get_wsgi_handler
raise Exception('WSGI_HANDLER env var must be set')
Exception: WSGI_HANDLER env var must be set
StdOut:
StdErr:
Check your settings of FASTCGI Module and add needed environment variables
Full docs from Microsoft to run python scripts on IIS are here
offtop: don't use space in your user_name (and in the user folder respectively).

Error when running dev_appserver.py on gcloud "ImportError: No module named MySQLdb._mysql"?

I am trying to deploy a Python-Flask application on Google App Engine. The application works fine without MySQL. On importing from flask_mysqldb import MySQL dev_appserver.py returns the error ImportError: No module named MySQLdb._mysql
I have updated app.yaml and requirements.txt (tried MySQL-python, PyMySQL as well)
libraries:
- name: MySQLdb
version: latest
Flask-MySQLdb==0.2.0
mysqlclient==1.4.1
First few lines of main.py
from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
from flask_mysqldb import MySQL
app = Flask(__name__)
# Config MySQL
app.config['MYSQL_HOST'] = '35.236.72.247'
app.config['MYSQL_USER'] = <removed>
app.config['MYSQL_PASSWORD'] = <removed>
app.config['MYSQL_DB'] = 'myFlaskApp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# Initialize MySQL
mysql = MySQL(app)
Terminal dump of dev_appserver.py app.yaml
INFO 2019-02-09 01:13:00,098 devappserver2.py:278] Skipping SDK update check.
INFO 2019-02-09 01:13:00,172 api_server.py:275] Starting API server at: http://localhost:52922
INFO 2019-02-09 01:13:00,178 dispatcher.py:256] Starting module "default" running at: http://localhost:8080
INFO 2019-02-09 01:13:00,179 admin_server.py:150] Starting admin server at: http://localhost:8000
INFO 2019-02-09 01:13:02,374 instance.py:294] Instance PID: 28866
ERROR 2019-02-09 01:13:04,295 wsgi.py:263]
Traceback (most recent call last):
File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/zq/Desktop/Python-Flask/main.py", line 3, in <module>
from flask_mysqldb import MySQL
File "/Users/zq/Desktop/Python-Flask/lib/flask_mysqldb/__init__.py", line 1, in <module>
import MySQLdb
File "/Users/zq/Desktop/Python-Flask/lib/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
File "/Users/zq/Desktop/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1097, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named MySQLdb._mysql
INFO 2019-02-09 01:13:04,301 module.py:861] default: "GET / HTTP/1.1" 500 -
How do I fix this? Or otherwise, use MySQL in my flask app? The application works fine locally WITH MySQL on my system.
git rm -f <project-dir>/lib/MySQLdb
gcloud app deploy
Deleted lib/MysqlDB and pushed it again to google cloud and it worked. Apparently, my local library was conflicting with AppEngine's native mysqldb.
Now I just dispense with running dev_appserver.py app.yaml
This is just an explanation for the conflict you mentioned in your answer.
You were mixing the 2 distinct procedures for using 3rd party libraries in your GAE app:
requesting a built-in/GAE-provided library (via the libraries config in the app.yaml file), which in the mysqldb case also requires local system installation for use with the local development server.
bundling your own library copy with your app (via your requirements.txt file)
The action described in your answer removed the copy of the library bundled with your app, which is sufficient for the GAE-deployed app to work fine. Note that you didn't mention:
the removal of the library from the requirements.txt file which, if not done, means you'd run into the problem again if/when re-doing the lib installation using the file
if running locally is ok - i.e. if the required mysqldb installation on the local system is actually done

Categories

Resources