简体   繁体   English

连接字符串DNS查找是否被缓存?

[英]Do connection string DNS lookups get cached?

Suppose the following: 假设以下内容:

I have a database set up on database.mywebsite.com , which resolves to IP 111.111.1.1 , running from a local DNS server on our network. 我在database.mywebsite.com上建立了一个数据库,该数据库解析为IP 111.111.1.1 ,从我们网络上的本地DNS服务器运行。

I have countless ASP, ASP.NET and WinForms applications that use a connection string utilising database.mywebsite.com as the server name, all running from the internal network. 我有无数的ASP,ASP.NET和WinForms应用程序,这些应用程序使用以database.mywebsite.com作为服务器名称的连接字符串,它们都从内部网络运行。

Then the box running the database dies, and I switch over to a new box with an IP of 222.222.2.2 . 然后运行数据库的盒子消失了,我切换到IP为222.222.2.2的新盒子。

So, I update the DNS for database.mywebsite.com to point to 222.222.2.2 . 因此,我将database.mywebsite.com的DNS更新为指向222.222.2.2

Will all the applications and computers running them have cached the old resolved IP address? 所有运行它们的应用程序和计算机是否都将缓存旧的已解析IP地址?

I'm assuming they will have. 我假设他们会的。

Any suggestions along the lines of "don't have your IP change each time you switch box" are not too welcome as I cannot control this aspect of the situation, unfortunately. 不太欢迎您提出“每次切换盒时都不要更改IP”的建议,因为我无法控制这种情况。 We are currently using the machine name of the box, which changes every time it dies and all apps etc. have to be updated with the new machine name. 我们目前使用的是机器的机器名称,该名称每次消失时都会更改,并且所有应用程序等都必须使用新的机器名称进行更新。 It hurts. 这很痛。

Even if the DNS is not cached local to the machine, it will likely be cached somewhere along the DNS chain between the machine and the name servers, at least for a short while. 即使DNS不在计算机本地缓存,它也可能至少在短时间内缓存在计算机和名称服务器之间DNS链上的某个位置。 My understanding is this situation would usually be handled with IP takeover where you just make the new machine 111.111.1.1. 我的理解是,这种情况通常将通过IP接管来解决,您只需将新机器制造为111.111.1.1。

Probably a question for serverfault. 可能是服务器故障的问题。

You're looking for DNS TTL (Time To Live) I guess.. In my opinion applications may cache the IP for at most the value of the TTL. 我猜您正在寻找DNS TTL(生存时间)。在我看来,应用程序最多可以为TTL值缓存IP。 I'm afraid however that some applications/technologies might actually cache it longer (agian in my opinion completely wrong) 但是,我担心某些应用程序/技术实际上可能会缓存更长的时间(我认为Agian完全错误)

Each machine will cache the ip address. 每台机器将缓存IP地址。

The length of time it is cached is the TTL (Time To Live). 它被缓存的时间长度是TTL(生存时间)。 This is a setting on your DNS server, if you set it very low say 5 mins, then you show be up and running fairly quikly. 这是DNS服务器上的一项设置,如果将其设置得很低(例如5分钟),那么您将很快启动并运行。 A bit of a hack but it should work. 有点破解,但应该可以。

Yes, the other comments are correct in that what controls this is the DNS TTL set for the hostname database.mywebsite.com. 是的,其他注释是正确的,因为控制这是为主机名database.mywebsite.com设置的DNS TTL。

You'll have to decide what the maximum amount of time you're willing to wait for if you have a failure on your primary address (111.111.1.1) after you make the switch to the secondary address. 切换到辅助地址后,如果主地址(111.111.1.1)发生故障,您必须决定愿意等待的最长时间。 Lower settings will give you a quicker recovery time, but will also increase the load and bandwidth to your DNS server because clients will have to re-query it to refresh their cache more often. 较低的设置将为您提供更快的恢复时间,但同时也会增加DNS服务器的负载和带宽,因为客户端将不得不重新查询它以更频繁地刷新其缓存。

You can use nslookup using the -d option from your cmd prompt to see what your default TTL times and remaining TTL times are for the DNS server you are querying. 您可以使用cmd提示符中的-d选项使用nslookup,以查看查询的DNS服务器的默认TTL时间和剩余TTL时间。

%> nslookup -d google.com

You should assume that they are cashed for two reasons not clearly mentioned before: 您应假定它们被兑现的原因有两个,之前没有明确提及:

1- Many "modern" versions of OS families do DNS caching. 1- OS系列的许多“现代”版本都进行DNS缓存。 2- Many applications do DNS caching or have poor error/failure detection on live connections and/or opening new connections. 2-许多应用程序都进行DNS缓存,或者在实时连接和/或打开新连接时检测错误/失败的能力很差。 This would possibly include your database client. 这可能包括您的数据库客户端。

Also, this is probably not well documented. 另外,这可能没有很好的记录。 I did some googling, and found this for MySQL: 我进行了一些谷歌搜索,并发现它适用于MySQL:

http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html#connector-net-programming-connecting-errors http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming-connecting-connection-string.html#connector-net-programming-connecting-errors

It does not clearly explain its behavior in this regard. 它没有明确解释其在这方面的行为。

The DNS gets cached, but for any server that resolves to the wrong ip address, you can update the HOSTS file of the server and the ip should be updated immediately. DNS被缓存,但是对于解析为错误IP地址的任何服务器,您可以更新服务器的HOSTS文件,并且IP应该立即更新。 This could be a solution if you have a limited amount of servers accessing your database server. 如果访问数据库服务器的服务器数量有限,这可能是一个解决方案。

I had a similar issue with a web site that disables the application pool recycling features and runs for weeks on end. 我在网站上遇到了类似的问题,该网站禁用了应用程序池回收功能,并连续运行了数周。 Sometimes, a clustered SQL Server box would restart and for some reason, my SqlConnection's were not reconnecting. 有时,群集的SQL Server框将重新启动,并且由于某种原因,我的SqlConnection没有重新连接。 I was getting the error: 我收到错误:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

The server was there - and running - in fact, if I just recycled the app pool, the app would work fine - but I don't like recycling app pools! 服务器在那里-并且正在运行-实际上,如果我只是回收应用程序池,则该应用程序可以正常工作-但我不喜欢回收应用程序池!

The connections that were being held in the connection pool were somehow using old connection information, and that could have been old IP addresses. 连接池中保留的连接以某种方式使用了旧的连接信息,并且可能是旧的IP地址。 This is what seems so similar to the poster's question, that it appears to be cached DNS information, because as soon as some sort of a cache is cleared, the app works fine. 这似乎与发布者的问题非常相似,似乎是在缓存DNS信息,因为一旦清除了某种缓存,该应用程序就可以正常工作。

This is how I solved it - by forcing all of the connections in the pool to be re-created: 这就是我解决问题的方法-通过强制重新创建池中的所有连接:

Try
    ' Example: SqlDependency, but this could also be any SqlConnection.Open call
    Dim result As Boolean = SqlClient.SqlDependency.Start(ConnStr)
Catch sqlex As SqlClient.SqlException
    SqlClient.SqlConnection.ClearAllPools()
End Try

The code sample is just the boiled-down basics - it should be tweaked for your situation! 代码示例只是精简的基础知识-应该针对您的情况进行调整!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM