SQL 无法使用 localhost\sqlserver 连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6565628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot connect to database using localhost\sqlserver
提问by Wullie
I know we should be aware of such phrases but all of a sudden all my connections to localhost\sqlexpress
stopped working. As .\sqlexpress
and (local)\sqlexpress
work, this wouldn't be a big deal if it weren't for the fact that I am working on a team project where all the connection strings are defined in the former format. And obviously I have a hard time to accept it shouldn't work anymore so I spent several hours analysing and reinstalling several SQL Server versions to no avail. (And on the way breaking the db projects in VS2010 so I needed to reinstall as well...).
我知道我们应该知道这些短语,但突然之间我所有的联系都localhost\sqlexpress
停止了。作为.\sqlexpress
和(local)\sqlexpress
工作,这不会是一个大问题,如果不是因为,我对所有的连接字符串前格式定义的团队项目工作的事实。显然我很难接受它不应该再工作,所以我花了几个小时分析和重新安装几个 SQL Server 版本无济于事。(并且在破坏 VS2010 中的 db 项目的过程中,我也需要重新安装......)。
But here are the details. As already mentioned I couldn't connect to localhost\sqlexpress
but to .\sqlexpress
. The same symptoms using SQL Server Manager, and connection strings for deployment and running wcf services. Funnily enough resolving the default instance as localhost
works.
但这里是细节。如前所述,我无法连接localhost\sqlexpress
到.\sqlexpress
. 使用 SQL Server 管理器的相同症状,以及用于部署和运行 wcf 服务的连接字符串。有趣的是,将默认实例解析为localhost
有效。
First things first, yes the SQL Server is running and just to be sure I enabled TCP/IP and set the port fixed to 1433 although it works on my colleagues machine with TCP/IP disabled. (Somewhere along the way I read that localhost
can be resolved using shared memory when using the native SQL client so I guess that is what's happening on his system.)
首先,是的,SQL Server 正在运行,只是为了确保我启用了 TCP/IP 并将端口设置为固定为 1433,尽管它可以在禁用 TCP/IP 的同事机器上运行。(在我阅读的某个地方,localhost
可以在使用本机 SQL 客户端时使用共享内存解决,所以我猜这就是他的系统上发生的事情。)
The setup is only one SQL Server Express 2008 R2 instance and VS2010 on Windows 7 Professional.
设置只有一个 SQL Server Express 2008 R2 实例和 Windows 7 Professional 上的 VS2010。
Starting up Sql express the errorlog says:
启动 Sql express 错误日志说:
Server is listening on [ 'any' <ipv6> 1433].
Server is listening on [ 'any' <ipv4> 1433].
Netstat says (excerpt):
Netstat 说(摘录):
TCP 0.0.0.0:1433 Machinenename:0 LISTENING
TCP [::]:1433 Machinenename:0 LISTENING
I note the absence of 127.0.0.1:1433 and any UDP on port 1433
我注意到端口 1433 上没有 127.0.0.1:1433 和任何 UDP
In order to diagnose the problem I use sqlcmd to find out what transport is used:
为了诊断问题,我使用 sqlcmd 找出使用的传输方式:
1. localhost\sqlexpress
1. 本地主机\sqlexpress
sqlcmd -Slocalhost\sqlexpress -> Shared memory
sqlcmd -Stcp:localhost\sqlexpress -> SQL Server Network Interfaces: Error Locating Server/Instance Specified
Cannot connect to localhost\sqlexpress from SQL Server Management Studio even if I specify shared memory as protocol
即使我将共享内存指定为协议,也无法从 SQL Server Management Studio 连接到 localhost\sqlexpress
2. localhost
2.本地主机
sqlcmd -Slocalhost -> TCP
sqlcmd -Stcp:localhost -> TCP
Can connect to localhost from SQL Server Management Studio
可以从 SQL Server Management Studio 连接到 localhost
3. (local)\sqlexpress
3.(本地)\sqlexpress
sqlcmd -S(local)\sqlexpress -> Shared memory
sqlcmd -Stcp:(local)\sqlexpress -> SQL Server Network Interfaces: Error Locating Server/Instance Specified
Can connect to (local)\sqlexpress from SQL Server Management Studio
可以从 SQL Server Management Studio 连接到 (local)\sqlexpress
So I am lost here. Any help is highly appreciated.
所以我在这里迷路了。任何帮助都受到高度赞赏。
回答by Jonnadula Sasikumar
in MS SQL, go to Configuration tools -> SQL Server Configuration Manager Select SQL Server Network Configuration -> Select protocol in the right side window enable tcp/ip and restart the services in services.msc then try to connect to localhost\sqlexpress
在 MS SQL 中,转到配置工具 -> SQL Server 配置管理器选择 SQL Server 网络配置 -> 在右侧窗口中选择协议启用 tcp/ip 并重新启动 services.msc 中的服务,然后尝试连接到 localhost\sqlexpress
回答by James L.
Have you verified that Shared Memory is enabled in SQL Server Configuration Manager, under SQL Server Network Configuration | Protocols?
您是否确认在 SQL Server 配置管理器中的 SQL Server 网络配置下启用了共享内存?协议?
When I disable the "Shared Memory" protocol, "sqlcmd -S.\sqlexpress" times out. Enabling it makes the command work just fine.
当我禁用“共享内存”协议时,“sqlcmd -S.\sqlexpress”超时。启用它可以使命令正常工作。
One more thought... Port 1434 is used for the SQL Server Discovery service, which lets programs like SQL Management Studio and the like discover that SQL Server is running on the machine. Just one more thing to investigate.
另一个想法... 端口 1434 用于 SQL Server 发现服务,它允许 SQL Management Studio 等程序发现 SQL Server 正在机器上运行。还有一件事要调查。
回答by Andrew Shepherd
We had this problem. For some reason, on this machine, an Alias had been created that directed the server name we were using (localhost\SQL2008_R2) to a server which didn't exist any more.
我们遇到了这个问题。出于某种原因,在这台机器上,创建了一个别名,将我们使用的服务器名称 (localhost\SQL2008_R2) 指向一个不再存在的服务器。
Once we deleted the Alias it all worked.
一旦我们删除了别名,一切就都奏效了。
The aliases are a tree node in SQL Server Configuration Manager.
别名是 SQL Server 配置管理器中的树节点。