SQL Server 2008 将 localhost 解析为计算机名称
我们正在从 Windows Server 2003 切换到 Windows Server 2008 的过程中,遇到了一个令人沮丧的问题:
在旧环境中,一些开发人员使用 sql server 2005,另一些开发人员使用 sql server 2008。没有问题。我们的连接字符串都指向:localhost/sqlserver。现在,在新的 Server 2008 环境中,Sql Server 2008 偶尔会解析“localhost”,这会立即导致抛出异常。
在 sys.servers 中,我们使用以下命令更改了条目以指向 localhost/sqlserver:
exec sp_dropserver 'buckaroo-banzai\sqlserver'
exec sp_addserver 'localhost\sqlserver', local
exec sp_serveroption 'localhost\sqlserver', 'data access', TRUE
最常见的违规 sql 语句如下所示(我知道这是已弃用的形式)(注意:它们不是唯一的违规者,只是最常见的那些):
[localhost\sqlserver].[database].[table].exec sp_executesql blahblah; exec sp_another_sp
我从中得到的错误是:
Server buckaroo-bonzai\sqlserver not found in sys.servers
将我的 sys.servers 条目切换回 Buckaroo-bonzai\sqlserver 会出现此错误:
Server localhost-bonzai\sqlserver not found in sys.servers
如果所有内容将 sql 服务器引用为 Buckaroo-bonzai\sqlserver一切正常,但对于开发来说,这不是一个选择。
根据记录,这种情况以前在我们的 Windows Server 2003 环境中没有发生过,只是在新的 Server 2008 环境中发生过;有什么想法吗?
我想到的可能的解决方法:
- 从版本控制中删除app.config和web.config文件(yech)
- 费力地在版本控制文件(双yech)中保留不同的连接字符串版本
- 控制文件,但以某种方式有一些统一的引用方式到本地主机上的sql服务器(也许是localhost?)=)
- 实际上弄清楚为什么sql server 2008正在解析localhost并停止它。 (耶!)
We're in the process of switching from Windows Server 2003 to Windows Server 2008, and we've encountered a frustrating problem:
In the old environment, some developers used sql server 2005 and others used sql server 2008. No problems. Our connection strings all pointed to: localhost/sqlserver. Now, on the new Server 2008 environment, Sql Server 2008 is occasionally resolving the "localhost" which immediately causes the thing to throw an exception.
In sys.servers we've changed the entry to point to localhost/sqlserver using:
exec sp_dropserver 'buckaroo-banzai\sqlserver'
exec sp_addserver 'localhost\sqlserver', local
exec sp_serveroption 'localhost\sqlserver', 'data access', TRUE
and the most frequent offending sql statements look like this (I know it's deprecated form) (note: they aren't the only offenders, just the most common ones):
[localhost\sqlserver].[database].[table].exec sp_executesql blahblah; exec sp_another_sp
The error I'm getting from those is:
Server buckaroo-bonzai\sqlserver not found in sys.servers
switching my sys.servers entry back to buckaroo-bonzai\sqlserver gets this error:
Server localhost-bonzai\sqlserver not found in sys.servers
If everything refers to the sql server as buckaroo-bonzai\sqlserver everything works, but for development, this just isn't an option.
For the record, this hasn't happened before on our windows server 2003 environments, just the new server 2008 environments; any ideas?
possible workarounds I've thought of:
- remove the app.config and web.config files from version control (yech)
- laboriously keep different connection strings in version controlled files (double yech)
- version control the files, but somehow have some unified way of referring to an sql server on a local host (localhost maybe? )= )
- Actually figure out why sql server 2008 is resolving localhost and stop it dead. (yay!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查主机文件以进行重定向
Check the hosts file for redirection
如果使用“.”,效果会更好吗?而不是“localhost”,如“.\sqlserver”?单个句点指的是本地计算机,但显然无法对其进行 DNS 查找,因此我怀疑 SQL Server 是否会尝试。
Does it work any better if you use "." instead of "localhost", as in ".\sqlserver"? A single period refers to the local machine, but there's obviously no way to do a DNS lookup on it, so I doubt SQL Server would try.
它不是 DNS 或 HOST 文件:由于没有 sys.servers 条目,因此无法解析由 4 部分组成的对象名称。
您也许可以使用 sp_addlinkedserver 解决
现在,这可能会失败(我无法测试),因为 sys.servers 中的“data_source”将与“localhost\sqlserver”重复。但是,您可以将其更改为“127.0.0.1\sqlserver”或“actualservername\sqlserver”之类的内容以删除重复项。
我确信我不久前做过此操作...
编辑,评论后:该错误可能与 SQL 2008 中环回链接服务器的行为更改。抱歉,我不知道这件事。
sp_addserver 仅适用于更改本地服务器名称(即@@servername)。
It's not DNS or HOST file: the 4 part object name can't resolve because of no sys.servers entry.
You might be able to workaround with sp_addlinkedserver
Now, this may fail (I can't test) because the "data_source" in sys.servers would be duplicated with "localhost\sqlserver". However, you can change this to something like "127.0.0.1\sqlserver" or "actualservername\sqlserver" to remove the duplicate.
I'm sure I did this some time ago...
Edit, after comment: The error might be related to Behaviour change in SQL 2008 for loopback linked servers. Sorry, i did't know about this.
sp_addserver is only useful for changing local server name (that is @@servername).
嗯,听起来像是 DNS 问题。不确定为什么将本身引用为“localhost”时会出现问题,但您可能需要检查系统 HOSTS 文件,以确保该文件没有以某种方式更改。
DNS 查询的解析顺序如下:
Well, sounds like a DNS issue. Not sure why it would have problems referring to itself as "localhost", but you might want to check your system HOSTS file, to make sure that hasn't been changed somehow.
The order of resolution for a DNS query is as follows: