單用戶模式啟動SQL Server實例總結


 

SQL Server的數據庫維護過程中,有時候在一些特殊情況下需要在單用戶模式下啟動SQL Server實例。 下面總結一下單用戶模式啟動SQL Server的幾種方式:

 

1:命令模式(sqlservr.exe)啟動

 

首先在命令窗口中切換到Binn目錄(這個要視SQL Server實際安裝路徑情況而定,另外,多實例情況下,必須切換到對應路徑),如果你對sqlservr.exe命令不熟悉,可以查看相關幫助信息。如下所示:

 

C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn>sqlservr.exe /?
usage: sqlservr
        [-a<L2 buffer pool directory>,<size in GB>]       (adding an L2 buffer pool file)
        [-c] (not as a service)
        [-d file] (alternative master data file)
        [-l file] (alternative master log file)
        [-e file] (alternate errorlog file)
        [-f] (minimal configuration mode)
        [-m] (single user admin mode)
        [-g number] (stack MB to reserve)
        [-k <decimal number>] (checkpoint speed in MB/sec)
        [-n] (do not use event logging)
        [-s name] (alternate registry key name)
        [-T <number>] (trace flag turned on at startup)
        [-x] (no statistics tracking)
        [-y number] (stack dump on this error)
        [-B] (breakpoint on error (used with -y))
        [-K] (force regeneration of service master key (if exists))
        [-v] (list version information)
 
See documentation for details.
2018-04-06 11:28:00.52             SQL Server shutdown has been initiated

 

 

sqlservr.ex啟動時,當前環境有多實例,而你有沒有指定參數-s,那么就會提示類似如下信息, 需要你指定-s參數的SQL Server服務名稱。

 

 

C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn>sqlservr.ex

e -c -m

2018-04-06 11:40:54.15 Server      Multiple instances of SQL server are installe

d on this computer. Renter the command, specifying the -s parameter with the nam

e of the instance that you want to start.

2018-04-06 11:40:54.16 Server      SQL Server shutdown has been initiated

 

sqlservr.exe -c -m  -s{instancename}

 

 

 

sqlservr.ex啟動時,如果SQL Server服務本身還在運行,就會報Operating system error = 32(The process cannot access the file because it is being used by another process.).

 

 

C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn>sqlservr.ex

e -c -m -sMSSQLSERVER

2018-04-06 11:41:59.01 Server      Error: 17058, Severity: 16, State: 1.

2018-04-06 11:41:59.01 Server      initerrlog: Could not open error log file 'C:

\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Log\ERRORLOG'. Ope

rating system error = 32(The process cannot access the file because it is being

used by another process.).

2018-04-06 11:41:59.32 Server      Error: 17058, Severity: 16, State: 1.

2018-04-06 11:41:59.32 Server      initerrlog: Could not open error log file 'C:

\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Log\ERRORLOG'. Ope

rating system error = 32(The process cannot access the file because it is being

used by another process.).

2018-04-06 11:42:02.04 Server      SQL Server shutdown has been initiated

 

 

 

如果在sqlservr.exe當中退出單用戶模式,直接使用CTRL+C CTRL + Break,如下所示:

 

clip_image001

 

 

 

 

2:命令模式(net star)啟動

 

 

C:\Users>net stop mssqlserver

The following services are dependent on the SQL Server (MSSQLSERVER) service.

Stopping the SQL Server (MSSQLSERVER) service will also stop these services.

 

   SQL Server Agent (MSSQLSERVER)

 

Do you want to continue this operation? (Y/N) [N]: y

The SQL Server Agent (MSSQLSERVER) service is stopping.

The SQL Server Agent (MSSQLSERVER) service was stopped successfully.

 

The SQL Server (MSSQLSERVER) service is stopping.

The SQL Server (MSSQLSERVER) service was stopped successfully.

 

 

C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn>net start mssqlserver /m

The SQL Server (MSSQLSERVER) service is starting.

The SQL Server (MSSQLSERVER) service was started successfully.

 

clip_image002

 

 

3SQL Server配置管理器啟動

 

 

SQL Server配置管理器中,找到對應實例,右鍵單擊屬性,在啟動參數里面增加參數-m,然后重啟即可。

 

clip_image003

 

 

在單用戶模式下啟動SQL Server實例時,請注意下列事項:

 

 

·         只有一個用戶可以連接到服務器。

 

·         不執行CHECKPOINT 進程。 默認情況下,啟動時自動執行此進程。

 

 

                                                                                                                                                                                                                 在單用戶模式下啟動 SQL Server 可使計算機本地 Administrators 組的任何成員作為 sysadmin 固定服務器角色的成員連接到 SQL Server 實例。 有關詳細信息,請參閱在系統管理員被鎖定時連接到 SQL Server

 

 

在單用戶模式下, 只有一個用戶可以連接到服務器,那么這樣問題就來了,很有可能當你需要登錄的時候,這個唯一的的用戶已經被其它用戶捷足先登了。此時你卻被拒之門外,是否相當抓狂。此時你可能遇到下面錯誤

 

 

 

C:\Users>sqlcmd

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login failed for user '

xxxx'. Reason: Server is in single user mode. Only one administrator c

an connect at this time..

 

SSMS客戶端一般遇到下面這樣的錯誤信息:

 

 

Login failed for user 'xxxx'. Reason: Server is in single user mode. Only one administrator can connect at this time. (Microsoft SQL Server, Error: 18461)

 

 

錯誤日志或命令里面輸出的日志,你會看到類似如下信息:

 

 

2018-04-06 12:21:14.85 Logon       Error: 18461, Severity: 14, State: 1.

2018-04-06 12:21:14.85 Logon       Login failed for user 'xxx'. Reason: Server is in single user mode. Only one administrator can connect at this time. [CLIENT: 192.168.xxx.xxx]

 

 

在這種情況下,怎么辦呢? 難道要拼速度? 當然不是,你需要從下面這些方面注意:

 

 

在單用戶模式下連接到SQL Server實例之前,停止SQL Server Agent 服務;否則 SQL Server Agent 服務將使用該連接,從而使其阻塞。

 

 

在單用戶模式下啟動SQL Server實例時,SQL Server Management Studio 可以連接到 SQL Server 但是Management Studio中的對象資源管理器可能會失敗,因為在某些操作中它需要使用多個連接。 若要在單用戶模式下管理 SQL Server,可以執行 Transact-SQL 語句(僅通過 Management Studio 中的查詢編輯器連接)或者使用 sqlcmd 實用工具。

 

當您將 -m 選項與 sqlcmd  Management Studio 結合使用時,可以將連接限制為指定的客戶端應用程序。 例如,-m"sqlcmd" 將連接限制為單個連接並且該連接必須將自身標識為 sqlcmd 客戶端程序。 當您正在單用戶模式下啟動 SQL Server 並且未知的客戶端應用程序正在占用這個唯一的可用連接時,使用此選項。 若要通過 Management Studio 中的查詢編輯器進行連接,請使用 -m"Microsoft SQL Server Management Studio - Query"

 

 

如下所示,如果你指定了單用戶只能以SQLCMD連接,那么此時,其它通過SSMS等其它方式連接數據庫都會報上面錯誤,其它通過程序連接過來的連接就不會搶占這個連接了。

 

C:\Windows\system32>net stop mssqlserver

The SQL Server (MSSQLSERVER) service is stopping.

The SQL Server (MSSQLSERVER) service was stopped successfully.

 

 

C:\Windows\system32>net start mssqlserver /m"SQLCMD"

The SQL Server (MSSQLSERVER) service is starting.

The SQL Server (MSSQLSERVER) service was started successfully.

 

 

C:\Windows\system32>

 

clip_image004

 

 

如果你指定參數/m"Microsoft SQL Server Management Studio - Query" 那么就會阻止像應用程序或SQLCMD登錄,如下所:

 

 

C:\Windows\system32>net stop mssqlserver

The SQL Server (MSSQLSERVER) service is stopping.

The SQL Server (MSSQLSERVER) service was stopped successfully.

 

 

C:\Windows\system32>net start mssqlserver /m"Microsoft SQL Server Management Studio - Query"

The SQL Server (MSSQLSERVER) service is starting.

The SQL Server (MSSQLSERVER) service was started successfully.

 

 

 

C:\Users>sqlcmd

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login failed for user '

xxx'. Reason: Server is in single user mode. Only one administrator c

an connect at this time..

 

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM