title: How to fix "The master key file does not exist or has invalid format." in SQL Server 2017
body: <p>The environment I am using is a VM hosted on Azure.</p>
  
  <p>I recently created a new instance of SQL Server on that VM called TESTINSTANCE (in addition to the existing default one). I used a separate service account.</p>
  
  <p>Subsequently I need to restore a database backup to the new instance.
  In the source a master key and certificate had been created.</p>
  
  <p>So before restoring, I think need to recreate that key in my destination instance (TESTINSTANCE). </p>
  
  <p>When I trey the below however: </p>
  
  <pre><code>-- Create a database master key on the destination instance of SQL Server.   
  USE master;  
  GO  
  
  CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'value_a';  
  GO
  
  OPEN MASTER KEY DECRYPTION BY PASSWORD = 'value_a';   
  go
  
  RESTORE MASTER KEY   
      FROM FILE = 'C:\folder_\masterdb_masterkey'   
      DECRYPTION BY PASSWORD = 'value_b'   
      ENCRYPTION BY PASSWORD = 'value_a';  
  GO  
  </code></pre>
  
  <p>I am getting is the following error message:</p>
  
  <blockquote>
    <p>The master key file does not exist or has invalid format.</p>
  </blockquote>
  
  <p>I believe this may be caused by the permissions of the service account that has been created during my installation of TESTINSTANCE. </p>
  
  <p>However when I check the Windows Privileges and Rights section of the page below, my understanding is that the TESTINSTANCE service account should get the same permissions as the default one.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-windows-service-accounts-and-permissions?view=sql-server-2017" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/configure-windows-service-accounts-and-permissions?view=sql-server-2017</a></p>
  
  <p>So I'm not sure why it can't access that location in the C drive.
  To confirm, the file does exist in 'C:\folder_\masterdb_masterkey'</p>