3

I'm trying to connect to a SQL Server backend from a Django app using the django-pyodbc-azure package.

I'm able to connect and return data using the Django manage.py shell (both ORM and pyodbc queries) and development server (HttpResponse in a view function), but when I attempt to use Apache and mod_wsgi, I receive the error:

('08S01', '[08S01] [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')

I'm using Red Hat Enterprise Linux 7, Python 3.3.5, Pyodbc 3.0.7, Django 1.7, Apache 2.4.6, Mod_WSGI 4.4.5, SQL Server 2014, FreeTDS 0.91 (using dsn-less), and Django-Pyodbc-Azure 1.2.0


Modwsgi.conf:

<VirtualHost *:80>
    WSGIDaemonProcess xxx.xxx.xxx.xxx python-path=/path/to/mysite.com:/path/to/virtualenv/lib/python3.3/site-packages processes=2 threads=15
    WSGIProcessGroup xxx.xxx.xxx.xxx

    WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py

    <Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
</VirtualHost>


wsgi.py:

"""
WSGI config for mysite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


In settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'hostname',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'PORT': '1433',
        'OPTIONS': {
            'driver': 'FreeTDS',
            'host_is_server': True,
            'unicode_results': True,
            'extra_params': 'tds_version=7.4'
        }
    }
}


When using manage.py runserver I can view data in a browser.

From the interpreter I can also do:

In [1]: import pyodbc
In [2]: conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=servername;PORT=1433;DATABASE=dbname;UID=dbuser;PWD=dbpassword')
In [3]: cursor = conn.cursor()
In [4]: cursor.execute("select top 5 test_names from test_table;").fetchone()
Out[4]: ('John', )


But for some reason the initial pyodbc database connection attempt fails when I try to use Apache.
Any help on this is much appreciated.

5
  • I'm not sure if this would do it (I doubt it), but your TDS Version should actually be 7.2, not 7.4. While 7.4 is the latest version in the Microsoft Spec, FreeTDS only support through 7.2: freetds.org/userguide/choosingtdsprotocol.htm Commented Mar 18, 2015 at 18:52
  • @FlipperPA I initially had the version at 7.2 when I encountered the problem and changed it to 7.4 in hopes it might help, but thank you for the suggestion. Commented Mar 18, 2015 at 19:03
  • RedHat often has SELinux enabled which applies restrictions on what code running under Apache can actually do. You may need to adjust the SELinux policies. Try temporarily disabling SELinux first to confirm. Commented Mar 18, 2015 at 22:32
  • @GrahamDumpleton That fixed it! I followed this link and set SELINUX=passive in /etc/selinux/config but there were no entries in /var/log/messages so I'm not exactly sure what was being blocked. Do you know of anyway I can I find this out without leaving SELinux completely disabled? Thank you for your help. Commented Mar 19, 2015 at 19:11
  • Sorry. Know very little about SELinux except for where to point people to to temporarily disable it to determine if that is the cause of problems. Commented Mar 19, 2015 at 20:51

1 Answer 1

1

Have you tried running

sudo setsebool -P httpd_can_network_connect=1

on the machine?

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up simply disabling SELinux and leaving it at that. But thanks for your suggestion, it seems much more safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.