# ServiceControl SQL Server Storage From ServiceControl 7, Error and Audit instances can use SQL Server instead of [RavenDB storage](/servicecontrol/storage/ravendb/index.md). Select SQL Server when an instance should use an existing SQL Server deployment. Storage is configured separately for each instance. > [!IMPORTANT] > When using SQL Server, Error instances require [external message body storage](/servicecontrol/storage/external-body/index.md) in addition to the database, even when message bodies fit inline. Audit instances start without it, but then [do not store large or binary bodies](/servicecontrol/storage/external-body/index.md#storage-providers-servicecontrol-auditmessagebodystoragetype). ## Supported SQL Server versions ServiceControl supports the SQL Server versions that are in mainstream support; see the [SQL Server lifecycle](https://learn.microsoft.com/en-us/lifecycle/products/?products=sql-server). Full-Text Search must be installed; see [Prepare the database](#prepare-the-database). ## Prepare the database Create a database before configuring the instance as ServiceControl does not create the database. Setup creates the instance's schema if it does not exist. If the identity that runs setup cannot create schemas, create the schema in advance and grant that identity rights on it. Error and Audit instances can use the same database but not the same schema; see [sharing a database](/servicecontrol/storage/index.md#sharing-a-database). > [!IMPORTANT] > SQL Server Full-Text Search must be installed on the server. Error and Audit instance setup fail without it. The `mcr.microsoft.com/mssql/server` container images do not include it; on Linux, including custom container images, install the `mssql-server-fts` package as described in [Install SQL Server Full-Text Search on Linux](https://learn.microsoft.com/en-us/sql/linux/install-upgrade/setup-full-text-search). Message body search is always enabled for SQL Server storage, regardless of the [`EnableFullTextSearchOnBodies`](/servicecontrol/servicecontrol-instances/configuration.md#ravendb-storage-servicecontrolenablefulltextsearchonbodies) setting. ## Database permissions Grant each identity the following rights on the instance's database: | Identity | Required rights | | --- | --- | | The identity that runs setup | Membership in `db_ddladmin`, `db_datareader`, and `db_datawriter`, or in `db_owner`. Setup creates the schema, tables, indexes, and the full-text catalog and index, and records the applied migrations. | | The identity the instance runs as | Membership in `db_datareader` and `db_datawriter`, or `SELECT`, `INSERT`, `UPDATE`, and `DELETE` on the instance's schema. Normal operation does not change the schema. | When the same connection string is used for setup and normal operation, as the ServiceControl Management Utility and PowerShell do, that identity needs the setup rights. ## Authentication Configure database authentication in the connection string for each Error or Audit instance. This is separate from the transport connection string. ServiceControl uses `Microsoft.Data.SqlClient`. Microsoft Entra ID authentication is supported via the [standard connection string options](https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/azure-active-directory-authentication), including managed identity and service principal authentication. Choose an authentication method suitable for an unattended service. Ensure the selected identity is available to the ServiceControl host and has the database permissions required for setup and normal operation. ## Error instance configuration Set the storage type to `SQLServer`, provide the database connection string, and configure the retention period. Also configure an [external message body store](/servicecontrol/storage/external-body/index.md), which is required even if most bodies fit inline in the database. The ServiceControl Management Utility and the PowerShell module configure it when they create the instance. For manual or container deployments, add the `ServiceControl/MessageBody/*` settings to the instance's configuration file or environment. ### ServiceControl/StorageType _Added in version 7.0.0_ The storage implementation used by the instance. This setting is required; an instance without it does not start. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_STORAGETYPE` | | **App config key** | `ServiceControl/StorageType` | | **SCMU field** | `STORAGE TYPE` | | Type | Default value | | --- | --- | | string | None (required) | Valid values are `RavenDB`, `SQLServer`, and `PostgreSQL`. To choose where message bodies are stored with SQL Server or PostgreSQL storage, use [`ServiceControl/MessageBody/StorageType`](/servicecontrol/servicecontrol-instances/configuration.md#message-body-storage-servicecontrolmessagebodystoragetype). ### ServiceControl/Database/ConnectionString _Added in version 7.0.0_ The connection string for the SQL Server or PostgreSQL database. The database must already exist. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_DATABASE_CONNECTIONSTRING` | | **App config key** | `ServiceControl/Database/ConnectionString` | | **SCMU field** | `DATABASE CONNECTION STRING` | | Type | Default value | | --- | --- | | string | None (required for SQL Server and PostgreSQL storage) | ### ServiceControl/Database/Schema _Added in version 7.0.0_ The schema that contains the Error instance tables. If the schema does not exist, setup creates it, which requires permission to create schemas. Changing this setting does not move existing data; an instance that already has data must keep using the schema that contains it. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_DATABASE_SCHEMA` | | **App config key** | `ServiceControl/Database/Schema` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | string | None. Tables are created in the connection's default schema: the login's default schema on SQL Server (usually `dbo`), or the current schema from `search_path` on PostgreSQL (usually `public`). | ### ServiceControl/ErrorRetentionPeriod The grace period that errored messages are kept before they are deleted. For a message to be considered for deletion, it needs to have a status of either `Archived`, `RetryIssued`, or `Resolved`. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_ERRORRETENTIONPERIOD` | | **App config key** | `ServiceControl/ErrorRetentionPeriod` | | **SCMU field** | `ERROR RETENTION PERIOD` | | Type | Default value | | --- | --- | | timespan | None (required) (SCMU slider defaults to `15 Days`) | Valid range for this setting is between 5 days and 45 days. Set these variables in the Error instance's environment to use file system body storage. Replace the connection string placeholder with credentials supplied by the deployment environment. These storage settings supplement the instance's transport and other required settings. ```text SERVICECONTROL_STORAGETYPE=SQLServer SERVICECONTROL_DATABASE_CONNECTIONSTRING="Server=sqlserver;Database=ServiceControl;User Id=sc;Password=..." SERVICECONTROL_MESSAGEBODY_STORAGETYPE=FileSystem SERVICECONTROL_MESSAGEBODY_FILESYSTEM_STORAGEPATH=/var/servicecontrol/bodies SERVICECONTROL_ERRORRETENTIONPERIOD=15 ``` See the [Error instance settings reference](/servicecontrol/servicecontrol-instances/configuration.md#sql-server-and-postgresql-storage) for the complete reference. To share the ingestion load across several processes, see [Scaling out error ingestion](/servicecontrol/servicecontrol-instances/scaling-out-ingestion.md). Workers use this instance's database, schema, and message body store, and only this instance runs setup. ## Audit instance configuration Set the storage type to `SQLServer`, provide the database connection string, and configure the retention period. Also configure an [external message body store](/servicecontrol/storage/external-body/index.md), which is required even if most bodies fit inline in the database. The ServiceControl Management Utility and the PowerShell module configure it when they create the instance. For manual or container deployments, add the `ServiceControl.Audit/MessageBody/*` settings to the instance's configuration file or environment. ### ServiceControl.Audit/StorageType _Added in version 7.0.0_ The storage implementation used by the Audit instance. This setting is required; an instance without it does not start. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_STORAGETYPE` | | **App config key** | `ServiceControl.Audit/StorageType` | | **SCMU field** | `STORAGE TYPE` | | Type | Default value | | --- | --- | | string | None (required) | Valid values are `RavenDB`, `SQLServer`, and `PostgreSQL`. To choose where message bodies are stored with SQL Server or PostgreSQL storage, use [`ServiceControl.Audit/MessageBody/StorageType`](/servicecontrol/audit-instances/configuration.md#message-body-storage-servicecontrol-auditmessagebodystoragetype). ### ServiceControl.Audit/Database/ConnectionString _Added in version 7.0.0_ The connection string for the SQL Server or PostgreSQL database. The database must already exist. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_DATABASE_CONNECTIONSTRING` | | **App config key** | `ServiceControl.Audit/Database/ConnectionString` | | **SCMU field** | `DATABASE CONNECTION STRING` | | Type | Default value | | --- | --- | | string | None (required for SQL Server and PostgreSQL storage) | ### ServiceControl.Audit/Database/Schema _Added in version 7.0.0_ The schema that contains the Audit instance tables. If the schema does not exist, setup creates it, which requires permission to create schemas. Changing this setting does not move existing data; an instance that already has data must keep using the schema that contains it. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_DATABASE_SCHEMA` | | **App config key** | `ServiceControl.Audit/Database/Schema` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | string | `sc_audit` | ### ServiceControl.Audit/AuditRetentionPeriod The grace period to keep an audit message before it is deleted. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_AUDITRETENTIONPERIOD` | | **App config key** | `ServiceControl.Audit/AuditRetentionPeriod` | | **SCMU field** | `AUDIT RETENTION PERIOD` | | Type | Default value | | --- | --- | | timespan | None (required) | Valid range for this setting is from 1 hour to 365 days. > [!NOTE] > With PostgreSQL storage, the retention period must be between 1 and 90 days. ServiceControl does not start with a period outside that range. SQL Server storage uses the general range above. ### ServiceControl.Audit/MaxBodySizeToStore A size limit, in bytes, for message bodies. Its effect depends on the storage type: | Storage | Effect | | --- | --- | | RavenDB | The largest message body that the Audit instance stores. Larger bodies cannot be viewed in ServicePulse. | | SQL Server or PostgreSQL | The largest text message body that is stored inline in the database. Larger text bodies, and bodies that are not text, are stored in the [external message body store](/servicecontrol/storage/external-body/index.md) and can be viewed in ServicePulse. A larger text body also keeps its first `MaxBodySizeToStore` bytes in the database for full-text search. See [what is stored where](/servicecontrol/storage/external-body/index.md#inline-storage-and-compression). | | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_MAXBODYSIZETOSTORE` | | **App config key** | `ServiceControl.Audit/MaxBodySizeToStore` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | int | `102400` (100 KB) | > [!NOTE] > SQL Server and PostgreSQL storage do not support maintenance mode. An instance with `MaintenanceMode` set to `true` does not start. Example environment variables for an Audit instance using file system body storage: ```text SERVICECONTROL_AUDIT_STORAGETYPE=SQLServer SERVICECONTROL_AUDIT_DATABASE_CONNECTIONSTRING="Server=sqlserver;Database=ServiceControl.Audit;User Id=sc;Password=..." SERVICECONTROL_AUDIT_MESSAGEBODY_STORAGETYPE=FileSystem SERVICECONTROL_AUDIT_MESSAGEBODY_FILESYSTEM_STORAGEPATH=/var/servicecontrol/audit-bodies SERVICECONTROL_AUDIT_AUDITRETENTIONPERIOD=30 ``` See the [Audit instance settings reference](/servicecontrol/audit-instances/configuration.md#sql-server-and-postgresql-storage) for the complete reference. ## Run setup > [!IMPORTANT] > Run the instance setup process (`--setup`) before first use and after every upgrade. Setup applies the database migrations; the instance does not apply them at startup. Setup applies database migrations for both Error and Audit instances. Setup also provisions the external message body store. See [database permissions](#database-permissions) for the rights setup and normal operation need. See the [Error container setup](/servicecontrol/servicecontrol-instances/deployment/containers.md#initial-setup) or [Audit container setup](/servicecontrol/audit-instances/deployment/containers.md#initial-setup) instructions when deploying in containers. The ServiceControl Management Utility (SCMU) and the PowerShell module run setup when they create or upgrade an instance, and can create SQL Server-backed Error and Audit instances: - **SCMU:** choose `SQL Server` as the **STORAGE TYPE**, enter the **DATABASE CONNECTION STRING**, and check the **MESSAGE BODY PATH**, which defaults to `%ProgramData%\Particular\ServiceControl\\MessageBodies`. SCMU stores message bodies only on the file system. - **PowerShell:** pass `-StorageType SQLServer` and `-DatabaseConnectionString` to `New-ServiceControlInstance` or `New-ServiceControlAuditInstance`, and leave out `-DBPath` and `-DatabaseMaintenancePort`, which apply only to RavenDB. Also pass `-MessageBodyPath`, or choose [Azure Blob Storage](/servicecontrol/storage/external-body/azure-blob.md) or [Amazon S3](/servicecontrol/storage/external-body/amazon-s3.md) with `-MessageBodyStorageType`. See the [Error instance](/servicecontrol/servicecontrol-instances/deployment/powershell.md) and [Audit instance](/servicecontrol/audit-instances/deployment/powershell.md) PowerShell guides. To change the message body store after the instance is created, change the message body settings in the instance's configuration file. Bodies already stored are not moved, so make the change before the instance ingests messages. Neither tool sets `Database/Schema`, so instances they create use the default schema. Two Audit instances in one database both use the `sc_audit` schema, and two Error instances that connect with the same login both use that login's default schema, so they share tables. Create independent instances of the same type in separate databases. To share a database between Audit instances on purpose, see [Scaling out audit ingestion](/servicecontrol/audit-instances/scaling-out-ingestion.md#sql-storage-shared-database); those instances must also share a message body store. An Error instance and an Audit instance can use one database, because their default schemas differ. ## Verify the setup Setup logs `SQL Server database migration completed` for an Error instance, or `Audit database migration completed` for an Audit instance, and exits without an error. After the instance starts, check that no storage [custom checks](/servicecontrol/servicecontrol-instances/index.md#notifications-health-monitoring) report a failure in ServicePulse. ## Troubleshooting - **Setup fails with "ServiceControl requires the SQL Server Full-Text Search feature":** install Full-Text Search on the server, then run setup again. - **Setup fails with a permission error:** grant the rights in [database permissions](#database-permissions), or create the schema in advance. - **Setup fails because "A primary instance and an audit instance cannot share a schema":** set a different `Database/Schema` for one of the instances. See [sharing a database](/servicecontrol/storage/index.md#sharing-a-database). - **The instance does not start with "Maintenance mode is not supported":** set `MaintenanceMode` to `false`. Maintenance mode is only available with embedded RavenDB storage. To perform database maintenance, stop the ServiceControl instance and access SQL Server directly. - **The ServiceControl Retention custom check fails:** check the instance log. A body store that refuses deletes stops failed-message retention; see [external message body storage operations](/servicecontrol/storage/external-body/index.md#operations). - **Messages show without their bodies after a restore:** restore the database and the external message body store from the same point in time. See [backup and restore](/servicecontrol/storage/index.md#backup-and-restore). ## Next steps - [Choose and configure an external message body store](/servicecontrol/storage/external-body/index.md) for each instance. - Plan [backup and restore](/servicecontrol/storage/index.md#backup-and-restore) of the database and the message bodies together; RavenDB-specific procedures do not apply. - To deploy in containers, see the [Error instance](/servicecontrol/servicecontrol-instances/deployment/containers.md) and [Audit instance](/servicecontrol/audit-instances/deployment/containers.md) container guides.