Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jocampo
Starting Member
48 Posts |
Posted - 2006-11-08 : 21:30:31
|
How to automate the failover process in SQL 2005 Mirroring? How do you design a job to run after the failover is complete so the logins can be fixed? Can a job be started automatically right after the failover of the database has completed? Even if the logins are transferred they will not be mapped correctly to the database after the failover from the Principal to the Mirror server. |
|
mr_mist
Grunnio
1870 Posts |
Posted - 2006-11-09 : 04:18:44
|
The failover process is automated if there is a witness server in place.The logins can auto-map if you transfer them across with the same SID values. Here's some code that should do that (from some website, probably MS tech) ----- Begin Script, Create sp_help_revlogin procedure -----USE masterGOIF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimalGOCREATE PROCEDURE sp_hexadecimal @binvalue varbinary(256), @hexvalue varchar(256) OUTPUTASDECLARE @charvalue varchar(256)DECLARE @i intDECLARE @length intDECLARE @hexstring char(16)SELECT @charvalue = '0x'SELECT @i = 1SELECT @length = DATALENGTH (@binvalue)SELECT @hexstring = '0123456789ABCDEF' WHILE (@i <= @length) BEGIN DECLARE @tempint int DECLARE @firstint int DECLARE @secondint int SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1)) SELECT @firstint = FLOOR(@tempint/16) SELECT @secondint = @tempint - (@firstint*16) SELECT @charvalue = @charvalue + SUBSTRING(@hexstring, @firstint+1, 1) + SUBSTRING(@hexstring, @secondint+1, 1) SELECT @i = @i + 1ENDSELECT @hexvalue = @charvalueGOIF OBJECT_ID ('sp_help_revlogin') IS NOT NULL DROP PROCEDURE sp_help_revlogin GOCREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL ASDECLARE @name sysnameDECLARE @xstatus intDECLARE @binpwd varbinary (256)DECLARE @txtpwd sysnameDECLARE @tmpstr varchar (256)DECLARE @SID_varbinary varbinary(85)DECLARE @SID_string varchar(256)IF (@login_name IS NULL) DECLARE login_curs CURSOR FOR SELECT sid, name, xstatus, password FROM master..sysxlogins WHERE srvid IS NULL AND name <> 'sa'ELSE DECLARE login_curs CURSOR FOR SELECT sid, name, xstatus, password FROM master..sysxlogins WHERE srvid IS NULL AND name = @login_nameOPEN login_curs FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwdIF (@@fetch_status = -1)BEGIN PRINT 'No login(s) found.' CLOSE login_curs DEALLOCATE login_curs RETURN -1ENDSET @tmpstr = '/* sp_help_revlogin script ' PRINT @tmpstrSET @tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'PRINT @tmpstrPRINT ''PRINT 'DECLARE @pwd sysname'WHILE (@@fetch_status <> -1)BEGIN IF (@@fetch_status <> -2) BEGIN PRINT '' SET @tmpstr = '-- Login: ' + @name PRINT @tmpstr IF (@xstatus & 4) = 4 BEGIN -- NT authenticated account/group IF (@xstatus & 1) = 1 BEGIN -- NT login is denied access SET @tmpstr = 'EXEC master..sp_denylogin ''' + @name + '''' PRINT @tmpstr END ELSE BEGIN -- NT login has access SET @tmpstr = 'EXEC master..sp_grantlogin ''' + @name + '''' PRINT @tmpstr END END ELSE BEGIN -- SQL Server authentication IF (@binpwd IS NOT NULL) BEGIN -- Non-null password EXEC sp_hexadecimal @binpwd, @txtpwd OUT IF (@xstatus & 2048) = 2048 SET @tmpstr = 'SET @pwd = CONVERT (varchar(256), ' + @txtpwd + ')' ELSE SET @tmpstr = 'SET @pwd = CONVERT (varbinary(256), ' + @txtpwd + ')' PRINT @tmpstr EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT SET @tmpstr = 'EXEC master..sp_addlogin ''' + @name + ''', @pwd, @sid = ' + @SID_string + ', @encryptopt = ' END ELSE BEGIN -- Null password EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT SET @tmpstr = 'EXEC master..sp_addlogin ''' + @name + ''', NULL, @sid = ' + @SID_string + ', @encryptopt = ' END IF (@xstatus & 2048) = 2048 -- login upgraded from 6.5 SET @tmpstr = @tmpstr + '''skip_encryption_old''' ELSE SET @tmpstr = @tmpstr + '''skip_encryption''' PRINT @tmpstr END END FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwd ENDCLOSE login_curs DEALLOCATE login_curs RETURN 0GO ----- End Script ----- That may be the SQL 2000 version though. -------Moo. :) |
 |
|
jocampo
Starting Member
48 Posts |
Posted - 2006-11-09 : 11:42:29
|
No, the failover is not automatically with witness. That's what Microsoft says. If you're using an app. against that database, you're not able to login because there are no credentials in the new Principal. Got my point? ... Could be automatically, but that's at database level ... and DBA level. In real world, you need to include the App. side. If this affects your application, then you got no auto failover. You need to manually fix it; so, it's manual, no auto.Thanks for reply, but i used Microsoft Technet scripts and they do not work for me. What i need most is the USER MAPPINGS to the database, in auto mode. I mean...the SQL login, but with a map already set up. |
 |
|
jocampo
Starting Member
48 Posts |
Posted - 2006-11-10 : 20:50:10
|
Ok ... got it! ... and i will post this because there are a lot of persons with the same problem. I can not believe nobody knows about this ...Some times, a step by step explanation helps much more than just point to a simple script or link ...1. On the server which will be Principal, create the Login (which the app. will use to connect) at Server level WITH NO MAPPING to the database. Put master database at default db instead.2. Use SSIS and execute the required package to copy Logins from one server to another. If package turns green, it works. You should also be able to see the Login in the second server.3. Now, on 1st server again, set database mapping for SQL login. Set the required permissions to database also (data reader, data writer, etc)3. Restore database on 2nd server (future mirror) in recovery mode.4. Establish the Mirror5. Test the failover: check the 2nd server (now the Principal) and you should be able to see the SQL login. Change default database from master to the desired database (now you can, because you're in the principal)6. Make another manual failover. Since now ... SQL server login its already there and mapping exist with the required permissions also.Jose Campo |
 |
|
mr_mist
Grunnio
1870 Posts |
Posted - 2006-11-14 : 05:15:23
|
quote: No, the failover is not automatically with witness. That's what Microsoft says. If you're using an app. against that database, you're not able to login because there are no credentials in the new Principal. Got my point?
I'm not really sure if you were trying intentionally to be condescending or that just how it came across, but yes, I "got your point" from the beginning. There's no reason why using the scripts I gave to create a login with the same SID would be any different to using the transfer logins wizard. Anyway I'm pleased that you solved your issue.-------Moo. :) |
 |
|
jocampo
Starting Member
48 Posts |
Posted - 2006-11-14 : 11:37:04
|
condescending ? what you mean with that?...to be honest, your script did not work for me (nothing personal) and after a few tries I was able to solve the issue using SSIS. Hope this help new people who find the same problem.quote: Originally posted by mr_mist
quote: No, the failover is not automatically with witness. That's what Microsoft says. If you're using an app. against that database, you're not able to login because there are no credentials in the new Principal. Got my point?
I'm not really sure if you were trying intentionally to be condescending or that just how it came across, but yes, I "got your point" from the beginning. There's no reason why using the scripts I gave to create a login with the same SID would be any different to using the transfer logins wizard. Anyway I'm pleased that you solved your issue.-------Moo. :)
|
 |
|
|
|
|
|
|