【问题标题】:Django / Pyodbc / Apache - Connection error when using ApacheDjango / Pyodbc / Apache - 使用 Apache 时出现连接错误
【发布时间】:2015-05-21 16:06:03
【问题描述】:

我正在尝试使用 django-pyodbc-azure 包从 Django 应用程序连接到 SQL Server 后端。

我能够使用 Django manage.py shell(ORM 和 pyodbc 查询)和开发服务器(视图函数中的 HttpResponse)连接和返回数据,但是当我尝试使用 Apache 和 mod_wsgi 时,我收到了错误:

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

我正在使用 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(使用 dsn-less)和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()


在 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'
        }
    }
}


使用 manage.py runserver 时,我可以在浏览器中查看数据。

从解释器我也可以这样做:

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', )


但是由于某种原因,当我尝试使用 Apache 时,初始 pyodbc 数据库连接尝试失败。
非常感谢您对此提供任何帮助。

【问题讨论】:

  • 我不确定这是否可行(我对此表示怀疑),但您的 TDS 版本实际上应该是 7.2,而不是 7.4。虽然 7.4 是 Microsoft Spec 中的最新版本,但 FreeTDS 仅支持 7.2:freetds.org/userguide/choosingtdsprotocol.htm
  • @FlipperPA 我最初的版本是 7.2 时遇到问题并将其更改为 7.4 希望它可能会有所帮助,但感谢您的建议。
  • RedHat 通常启用 SELinux,这对在 Apache 下运行的代码实际可以执行的操作施加了限制。您可能需要调整 SELinux 策略。请先尝试暂时禁用 SELinux 以确认。
  • @GrahamDumpleton 解决了!我关注了link 并在 /etc/selinux/config 中设置了 SELINUX=passive 但 /var/log/messages 中没有条目,所以我不确定是什么被阻止了。你知道我能在不完全禁用 SELinux 的情况下找到这个吗?感谢您的帮助。
  • 对不起。对 SELinux 知之甚少,除了将人们指向何处以暂时禁用它以确定这是否是问题的原因。

标签: django apache mod-wsgi pyodbc sql-server-2014


【解决方案1】:

你试过跑步吗

sudo setsebool -P httpd_can_network_connect=1

在机器上?

【讨论】:

  • 我最终只是简单地禁用了 SELinux 并将其留在那里。不过谢谢你的建议,好像安全多了。
猜你喜欢
  • 1970-01-01
  • 2016-10-08
  • 2014-03-20
  • 2017-12-26
  • 2016-03-11
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
相关资源
最近更新 更多