patternsqlModerate
Find all IP's or users who access a specific SQL Server
Viewed 0 times
allsqlwhofindserverusersspecificaccess
Problem
Let's say I found a SQL Server and I have no idea which applications connect to it. Maybe I find one application but I am not sure if it is the only one using it.
Is there a nice way of finding all distinct connections?
Is there a nice way of finding all distinct connections?
Solution
You can look at the currently running processes in SSMS in Activity Monitor.
You can also get it using sys.dm_exec_sessions as well as sys.dm_exec_connections and sys.dm_exec_requests.
Query:
Hostname, process name or domain user names often give more information than just an IP address. Other columns can be added such as:
IP and other information can be retrieved with a JOIN to sys.dm_exec_connections on
sys.dm_exec_requests can also give useful information:
This query is just a sample. You must
Data from these view disapear when SQL Server is restarted. Therefore if the application is not regularly connected it may be a good idea to store them on a regular basis using a job or a script (Powershell).
You can also get it using sys.dm_exec_sessions as well as sys.dm_exec_connections and sys.dm_exec_requests.
Query:
SELECT DB_NAME(database_id) as [DB]
, login_name
, nt_domain
, nt_user_name
, status
, host_name
, program_name
, COUNT(*) AS [Connections]
FROM sys.dm_exec_sessions
WHERE database_id > 0 -- OR 4 for user DBs
GROUP BY database_id, login_name, status, host_name, program_name, nt_domain, nt_user_name;Hostname, process name or domain user names often give more information than just an IP address. Other columns can be added such as:
login_time, last_successful_logonIP and other information can be retrieved with a JOIN to sys.dm_exec_connections on
session_id: client_net_address, local_net_address, connect_time, ports, ...sys.dm_exec_requests can also give useful information:
command type, sql_handle, ...This query is just a sample. You must
JOIN these 3 views together and output/store relevant information from either of them. Data from these view disapear when SQL Server is restarted. Therefore if the application is not regularly connected it may be a good idea to store them on a regular basis using a job or a script (Powershell).
Code Snippets
SELECT DB_NAME(database_id) as [DB]
, login_name
, nt_domain
, nt_user_name
, status
, host_name
, program_name
, COUNT(*) AS [Connections]
FROM sys.dm_exec_sessions
WHERE database_id > 0 -- OR 4 for user DBs
GROUP BY database_id, login_name, status, host_name, program_name, nt_domain, nt_user_name;Context
StackExchange Database Administrators Q#130977, answer score: 17
Revisions (0)
No revisions yet.