title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | helpviewer_keywords | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Restore master Database on SQL Server in Single-User Mode on Linux |
Learn how to restore the master database using single-user mode in SQL Server on Linux. |
rwestMSFT |
randolphwest |
11/18/2024 |
sql |
configuration |
how-to |
|
|
[!INCLUDE SQL Server - Linux]
Under certain circumstances, you might need to restore the master
database on an instance of [!INCLUDE ssNoVersion] in single-user mode on Linux. Scenarios include migrating to a new instance, or recovering from inconsistencies.
Note
[!INCLUDE ssnoversion-md] will automatically shut down after the restore is complete. This behavior is by design.
To restore the master
database, you must start [!INCLUDE ssNoVersion] in single-user mode, by using the startup option -m
from the command line.
For starting a [!INCLUDE ssNoVersion] instance in single-user mode on Windows, see Single-user mode for SQL Server.
Starting [!INCLUDE ssNoVersion] in single-user mode enables any member of the local administrator group to connect to [!INCLUDE ssNoVersion] as a member of the sysadmin fixed server role. For more information, see Connect to SQL Server when system administrators are locked out.
When you start an instance of [!INCLUDE ssNoVersion] in single-user mode:
- Only one user can connect to the server.
- The
CHECKPOINT
process isn't executed. By default, it runs automatically at startup.
-
The following command stops the [!INCLUDE ssnoversion-md] instance if it's currently running:
systemctl stop mssql-server
-
[!INCLUDE ssnoversion-md] on Linux runs under the
mssql
user, so you need to switch to this user first. You're prompted for theroot
password when running this command.su mssql
-
When you use the
-m
option withSQLCMD
, you can limit the connections to a specified client application (SQLCMD
must be capitalized as shown):/opt/mssql/bin/sqlservr -m"SQLCMD"
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] in single-user mode to restore amaster
database. -
When SQL Server starts up, it generates several log entries. You can confirm that it's running in single-user mode by looking for the following lines in the output:
[...] 2022-05-24 04:26:27.24 Server Command Line Startup Parameters: -m "SQLCMD" [...] 2022-05-24 04:26:28.20 spid8s Warning ****************** 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.
-
Use sqlcmd to connect to the SQL Server instance. After finishing the steps described in the Start SQL Server in single-user mode section, you can see that [!INCLUDE ssNoVersion] is running in interactive mode. Thus, you need to open a new terminal session to launch sqlcmd as follows.
/opt/mssql-tools/bin/sqlcmd -S <ServerName> -U sa -P <password>
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 uselocalhost
.<StringPassword>
is the password for the SA account.
-
Run the following commands inside sqlcmd. Remember that sqlcmd expects
GO
at the end of the script to execute it.USE [master]; GO RESTORE DATABASE [master] FROM DISK = N'/var/opt/mssql/data/master.bak' WITH FILE = 1, MOVE N'master' TO N'/var/opt/mssql/data/master.mdf', MOVE N'mastlog' TO N'/var/opt/mssql/data/mastlog.ldf', NOUNLOAD, REPLACE, STATS = 5; GO
In the previous example, the path to the
master
database backup file is/var/opt/mssql/data/master.bak
. You must replace this value with the correct path to yourmaster
database backup file. -
You should see output similar to the following example, if the restore is successful.
Processed 456 pages for database 'master', file 'master' on file 1. Processed 5 pages for database 'master', file 'mastlog' on file 1. The master database has been successfully restored. Shutting down SQL Server. SQL Server is terminating this process.
-
To restart SQL Server, run the following command.
systemctl start mssql-server
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 need to manually reattach those user database files to bring those databases online. For more information, see Attach a Database.