CREATE FILE encountered operating system error 5(Access is denied.)


    這篇博文主要演示”CREATE FILE encountered operating system error 5(Access is denied.)“錯誤如出現的原因(當然只是導致這個錯誤出現的一種場景而已)和如何解決這個問題以及一些不解的迷惑。

 

實驗環境:

        操作系統版本: Windows Server 2012 SP2

        數據庫的版本:Microsoft SQL Server 2014 - 12.0.2000.8

如下所示,SQL Server Database Services 服務的啟動賬號為NT Service\MSSQLSERVER, 我們先設置一下數據庫的默認路徑為E:\SQL_DATA

clipboard

此時查看E:\SQL_DATA 目錄的權限(被屏蔽的是一域賬號)。此時我們創建數據庫Test完全沒有問題。我們先刪除數據庫Test。

clipboard[1]

現在我們刪除用戶GETTESTNT55\Users這個賬號,在目錄E:\SQL_DATA的屬性(Properties)->安全(Security)-> 編輯(Edit)下直接刪除會遇到下面錯誤。

clipboard[2]

我們從E盤的根目錄,右鍵屬性選擇Security 刪除這個用戶,然后我創建新的數據庫時就會遇到CREATE FILE encountered operating system error 5(Access is denied.)錯誤。

clipboard[3]

那么如何解決呢? 我們可以給當前目錄添加虛擬帳戶NT Service\MSSQLSERVER 並授予相關權限解決這個問題。具體可以參考MSDN文檔,截取部分如下所示

  1. 使用 Windows 資源管理器,導航到存儲數據庫文件的文件系統位置。 右鍵單擊文件系統文件夾,然后單擊“屬性”。

  2. 在“安全性”選項卡上,單擊“編輯”,然后單擊“添加”。

  3. 在“選擇用戶、計算機、服務帳戶或組”對話框中,單擊“位置”,在位置列表的頂部選擇您的計算機名稱,然后單擊“確定”。

  4. 在“輸入要選擇的對象名稱”框中,鍵入聯機叢書主題“配置 Windows 服務帳戶和權限”中所列的每個服務 SID 的名稱。   (對於數據庫引擎每個服務 SID,將  NT SERVICE\MSSQLSERVER  用於默認實例,或將  NT SERVICE\MSSQL$InstanceName  用於命名實例。)

  5. 單擊“檢查名稱”以驗證該條目。 驗證經常失敗,而且可能告知您找不到該名稱。 單擊“確定”時,將顯示“找到多個名稱”對話框。

  6. 現在選擇每個服務 SID(MSSQLSERVER  NT SERVICE\MSSQL$InstanceName),然后單擊“確定”。

  7. 再次單擊“確定”以返回“權限”對話框。

  8. 在“組或用戶”名稱框中,選擇每個服務 SID,然后在“<名稱> 的權限”框中,為“完全控制”選中“允許”復選框。

  9. 單擊“應用”,然后單擊“確定”兩次以退出。

具體操作過程的截圖如下所示

clipboard[4]

clipboard[5]

權限的話,至少需要”Read & Execute“、”List Folder Content“、”Read“、”Write“權限,當然如果你能給”Full Control“權限那肯定是可以。權限我們應該盡量控制最小范圍內。

clipboard[6]

此時就能解決這個錯誤,此時你刪除該用戶,然后在這個目錄添加EVERYONE用戶,授予上面的權限也能解決這個錯誤。當然在安全性上就不如給虛擬賬戶NT Service\MSSQLSERVER賬號授權。

肯定有細心的人會問,我們不添加虛擬賬戶NT Service\MSSQLSERVER或EVERYONE,我們重新添加這個用戶GETTESTNT55\Users、授予同樣的權限是否可以解決這個問題? 答案是依然會遇到這個錯誤,為什么呢? 暫時我沒有搞明白具體原因,是不是有點諷刺,知道了怎么解決,但是依然不清楚具體原因。當然涉及權限的問題確實有點復雜。如果哪位知道具體原因,請不吝賜教。

 

----------------------------------------------分割線 2015-12-15補充--------------------------------------------------

今天有空,為了搞清楚這個問題,我查了大量的資料。下面是一些來自MSDN的資料和我的一些驗證。下面我們一一梳理清楚。

 

關於SQL SERVER服務的一些屬性與配置的信息(中文與英文信息)。

服務屬性和配置

用於啟動和運行 SQL Server 的啟動帳戶可以是域用戶帳戶本地用戶帳戶托管服務帳戶虛擬帳戶內置系統帳戶。 若要啟動和運行 SQL Server 中的每項服務,這些服務都必須有一個在安裝過程中配置的啟動帳戶。

此部分介紹可配置為啟動 SQL Server 服務的帳戶、SQL Server 安裝程序使用的默認值、Per-service SID 的概念、啟動選項以及配置防火牆。


Service Properties and Configuration

    Startup accounts used to start and run SQL Server can be domain user accounts, local user accounts, managed service accounts, virtual accounts, or built-in system accounts. To start and run, each service in SQL Server must have a startup account configured during installation.

    This section describes the accounts that can be configured to start SQL Server services, the default values used by SQL Server Setup, the concept of per-service SID’s, the startup options, and configuring the firewall.

 

NT Service\MSSQLSERVER 它屬於虛擬賬戶(Virtual Account)。虛擬賬戶的概念請看下面MSDN描述(中文與英文)

虛擬帳戶

Windows Server 2008 R2 和 Windows 7 中的虛擬帳戶是“托管的本地帳戶”,此類帳戶提供以下功能以簡化服務管理。 虛擬帳戶是自動管理的,並且虛擬帳戶可以訪問域環境中的網絡。 如果在 Windows Server 2008 R2 或 Windows 7 上安裝 SQL Server 時對服務帳戶使用默認值,則將使用將實例名稱用作服務名稱的虛擬帳戶,格式為 NT SERVICE\<SERVICENAME>。 以虛擬帳戶身份運行的服務通過使用計算機帳戶的憑據(格式為 <domain_name>\<computer_name>$)訪問網絡資源。 當指定一個虛擬帳戶以啟動 SQL Server 時,應將密碼留空。 如果虛擬帳戶無法注冊服務主體名稱 (SPN),則手動注冊該 SPN。 有關手動注冊 SPN 的詳細信息,請參閱手動注冊 SPN

Virtual Accounts

Virtual accounts in Windows Server 2008 R2 and Windows 7 are managed local accounts that provide the following features to simplify service administration. The virtual account is auto-managed, and the virtual account can access the network in a domain environment. If the default value is used for the service accounts during SQL Server setup on Windows Server 2008 R2 or Windows 7, a virtual account using the instance name as the service name is used, in the format NT SERVICE\<SERVICENAME>. Services that run as virtual accounts access network resources by using the credentials of the computer account in the format <domain_name>\<computer_name>$. When specifying a virtual account to start SQL Server, leave the password blank. If the virtual account fails to register the Service Principal Name (SPN), register the SPN manually. For more information on registering a SPN manually, see Manual SPN Registration.

關於Users組的概念

A built-in group. After the initial installation of the operating system, the only member is the Authenticated Users group. When a computer joins a domain, the Domain Users group is added to the Users group on the computer. Users can perform tasks such as running applications, using local and network printers, shutting down the computer, and locking the computer. Users can install applications that only they are allowed to use if the installation program of the application supports per-user installation.

那么我們用Icacls命令來查看E:\SQL_DATA目錄的隨機訪問控制列表 (DACL),我們可以對比GETTESTNT55\Users刪除前后的權限,發現刪除該用戶組后,BUILTIN\Users對該目錄的權限沒有了。

clipboard[1]

看來SQL SERVER服務訪問相關的文件系統權限的最終映射到了這個“內置帳戶”賬戶上了(僅僅個人猜想、判斷)。所以才會出現我上面的錯誤。

在文件上面我曾經介紹過“我們不添加虛擬賬戶NT Service\MSSQLSERVER,我們重新添加這個用戶、授予同樣的權限是否可以解決這個問題? 答案是依然會遇到這個錯誤,為什么呢?” ,當時我是在E:\SQL_DATA目錄設置權限。這樣依然不行,如果我去E盤設置權限則不會出現這個錯誤(實驗驗證結果)。我對比了下兩者在E:\SQL_DATA文件上的隨機訪問控制列表 (DACL)的不同。如下所示。權限的順序有所不同(真是腦殼痛,研究到此為止),看來是某種權限控制機制導致。

clipboard[2]

clipboard[3]

參考資料:

http://www.guyellisrocks.com/2010/07/create-file-encountered-operating.html

http://stackoverflow.com/questions/11178536/create-file-encountered-operating-system-error-5failed-to-retrieve-text-for-thi

https://msdn.microsoft.com/zh-cn/library/jj219062.aspx

http://ss64.com/nt/syntax-security_groups.html

https://msdn.microsoft.com/zh-cn/library/ms143504(v=sql.120).aspx#VA_Desc


免責聲明!

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



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