Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring SQL Server Database - Master Key Not Opening

I have to make a local copy of a remote SQL Server database. I did this by using Tasks > Backup from Management Studio. I then locally restored the backup, which seems to have everything -- tables, users, symmetric key, and certificate.

When I try to perform a select that requires me to open the symmetric key and decrypt by the certificate, I get this error:

Please create a master key in the database or open the master key in the session before performing this operation.

Why am I being asked for this, and why doesn't it open automatically like it does on the remote server?

I've tried changing the master key, but without the original password, I can't do much.

like image 527
ashes999 Avatar asked Jun 16 '11 19:06

ashes999


People also ask

How do I regenerate master key in SQL Server?

If the master key cannot be opened, use the RESTORE MASTER KEY statement to restore the master key from a backup. Use the FORCE option only if the master key is irretrievable or if decryption fails. Information that is encrypted only by an irretrievable key will be lost.

How do I restore a SQL master database?

To restore the master databaseThe REPLACE option instructs SQL Server to restore the specified database even when a database of the same name already exists. The existing database, if any, is deleted. In single-user mode, we recommend that you enter the RESTORE DATABASE statement in the sqlcmd utility.

How do I get my backup master key?

To back up the database master key In SQL Server Management Studio, connect to the SQL Server instance containing the database master key you wish to back up. Choose a password that will be used to encrypt the database master key on the backup medium. This password is subject to complexity checks.


2 Answers

The problem is the SMK has changed (since the machine has changed). There's an article explaining it here. Just export and import the SMK -- bearing in mind that any encrypted data in your copied-to system will be unreadable.

MSDN articles:

  • Backing up the SMK
  • Restoring the SMK
like image 171
ashes999 Avatar answered Oct 21 '22 20:10

ashes999


Here is a good article specifically on migrating a database that includes encryption:

http://www.sql-server-performance.com/2009/migrating-databases-checklist-part3/3/

But in short, you need to know the original password in order to move it.

You can backup and restore the key (i.e. replicate as you mention) but you'll need access to the remote server, ability to create backup, or copy of backup with original passwords:

http://msdn.microsoft.com/en-us/library/ff848768.aspx

This forum conversation may also prove useful for insight:

http://www.sqlservercentral.com/Forums/Topic775644-146-1.aspx

like image 36
RThomas Avatar answered Oct 21 '22 22:10

RThomas