【问题标题】:Django database connection with Host and Instances with SQL Server使用 SQL Server 连接主机和实例的 Django 数据库
【发布时间】:2019-12-16 08:19:41
【问题描述】:

我使用 SQL Server 作为数据库的后端。在setting.py 文件中,我需要将主机名与实例一起使用。因此,我得到了下面给出的错误,

The above exception (('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]TCP Provider: No connection could be made because the target machine actively refused it.\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Server Native Client 11.0]Login timeout expired (0); [08001] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [08001] [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. (10061)')) was the direct cause of the following exception:

在setting.py文件中,

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'xxxxx',
        'USER': 'xxx',
        'PASSWORD': 'xxxx',
        'PORT': '1433',
        'HOST': 'aaa\bbb',(hostname\instance)
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0',
        },
    }
}

如何解决此错误并连接到我的数据库?

【问题讨论】:

  • 您是否检查过您的主机上的端口 1433 是否打开?
  • 是的。它是开放的
  • 有一个新的答案可以直接解决这个问题,你可以选择接受它或者忽略它/点赞

标签: python sql-server django django-pyodbc-azure


【解决方案1】:

您的帖子是两年前发布的,但我现在遇到了同样的问题。 通过查看https://github.com/michiya/django-pyodbc-azure/issues/201

问题似乎来自连接字符串中包含端口的方式。以这种方式指定端口时,问题将得到解决:

'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'xxxxx',
    'USER': 'xxx',
    'PASSWORD': 'xxxx',
    'HOST': 'aaa\bbb',(hostname\instance)
    'OPTIONS': {
        'driver': 'SQL Server Native Client 11.0',
    },
    'extra_params': 'PORT=1433'

我试过了,效果很好。

【讨论】:

    【解决方案2】:

    由于您的 settings.py 看起来不错,我认为这是一个普遍的问题,可能是由数据库端的多个错误配置引起的,例如:

    • 在 SQL Server 配置管理中禁用 TCP/IP 连接
    • “IP ALL” 部分的 TCP/IP 属性中,无法配置端口 1433,因此它拒绝您的请求
    • 可能突然停止许多 SQL Windows 服务之一

    我发现this stack overflow 的帖子非常有用,其中包含涵盖上述每一点的多个答案。

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多