SQL Server 2008 R2 – User Mapping Dropped After Switching from Principal to Mirror Server

mirroringsql-server-2008-r2

I have principal server:A and mirror server:B with DB Name: "DBTest"
On the server A there is a username (SQL auth.) call: user1 and is mapped to "DBTest".
the Same is on Server B, same username and same DB Name (but is in recovery mode due to mirroring).
After Fail-over in Principle Server, the Witness switch the DB to the mirror server and everything is fine. except the user: user1 on the server B is not mapped to the "DBTest" on server B.
i should every time reassign manually the user1 to the "DBTest". The same i should do when i switch from server B again to server A.

This issue blocks the systems and applications which are connecting with the DB, because the user1 is not mapped automatically to the DB name after fail-over.
please help!

Best Answer

The issue you're running into most likely has to do with your users becoming orphaned on failover. Even though you're using the same logins, the SIDs are different, and don't match up with the users in the database. Basically, you need to recreate the user exactly as it is on server A.

You need to do a CREATE LOGIN with SID = xxx that will create an exact copy of the login on server A. Syntax can be found here. I use sp_help_revlogin to do this; see this knowledgebase article for more information. This will give you a CREATE LOGIN script with the same SID and password to execute on server B.

If you move to SQL Server 2012, you'll have the option of using contained databases. Contained databases can store their own logins, meaning the instance doesn't need to have its own set and preventing orphaned users. These are especially good for databases that get moved around a lot and may have users changing somewhat frequently.