3

I have a strange situation wherein users need permission on log shipped DB on secondary server but not on primary. The problem is, the login has access on many other DBs on primary server apart from log shipped DB and that needs to be maintained. My environment is SQL Server 2012 EE.

Is it possible to achieve this?

As a workaround what I have tried is granting temporary access on my primary DB just before the backup log-shipping starts and once the backup completes I have added a step to remove it. But the issue with this workaround is, the users can still access the primary log shipped DB when I try to grant them access just before the log backup. Any help would greatly be appreciated.

1
  • I have already tried that but it does not work because CREATE USER...WITHOUT LOGIN creates a SID that is over 16 bytes. CREATE LOGIN requires a 16 byte GUID so the longer SID value is not compatible with the SID parameter of CREATE LOGIN.
    – SQLPRODDBA
    Oct 14, 2015 at 6:58

1 Answer 1

1

On Primary

  • Create a new Login [testLogin] and a new User [testUser] with the requiered rights and roles:

    USE [master]
    GO
    CREATE LOGIN [testLogin] WITH PASSWORD=N'test123'
        , DEFAULT_DATABASE=[Test], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
    GO
    USE [Test]
    GO
    CREATE USER [testUser] FOR LOGIN [testLogin];
    GO
    USE [Test]
    GO
    ALTER ROLE [db_datareader] ADD MEMBER [testUser];
    GO
    

These login and user must not exist and be different from your current login and user.

  • Once [testUser] is available on Secondary, remove [testLogin] from Primary:

    USE [master]
    GO
    DROP LOGIN [testLogin];
    

Other option on Primary

  • Remove the orignal Login

You must plan your update if you have to remove login for a short time and make sure you disconnect them first.

  • Create a new Login with the same name

    CREATE LOGIN...
    
  • Link Orphan users to the newly created Login for all DBs except the log shipped DB:

    USE [DB1]
    GO
    sp_change_users_login @Action='update_one'
        , @UserNamePattern='testUser'
        , @LoginName='testLogin';
    

On Secondary [testLogin]:

  • Create a new login [testLogin]:

    USE [master]
    GO
    CREATE LOGIN [testLogin] WITH PASSWORD=N'test123'
         , DEFAULT_DATABASE=[Test], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
    GO
    
  • Link the orphan user [testUser] (shipped from Primary) to the new login [testLogin]:

    USE [Test]
    GO
    sp_change_users_login @Action='update_one'
        , @UserNamePattern='testUser'
        , @LoginName='testLogin';
    
6
  • As said, this works only when users don't have access to any other DBs on primary apart from log-shipped DBs. In my case this won't work as users have access on other DBs as well apart from log-shipped DBs. If I remove the login it will remove access for all DBs on primary. If I don't drop login and only drop user it will remove access from secondary DB as well.
    – SQLPRODDBA
    Oct 14, 2015 at 8:06
  • you missed this line: If [testLogin] is still needeed on Primary, don't remove it... Have you tried it before saying it does not work and can you explain which part does not work? Oct 14, 2015 at 8:07
  • Yes but if I keep it as it is it will give access to my primary log-shipped DB which I don't want. Also if I drop only user and not login the subsequent log backups will pick that command and it will drop from secondary as well.
    – SQLPRODDBA
    Oct 14, 2015 at 8:12
  • It would be a lot easier to create a new login or AD group Oct 14, 2015 at 8:13
  • I was thinking in that direction. i.e. to have separate login for log-shipped DBs.
    – SQLPRODDBA
    Oct 14, 2015 at 8:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.