---
title: Setting Up Database Monitoring for self-hosted SQL Server
description: Install and configure Database Monitoring for self-hosted SQL Server
breadcrumbs: >-
  Docs > Database Monitoring > Setting up SQL Server > Setting Up Database
  Monitoring for self-hosted SQL Server
---

# Setting Up Database Monitoring for self-hosted SQL Server

Database Monitoring provides deep visibility into your Microsoft SQL Server databases by exposing query metrics, query samples, explain plans, database states, failovers, and events.

Do the following steps to enable Database Monitoring with your database:

1. Grant the Agent access
1. Install the Agent

## Before you begin{% #before-you-begin %}

{% dl %}

{% dt %}
Supported SQL Server versions
{% /dt %}

{% dd %}
2012, 2014, 2016, 2017, 2019, 2022
{% /dd %}

{% dt %}
Supported Agent versions
{% /dt %}

{% dd %}
7.41.0+
{% /dd %}

{% dt %}
Performance impact
{% /dt %}

{% dd %}
The default Agent configuration for Database Monitoring is conservative, but you can adjust settings such as the collection interval and query sampling rate to better suit your needs. For most workloads, the Agent represents less than one percent of query execution time on the database and less than one percent of CPU.Database Monitoring runs as an integration on top of the base Agent ([see benchmarks](https://docs.datadoghq.com/database_monitoring/agent_integration_overhead/?tab=sqlserver)).
{% /dd %}

{% dt %}
Proxies, load balancers, and connection poolers
{% /dt %}

{% dd %}
The Datadog Agent must connect directly to the host being monitored. The Agent should not connect to the database through a proxy, load balancer, or connection pooler. If the Agent connects to different hosts while it is running (as in the case of failover, load balancing, and so on), the Agent calculates the difference in statistics between two hosts, producing inaccurate metrics.
{% /dd %}

{% dt %}
Data security considerations
{% /dt %}

{% dd %}
Read about how Database Management handles [sensitive information](https://docs.datadoghq.com/database_monitoring/data_collected/#sensitive-information) for information about what data the Agent collects from your databases and how to ensure it is secure.
{% /dd %}

{% /dl %}

## Grant the Agent access{% #grant-the-agent-access %}

The Datadog Agent requires read-only access to the database server in order to collect statistics and queries.

Create a read-only login to connect to your server and grant the required permissions:

{% tab title="SQL Server 2014+" %}

```SQL
CREATE LOGIN datadog WITH PASSWORD = '<PASSWORD>';
CREATE USER datadog FOR LOGIN datadog;
GRANT CONNECT ANY DATABASE to datadog;
GRANT VIEW SERVER STATE to datadog;
GRANT VIEW ANY DEFINITION to datadog;
-- If not using either of Log Shipping Monitoring (available in Agent v7.50+) or
-- SQL Server Agent Monitoring (available in Agent v7.57+), comment out the next three lines:
USE msdb;
CREATE USER datadog FOR LOGIN datadog;
GRANT SELECT to datadog;
```

{% /tab %}

{% tab title="SQL Server 2012" %}

```SQL
CREATE LOGIN datadog WITH PASSWORD = '<PASSWORD>';
CREATE USER datadog FOR LOGIN datadog;
GRANT VIEW SERVER STATE to datadog;
GRANT VIEW ANY DEFINITION to datadog;
-- If not using either of Log Shipping Monitoring (available in Agent v7.50+) or
-- SQL Server Agent Monitoring (available in Agent v7.57+), comment out the next three lines:
USE msdb;
CREATE USER datadog FOR LOGIN datadog;
GRANT SELECT to datadog;
```

Create the `datadog` user in each additional application database:

```SQL
USE [database_name];
CREATE USER datadog FOR LOGIN datadog;
```

{% /tab %}

### Securely store your password{% #securely-store-your-password %}

Store your password using secret management software such as [Vault](https://www.vaultproject.io/). You can then reference this password as `ENC[<SECRET_NAME>]` in your Agent configuration files: for example, `ENC[datadog_user_database_password]`. See [Secrets Management](https://docs.datadoghq.com/agent/configuration/secrets-management/) for more information.

The examples on this page use `datadog_user_database_password` to refer to the name of the secret where your password is stored. It is possible to reference your password in plain text, but this is not recommended.

## Install the Agent{% #install-the-agent %}

It's recommended to install the agent directly on the SQL Server host as that enables the agent to collect a variety of system telemetry (CPU, memory, disk, network) in addition to SQL Server specific telemetry.

{% tab title="Windows Host" %}
**Note: For AlwaysOn users, the recommended approach is to install the Agent on each individual replica, which will help provide related infrastructure metrics (CPU, memory, networking) for each replica**. Telemetry for the relevant availability groups will be collected from each replica the agent is connected to. As a fallback method, the Agent can be installed on a separate server and connected to the cluster through the listener endpoint, but this will make it impossible to relate infrastructure metrics for the underlying host. To start collecting SQL Server telemetry, first [install the Datadog Agent](https://app.datadoghq.com/account/settings#agent/windows).

Create the SQL Server Agent conf file `C:\ProgramData\Datadog\conf.d\sqlserver.d\conf.yaml`. See the [sample conf file](https://github.com/DataDog/integrations-core/blob/master/sqlserver/datadog_checks/sqlserver/data/conf.yaml.example) for all available configuration options.

```yaml
init_config:
instances:
  - dbm: true
    host: '<HOSTNAME>,<PORT>'
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    # Optional: For additional tags
    tags:  
      - 'service:<CUSTOM_SERVICE>'
      - 'env:<CUSTOM_ENV>'
```

To use [Windows Authentication](https://docs.microsoft.com/en-us/sql/relational-databases/security/choose-an-authentication-mode), set `connection_string: "Trusted_Connection=yes"` and omit the `username` and `password` fields.

The agent supports [SQL Server Browser Service](https://learn.microsoft.com/en-us/sql/tools/configuration-manager/sql-server-browser-service) in versions 7.41+. To enable SSBS, provide a port of `0` in the host string: `<HOSTNAME>,0`.

Use the `service` and `env` tags to link your database telemetry to other telemetry through a common tagging scheme. See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging) on how these tags are used throughout Datadog.

### Supported Drivers{% #supported-drivers %}

#### Microsoft ADO{% #microsoft-ado %}

The recommended [ADO](https://docs.microsoft.com/en-us/sql/ado/microsoft-activex-data-objects-ado) provider is [Microsoft OLE DB Driver](https://docs.microsoft.com/en-us/sql/connect/oledb/oledb-driver-for-sql-server). Ensure the driver is installed on the host where the agent is running.

```yaml
connector: adodbapi
adoprovider: MSOLEDBSQL19  # Replace with MSOLEDBSQL for versions 18 and lower
```

The other two providers, `SQLOLEDB` and `SQLNCLI`, are considered deprecated by Microsoft and should no longer be used.

#### ODBC{% #odbc %}

The recommended ODBC driver is [Microsoft ODBC Driver](https://docs.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server). Ensure the driver is installed on the host where the Agent is running.

```yaml
connector: odbc
driver: 'ODBC Driver 18 for SQL Server'
```

Once all Agent configuration is complete, [restart the Datadog Agent](https://docs.datadoghq.com/agent/configuration/agent-commands/#start-stop-and-restart-the-agent).

### Validate{% #validate %}

[Run the Agent's status subcommand](https://docs.datadoghq.com/agent/configuration/agent-commands/#agent-status-and-information) and look for `sqlserver` under the **Checks** section. Navigate to the [Databases](https://app.datadoghq.com/databases) page in Datadog to get started.
{% /tab %}

{% tab title="Linux Host" %}
**Note: For AlwaysOn users, the recommended approach is to install the Agent on each individual replica, which will help provide related infrastructure metrics (CPU, memory, networking) for each replica**. Telemetry for the relevant availability groups will be collected from each replica the agent is connected to. As a fallback method, the Agent can be installed on a separate server and connected to the cluster through the listener endpoint, but this will make it impossible to relate infrastructure metrics for the underlying host. To start collecting SQL Server telemetry, first [install the Datadog Agent](https://app.datadoghq.com/account/settings#agent).

On Linux, the Datadog Agent additionally requires an ODBC SQL Server driver to be installed—for example, the [Microsoft ODBC driver](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server). Once an ODBC SQL Server is installed, copy the `odbc.ini` and `odbcinst.ini` files into the `/opt/datadog-agent/embedded/etc` folder.

Use the `odbc` connector and specify the proper driver as indicated in the `odbcinst.ini` file.

Create the SQL Server Agent conf file `/etc/datadog-agent/conf.d/sqlserver.d/conf.yaml`. See the [sample conf file](https://github.com/DataDog/integrations-core/blob/master/sqlserver/datadog_checks/sqlserver/data/conf.yaml.example) for all available configuration options.

```yaml
init_config:
instances:
  - dbm: true
    host: '<HOSTNAME>,<PORT>'
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    connector: odbc
    driver: '<Driver from the `odbcinst.ini` file>'
    # Optional: For additional tags
    tags:  
      - 'service:<CUSTOM_SERVICE>'
      - 'env:<CUSTOM_ENV>'
```

Use the `service` and `env` tags to link your database telemetry to other telemetry through a common tagging scheme. See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging) on how these tags are used throughout Datadog.

Once all Agent configuration is complete, [restart the Datadog Agent](https://docs.datadoghq.com/agent/configuration/agent-commands/#start-stop-and-restart-the-agent).

### Validate{% #validate %}

[Run the Agent's status subcommand](https://docs.datadoghq.com/agent/configuration/agent-commands/#agent-status-and-information) and look for `sqlserver` under the **Checks** section. Navigate to the [Databases](https://app.datadoghq.com/databases) page in Datadog to get started.
{% /tab %}

## Example Agent Configurations{% #example-agent-configurations %}

### Connecting with DSN using the ODBC driver on Linux{% #connecting-with-dsn-using-the-odbc-driver-on-linux %}

1. Locate the `odbc.ini` and `odbcinst.ini` files. By default, these are placed in the `/etc` directory when installing ODBC.

1. Copy the `odbc.ini` and `odbcinst.ini` files into the `/opt/datadog-agent/embedded/etc` folder.

1. Configure your DSN settings as follows:

`odbcinst.ini` must provide at least one section header and ODBC driver location.

Example:

   ```text
   [ODBC Driver 18 for SQL Server]
   Description=Microsoft ODBC Driver 18 for SQL Server
   Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.3.so.2.1
   UsageCount=1
   ```

`odbc.ini` must provide a section header and a `Driver` path that matches `odbcinst.ini`.

Example:

   ```text
   [datadog]
   Driver=/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.3.so.2.1
   ```

1. Update the `/etc/datadog-agent/conf.d/sqlserver.d/conf.yaml` file with your DSN information.

Example:

   ```yaml
   instances:
     - dbm: true
       host: 'localhost,1433'
       username: datadog
       password: 'ENC[datadog_user_database_password]'
       connector: 'odbc'
       driver: 'ODBC Driver 18 for SQL Server' # This is the section header of odbcinst.ini
       dsn: 'datadog' # This is the section header of odbc.ini
   ```

1. Restart the Agent.

### Using AlwaysOn{% #using-alwayson %}

For AlwaysOn users, the Agent should be installed on each replica server and connected directly to each replica. The full set of AlwaysOn telemetry is collected from each individual replica, in addition to host-based telemetry (CPU, disk, memory, and so on) for each server.

```yaml
instances:
  - dbm: true
    host: 'shopist-prod,1433'
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    database_metrics:
      # If Availability Groups is enabled
      ao_metrics:
        enabled: true
      # If Failover Clustering is enabled
      fci_metrics:
        enabled: true
```

### Monitoring SQL Server Agent Jobs{% #monitoring-sql-server-agent-jobs %}

{% alert level="info" %}
To enable monitoring of SQL Server Agent jobs, the Datadog Agent must have access to the [msdb] database.
{% /alert %}

{% alert level="danger" %}
SQL Server Agent Jobs monitoring is not available for Azure SQL Database.
{% /alert %}

Monitoring of SQL Server Agent jobs is supported on SQL Server versions 2016 and newer. Starting from Agent v7.57, the Datadog Agent can collect SQL Server Agent job metrics and histories. To enable this feature, set `enabled` to `true` in the `agent_jobs` section of the SQL Server integration configuration file. The `collection_interval` and `history_row_limit` fields are optional.

```yaml
instances:
  - dbm: true
    host: 'shopist-prod,1433'
    username: datadog
    password: '<PASSWORD>'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    agent_jobs:
      enabled: true
      collection_interval: 15
      history_row_limit: 10000
```

### Collecting schemas{% #collecting-schemas %}

{% alert level="danger" %}
Datadog Agent v7.56+ and SQL Server 2017 or higher are required for SQL Server schema collection.
{% /alert %}

To enable this feature, use the `collect_schemas` option. Schemas are collected on databases for which the Agent has `CONNECT` access.

{% alert level="info" %}
To collect schema information from RDS instances, you must grant the `datadog` user explicit `CONNECT` access to each database on the instance. For more information, see [Grant the Agent access](https://docs.datadoghq.com/database_monitoring/setup_sql_server/rds/?tab=windowshost#grant-the-agent-access).
{% /alert %}

Use the `database_autodiscovery` option to avoid specifying each logical database. See the sample [sqlserver.d/conf.yaml](https://github.com/DataDog/integrations-core/blob/master/sqlserver/datadog_checks/sqlserver/data/conf.yaml.example) for more details.

```yaml
init_config:
instances:
 # This instance detects every logical database automatically
  - dbm: true
        host: 'shopist-prod,1433'
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    database_autodiscovery: true
    collect_schemas:
      enabled: true
    database_metrics:
      # Optional: enable metric collection for indexes
      index_usage_metrics:
        enabled: true
# This instance only collects schemas and index metrics from the `users` database
  - dbm: true
        host: 'shopist-prod,1433'
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    database: users
    collect_schemas:
      enabled: true
    database_metrics:
      # Optional: enable metric collection for indexes
      index_usage_metrics:
        enabled: true
```

**Note**: For Agent v7.68 and below, use `schemas_collection` instead of `collect_schemas`.

### One Agent connecting to multiple hosts{% #one-agent-connecting-to-multiple-hosts %}

It is common to configure a single Agent host to connect to multiple remote database instances (see [Agent installation architectures](https://docs.datadoghq.com/database_monitoring/architecture/) for DBM). To connect to multiple hosts, create an entry for each host in the SQL Server integration config.

{% alert level="info" %}
Datadog recommends using one Agent to monitor no more than 30 database instances.Benchmarks show that one Agent running on a t4g.medium EC2 instance (2 CPUs and 4GB of RAM) can successfully monitor 30 RDS db.t3.medium instances (2 CPUs and 4GB of RAM).
{% /alert %}

```yaml
init_config:
instances:
  - dbm: true
    host: 'example-service-primary.example-host.com,1433'
    username: datadog
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: 'example-service–replica-1.example-host.com,1433'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: 'example-service–replica-2.example-host.com,1433'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
    [...]
```

### Running custom queries{% #running-custom-queries %}

To collect custom metrics, use the `custom_queries` option. See the sample [sqlserver.d/conf.yaml](https://github.com/DataDog/integrations-core/blob/master/sqlserver/datadog_checks/sqlserver/data/conf.yaml.example) for more details.

```yaml
init_config:
instances:
  - dbm: true
    host: 'localhost,1433'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    custom_queries:
    - query: SELECT age, salary, hours_worked, name FROM hr.employees;
      columns:
        - name: custom.employee_age
          type: gauge
        - name: custom.employee_salary
           type: gauge
        - name: custom.employee_hours
           type: count
        - name: name
           type: tag
      tags:
        - 'table:employees'
```

### Working with hosts through a remote proxy{% #working-with-hosts-through-a-remote-proxy %}

If the Agent must connect to a database host through a remote proxy, all telemetry is tagged with the hostname of the proxy rather than the database instance. Use the `reported_hostname` option to set a custom override of the hostname detected by the Agent.

```yaml
init_config:
instances:
  - dbm: true
    host: 'localhost,1433'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: products-primary
  - dbm: true
    host: 'localhost,1433'
    connector: adodbapi
    adoprovider: MSOLEDBSQL
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: products-replica-1
```

### Discovering ports automatically{% #discovering-ports-automatically %}

SQL Server Browser Service, Named Instances, and other services can automatically detect port numbers. You can use this instead of hardcoding port numbers in connection strings. To use the Agent with one of these services, set the `port` field to `0`.

For example, a Named Instance config:

```yaml
init_config:
instances:
  - host: <hostname\instance name>
    port: 0
```

## Further reading{% #further-reading %}

- [Basic SQL Server Integration](https://docs.datadoghq.com/integrations/sqlserver/)
- [Troubleshoot Common Issues](https://docs.datadoghq.com/database_monitoring/troubleshooting/?tab=sqlserver)
- [Configure Deadlock Monitoring](https://docs.datadoghq.com/database_monitoring/guide/sql_deadlock/)
- [Configure Query Completion and Query Error Collection](https://docs.datadoghq.com/database_monitoring/guide/sql_extended_events/)
- [Capturing SQL Query Parameter Values](https://docs.datadoghq.com/database_monitoring/guide/parameterized_queries/)
- [Strategize your Azure migration for SQL workloads with Datadog](https://www.datadoghq.com/blog/migrate-sql-workloads-to-azure-with-datadog/)
- [Monitor your AlwaysOn availability groups with Datadog Database Monitoring](https://www.datadoghq.com/blog/datadog-monitoring-always-on/)
