SQL Server Mirroring Starter

The past few days, I have been playing around with mirroring on SQL Server. Even though there is a GUI to guide you through setup, it isn’t that easy until you understand a few things. I won’t go into details since there are tons of great resources to help setting up mirroring, I will just point you towards some of those resources.

  • The step-by-step guide – This has most of the info you need for mirroring, but it is way too long. Still a great article though.
  • Servers should be in a domain – The first time I tried this, I tried it without a domain controller and DNS. It was a pain. Adding the servers to an AD domain makes it a ton easier. Also, most everyone with SQL Server already has an AD domain, so this is a no brainer.
  • SID’s are a pain – In a previous post, I talked about the orphaned user problem that backup restores have. Well, mirroring has the same problem, but a different solution. Since the witness server controls failover, you can’t rely on a script to fix the user mapping when something fails over. Instead you have to create a login with the same SID on both the principal and partner servers. This article talks more about SIDs and the MSDN site has info on CREATE LOGIN syntax.

Once mirroring has been setup correctly once, the next time is a lot easier. I would highly recommend creating deployment scripts that can set up your entire DB environment because SQL Server setup just has too many steps that are easy to forget or miss.

SQL Server 2005: Backup, Restore, and Orphaned Users

Backing up a SQL Server database and restoring it to another server is a common operation done for reporting, testing, or development. When restoring on a different machine, a common problem is the login principals don’t actually correlate to the restored database users. This is because they are linked by an SID. This SID isn’t the user name, its some hash value, probably a GUID. The problem is fully described in this article.

Fixing the problem is really simple. Just run the following SQL and substitute and with the appropriate values. I added these lines to all my Powershell scripts that are used to restore a database and it works like a charm.

USE <database_name>;
GO
sp_change_users_login @Action='update_one', @UserNamePattern='<database_user>',  @LoginName='<login_name>';
GO