0

我使用 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',
        },
    }
}

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

4

2 回答 2

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'

我试过了,效果很好。

于 2021-08-07T16:40:20.303 回答
0

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

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

我发现这个堆栈溢出帖子非常有用,有多个答案涵盖了上述每一点。

于 2019-12-16T09:52:18.640 回答