Skip to content

Commit 4e07643

Browse files
committed
Single-user mode on Linux
1 parent 970d3fc commit 4e07643

4 files changed

Lines changed: 133 additions & 89 deletions

File tree

docs/database-engine/configure-windows/start-sql-server-in-single-user-mode.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ helpviewer_keywords:
1818

1919
Under certain circumstances, you may have to start an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode by using the **startup option** `-m`. For example, you may want to change server configuration options or recover a damaged `master` database or other system database. Both actions require starting an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode.
2020

21+
For restoring a `master` database on Linux in single-user mode, see [Restore the master database on Linux in single-user mode](../../linux/sql-server-linux-restore-master-database-in-single-user-mode.md).
22+
2123
Starting [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode enables any member of the computer's local Administrators group to connect to the instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] as a member of the **sysadmin** fixed server role. For more information, see [Connect to SQL Server when system administrators are locked out](connect-to-sql-server-when-system-administrators-are-locked-out.md).
2224

2325
When you start an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode, note the following:
2426

2527
- Only one user can connect to the server.
2628

27-
- The CHECKPOINT process is not executed. By default, it is executed automatically at startup.
29+
- The CHECKPOINT process isn't executed. By default, it is executed automatically at startup.
2830

2931
> [!NOTE]
3032
> Stop the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] Agent service before connecting to an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode; otherwise, the [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] Agent service uses the connection, thereby blocking it.
@@ -36,10 +38,10 @@ When you use the `-m` option with `SQLCMD` or [!INCLUDE[ssManStudio](../../inclu
3638
> [!NOTE]
3739
> On Linux, `SQLCMD` must be capitalized as shown.
3840
39-
For example, `-m"SQLCMD"` limits connections to a single connection and that connection must identify itself as the **sqlcmd** client program. Use this option when you are starting [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode and an unknown client application is taking the only available connection. To connect through the Query Editor in [!INCLUDE[ssManStudio](../../includes/ssmanstudio-md.md)], use `-m"Microsoft SQL Server Management Studio - Query"`.
41+
For example, `-m"SQLCMD"` limits connections to a single connection and that connection must identify itself as the **sqlcmd** client program. Use this option when you're starting [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] in single-user mode and an unknown client application is taking the only available connection. To connect through the Query Editor in [!INCLUDE[ssManStudio](../../includes/ssmanstudio-md.md)], use `-m"Microsoft SQL Server Management Studio - Query"`.
4042

4143
> [!IMPORTANT]
42-
> Do not use this option as a security feature. The client application provides the client application name, and can provide a false name as part of the connection string.
44+
> Don't use this option as a security feature. The client application provides the client application name, and can provide a false name as part of the connection string.
4345
4446
The following example starts the SQL Server instance in single-user mode and only allows connection through the SQL Server Management Studio Query Editor.
4547

@@ -71,6 +73,7 @@ To get around the problem use the following procedure:
7173

7274
## See also
7375

76+
- [Restore the master database on Linux in single-user mode](../../linux/sql-server-linux-restore-master-database-in-single-user-mode.md)
7477
- [Start, Stop, or Pause the SQL Server Agent Service](../../ssms/agent/start-stop-or-pause-the-sql-server-agent-service.md)
7578
- [Diagnostic Connection for Database Administrators](../../database-engine/configure-windows/diagnostic-connection-for-database-administrators.md)
7679
- [sqlcmd Utility](../../tools/sqlcmd-utility.md)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
title: "Restore master database on SQL Server in single-user mode on Linux"
3+
description: "Learn how to restore the master database using single-user mode in SQL Server on Linux."
4+
author: rwestMSFT
5+
ms.author: randolphwest
6+
ms.date: 08/09/2022
7+
ms.prod: sql
8+
ms.prod_service: high-availability
9+
ms.technology: configuration
10+
ms.topic: how-to
11+
helpviewer_keywords:
12+
- "starting SQL Server on Linux, single-user mode"
13+
- "single-user mode [SQL Server] Linux"
14+
ms.custom: template-how-to
15+
---
16+
# Restore the master database on Linux in single-user mode
17+
18+
[!INCLUDE [appliesto-ss-xxxx-xxxx-xxx-md-linuxonly](../includes/appliesto-ss-xxxx-xxxx-xxx-md-linuxonly.md)]
19+
20+
Under certain circumstances, you may need to restore the `master` database on an instance of [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] in single-user mode on Linux. Scenarios include migrating to a new instance, or recovering from inconsistencies.
21+
22+
> [!NOTE]
23+
> [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] will automatically shut down after the restore is complete. This behavior is by design.
24+
25+
Restoring the `master` database requires starting [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] in single-user mode, by using the **startup option** `-m` from the command line.
26+
27+
For starting a [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] instance in single-user mode on Windows, see [Start SQL Server in single-user mode](../database-engine/configure-windows/start-sql-server-in-single-user-mode.md).
28+
29+
## Prerequisites
30+
31+
Starting [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] in single-user mode enables any member of the local administrator group to connect to [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] as a member of the **sysadmin** fixed server role. For more information, see [Connect to SQL Server when system administrators are locked out](../database-engine/configure-windows/connect-to-sql-server-when-system-administrators-are-locked-out.md).
32+
33+
When you start an instance of [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] in single-user mode, note the following:
34+
35+
- Only one user can connect to the server.
36+
37+
- The CHECKPOINT process isn't executed. By default, it is executed automatically at startup.
38+
39+
## Stop the SQL Server service
40+
41+
1. The following command stops the [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] instance if it's currently running:
42+
43+
```bash
44+
systemctl stop mssql-server
45+
```
46+
47+
## Change current user to `mssql`
48+
49+
1. [!INCLUDE [ssnoversion-md](../includes/ssnoversion-md.md)] on Linux runs under the `mssql` user, so you'll need to switch to this user first. You'll be prompted for the `root` password when running this command.
50+
51+
```bash
52+
su mssql
53+
```
54+
55+
## Start SQL Server in single-user mode
56+
57+
1. When you use the `-m` option with `SQLCMD`, you can limit the connections to a specified client application (`SQLCMD` must be capitalized as shown):
58+
59+
```bash
60+
/opt/mssql/bin/sqlservr -m"SQLCMD"
61+
```
62+
63+
In the previous example, `-m"SQLCMD"` limits connections to a single connection and that connection must identify itself as the **sqlcmd** client program. Use this option when you're starting [!INCLUDE[ssNoVersion](../includes/ssnoversion-md.md)] in single-user mode to restore a `master` database.
64+
65+
1. When SQL Server starts up, it generates several log entries. You can confirm that it is running in single-user mode by looking for the following lines in the output:
66+
67+
```output
68+
[...]
69+
2022-05-24 04:26:27.24 Server Command Line Startup Parameters:
70+
-m "SQLCMD"
71+
[...]
72+
2022-05-24 04:26:28.20 spid8s Warning ******************
73+
2022-05-24 04:26:28.21 spid8s SQL Server started in single-user mode. This an informational message only. No user action is required.
74+
```
75+
76+
## Connect to the SQL Server instance
77+
78+
1. Use **sqlcmd** to connect to the SQL Server instance:
79+
80+
```bash
81+
/opt/mssql-tools/bin/sqlcmd -S <ServerName> -U sa -P <StrongPassword>
82+
```
83+
84+
In the previous example, `<ServerName>` is the name of the host running SQL Server if you're connecting remotely. If you're connecting directly on the host where SQL Server is running, you can skip this parameter, or use `localhost`. `<StringPassword>` is the password for the **SA** account.
85+
86+
## Restore the `master` database
87+
88+
1. Run the following commands inside **sqlcmd**. Remember that **sqlcmd** expects `GO` at the end of the script to execute it.
89+
90+
```sql
91+
use [master];
92+
RESTORE DATABASE [master] FROM DISK = N'/var/opt/mssql/data/master.bak' WITH FILE=1,
93+
MOVE N'master' to N'/var/opt/mssql/data/master.mdf',
94+
MOVE N'mastlog' to N'/var/opt/mssql/data/mastlog.ldf', NOUNLOAD, REPLACE, STATS=5;
95+
GO
96+
```
97+
98+
In the previous example, the path to the `master` database backup file is `/var/opt/mssql/data/master.bak`. You'll need to replace this value with the correct path to your `master` database backup file.
99+
100+
2. You should see output similar to the following example, if the restore is successful.
101+
102+
```output
103+
Processed 456 pages for database 'master', file 'master' on file 1.
104+
Processed 5 pages for database 'master', file 'mastlog' on file 1.
105+
The master database has been successfully restored. Shutting down SQL Server.
106+
SQL Server is terminating this process.
107+
```
108+
109+
## Restart the SQL Server service
110+
111+
1. To restart SQL Server, run the following command.
112+
113+
```bash
114+
systemctl start mssql-server
115+
```
116+
117+
## Remarks
118+
119+
When you restore a `master` database backup, any existing user databases that were added to the instance after the backup was taken, won't be visible after restoring `master`. The files should still exist on the storage layer, so you'll need to manually reattach those user database files to bring those databases online. See [Attach a Database](../relational-databases/databases/attach-a-database.md) for more information.
120+
121+
## Next steps
122+
123+
- [Troubleshoot SQL Server on Linux](sql-server-linux-troubleshooting-guide.md)
124+
- [sqlcmd Utility](../tools/sqlcmd-utility.md)
125+
- [Start, stop, and restart SQL Server services on Linux](sql-server-linux-start-stop-restart-sql-server-services.md)

docs/linux/sql-server-linux-start-in-single-user-mode.md

Lines changed: 0 additions & 86 deletions
This file was deleted.

docs/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10791,6 +10791,8 @@ items:
1079110791
items:
1079210792
- name: Troubleshoot
1079310793
href: linux/sql-server-linux-troubleshooting-guide.md
10794+
- name: Restore the master database
10795+
href: linux/sql-server-linux-restore-master-database-in-single-user-mode.md
1079410796
- name: How to License >>
1079510797
href: https://www.microsoft.com/sql-server/sql-server-2017-pricing
1079610798
- name: DBA Stack Exchange >>

0 commit comments

Comments
 (0)