检测 SQL 集群
我正在尝试编写一些代码来确定 SQL 服务器列表是否已启动。 我尝试过对每台服务器的 WMI、SQLDMO、SqlDataSourceEnumerator 和 Ping 端口 1433 进行 Ping,取得了不同程度的成功(请参阅下面的结果)。
使用 SQLDMO 和 SqlDataSourceEnumerator,我发现了 6 个中的 3 个,不得不说,3 个丢失的 SQL 服务器中的 2 个形成了一个集群。
Ping 端口 1433 发现 6 个端口中的 4 个,缺少的 2 个是构成 SQL 集群的 2 个服务器。
WMI 被证明是最不成功的,因为它只找到了六分之一的服务器。
以下是我用于服务器发现原型的代码:
private void buildServerMap(bool useLibCOM)
{
sqlServersMap = new Dictionary<string, string>();
if (useLibCOM)
{
//get all available SQL Servers
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
ArrayList servs = new ArrayList();
for (int i = 0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if (srv != null)
{
sqlServersMap.Add(srv.ToString(), srv.ToString());
}
}
}
else
{
System.Data.Sql.SqlDataSourceEnumerator enumSQL = System.Data.Sql.SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = enumSQL.GetDataSources();
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
sqlServersMap.Add((string)row[col], (string)row[col]);
}
}
}
}
private bool pingSqlServer(string server)
{
bool success = false;
TcpClient client = new TcpClient();
try
{
client.Connect(server, 1433);
success = true;
}
catch (Exception)
{
//throw;
}
return success;
}
public StringBuilder buildWmiServerList(string path, string sqlServer, string sqlServerServiceName, StringBuilder sb, out bool reportNeeded)
{
reportNeeded = false;
try
{
ManagementScope ms = new ManagementScope(path);
ms.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE Started = TRUE AND DisplayName='" + sqlServerServiceName + "'");
searcher.Scope = ms;
if (searcher != null && searcher.Get() != null)
{
foreach (ManagementObject service in searcher.Get())
{
sb.Append(sqlServer + " SQL Server service - RUNNING\n");
}
}
}
catch (Exception e)
{
sb.Append(sqlServer + " SQL Server service - UNVERIFIABLE\n");
reportNeeded = true;
}
return sb;
}
关于如何解析/检测形成 SQL 集群的 SQL 服务器有什么想法吗?
I am trying to write some code that will determine if a list of SQL servers are up. I have tried to WMI, SQLDMO, SqlDataSourceEnumerator, and Pinging port 1433 of each server, with varying degrees of success (see results below).
Using SQLDMO and SqlDataSourceEnumerator, i found 3 out of 6, it has to be said that 2 of the 3 missing SQL servers form a cluster.
Pinging port 1433 found 4 out of the 6, the 2 missing are the 2 servers that form the SQL cluster.
WMI proved to be the least successful, in that it only found 1 out of 6 servers.
Here is the code I used to prototype the server discovery:
private void buildServerMap(bool useLibCOM)
{
sqlServersMap = new Dictionary<string, string>();
if (useLibCOM)
{
//get all available SQL Servers
SQLDMO.Application sqlApp = new SQLDMO.ApplicationClass();
SQLDMO.NameList sqlServers = sqlApp.ListAvailableSQLServers();
ArrayList servs = new ArrayList();
for (int i = 0; i < sqlServers.Count; i++)
{
object srv = sqlServers.Item(i + 1);
if (srv != null)
{
sqlServersMap.Add(srv.ToString(), srv.ToString());
}
}
}
else
{
System.Data.Sql.SqlDataSourceEnumerator enumSQL = System.Data.Sql.SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = enumSQL.GetDataSources();
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
sqlServersMap.Add((string)row[col], (string)row[col]);
}
}
}
}
private bool pingSqlServer(string server)
{
bool success = false;
TcpClient client = new TcpClient();
try
{
client.Connect(server, 1433);
success = true;
}
catch (Exception)
{
//throw;
}
return success;
}
public StringBuilder buildWmiServerList(string path, string sqlServer, string sqlServerServiceName, StringBuilder sb, out bool reportNeeded)
{
reportNeeded = false;
try
{
ManagementScope ms = new ManagementScope(path);
ms.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE Started = TRUE AND DisplayName='" + sqlServerServiceName + "'");
searcher.Scope = ms;
if (searcher != null && searcher.Get() != null)
{
foreach (ManagementObject service in searcher.Get())
{
sb.Append(sqlServer + " SQL Server service - RUNNING\n");
}
}
}
catch (Exception e)
{
sb.Append(sqlServer + " SQL Server service - UNVERIFIABLE\n");
reportNeeded = true;
}
return sb;
}
Any ideas in how to resolve/detect SQL servers that form a SQL cluster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 DMO 已被弃用,但在 SMO 中您可以执行 Server.IsClustered。
你可能还想看看这个:
http://www.sqldbatips.com/showarticle.asp?ID=45
i don't know about DMO which is deprecated anyway but in SMO you can do Server.IsClustered.
you may also want to look at this:
http://www.sqldbatips.com/showarticle.asp?ID=45
为什么不尝试 SQL ping? 这里有源代码,因此您可以了解他们是如何做到的。
无论如何,有一些想法:
您是否正在尝试物理服务器、集群名称或虚拟服务器名称,例如 phys1、phys2、vclus、vserv1、vserv2(假设主动/主动)?
您的客户端使用 TCP 或命名管道吗? 您尝试过 tcp:vserv1 吗?
例如,如果禁用然后启用,命名管道在集群 IIRC 上可能会很有趣。
SQL 日志中列出了使用的协议以及使用的 tcp 端口(命名实例=随机)
您可以在客户端上创建系统 DSN 吗? 由此可以知道HKLM\SW\Microsoft\MSSQLServer下使用了什么端口和协议
Why not try SQL ping? There is source code here so you can see how they do it.
Anyway, some thoughts:
Are you trying the the physical server, the cluster name, or the virtual server name(s) eg phys1, phys2, vclus, vserv1, vserv2 (assuming active/active)?
Are you using tcp or named pipes from your client? Have you tried tcp:vserv1?
Named pipes can be funny on clusters IIRC if disabled then enabled, for example.
The protocols used are listed in the SQL logs, as well as the tcp port that is used (named instance = random)
Can you create a system DSN on your client? From this, you can work out what port and protocol is used under HKLM\SW\Microsoft\MSSQLServer