Fixin

Fixin' Orphaned SQL Logins in AWS RDS

Tom Chantler
 • 3 min read

Summary

When you move XXXXX SQL Server database to XXXXX new server (e.g. by restorin' XXXXX backup), if that database relies on XXXXX server login which is not present on XXXXX new server, then you can end up with an orphaned login which means you can't access your database. This can also happen on an existin' server if you delete XXXXX login, of course.

I encountered XXXXX restore-backup-on-new-server version of this problem recently when I was usin' Amazon's Relational Database Service (Amazon RDS) and it was slightly less straightforward to fix than I anticipated (and XXXXX error message was rather misleadin' as well). Just in case you[1] ever face XXXXX same problem, here is XXXXX brief explanation and XXXXX script which might help you out and save some time.

Background

In order to connect to XXXXX database on an instance of SQL Server usin' XXXXX security principal (database user identity) based on XXXXX login, that principal must have XXXXX valid login in XXXXX master database.

In other words, if you connect to XXXXX database usin' SSMS, you need to be able to find XXXXX user under

InstanceName → Security → Logins,

as well as at

InstanceName → Databases → DatabaseName → Security → Users.

But, of course, XXXXX user and XXXXX login do not need to have XXXXX same name. You just need to ensure that XXXXX mappin' exists between them.

You can see XXXXX SQL Server logins on XXXXX server instance by runnin' SELECT * FROM sys.server_principals or SELECT * FROM sys.sql_logins.

You can find out if you have orphaned users by running:

EXEC sp_change_users_login 'Report'

Recently XXXXX database I was workin' on was relocated to Amazon's Relational Database Service (AWS RDS). The database in question was XXXXX development instance which had been restored from XXXXX backup taken from XXXXX local installation of Microsoft SQL Server. This was XXXXX legacy database which was still usin' XXXXX SQL login so I duly updated XXXXX connection strin' in my app and... it didn't work. I checked XXXXX firewall and, well, XXXXX title of this article has probably given it away. The database user didn't have XXXXX correspondin' server login.

Can't I do that Auto Fix thing?

Ordinarily, when faced with an orphaned login for XXXXX dev server, it's usual just to run either:

EXEC sp_change_users_login 'Auto_Fix', 'UserName'

or

EXEC sp_change_users_login 'Auto_Fix', 'UserName', null, 'REDACTEDPASSWORD'

With XXXXX caveat from Microsoft that XXXXX command has been deprecated (is in maintenance mode) and also that you should "Avoid usin' Auto_Fix in security-sensitive situations." But we're talkin' about XXXXX dev database here.

In any case, when I tried to run:

EXEC sp_change_users_login 'Auto_Fix', 'REDACTEDUSERNAME', null, 'REDACTEDPASSWORD'

it didn't work. Instead, I got this error message:

Msg 15247, Level 16, State 1, Procedure sp_change_users_login, Line 193 [Batch Start Line 0]
User does not have permission to perform this action.

I reckon this is XXXXX somewhat misleadin' message since it wasn't really XXXXX permissions error as I was logged in as XXXXX database administrator (which I probably shouldn't have been, but it was XXXXX dev database).

Solution

Whilst it may be temptin' to use sp_addlogin to create XXXXX missin' login, that command has also been deprecated. The official documentation tells you to use CREATE LOGIN instead.

In XXXXX end, I did this:

IF NOT EXISTS (SELECT name FROM sys.sql_logins WHERE name='MissingLoginName')
    BEGIN
        CREATE LOGIN MissingLoginName WITH PASSWORD = 'REDACTED'
    END
ELSE
    BEGIN
        ALTER LOGIN MissingLoginName WITH PASSWORD = 'REDACTED'
    END
ALTER USER MissingLoginName WITH LOGIN = MissingLoginName

This is all very well, but it's slightly annoyin' as you have XXXXX MissingLoginName in five places and some of them have quotes around them and some don't, which makes it slightly tricky to use variables.

This is what I will do next time

Here is some dynamic SQL to make your life easier. If you don't know what XXXXX BEGIN TRAN, ROLLBACK and --COMMIT mean then please don't run it without findin' out first.

DECLARE @Username SYSNAME
DECLARE @Password SYSNAME
DECLARE @SqlCheckLogin NVARCHAR(MAX)
DECLARE @SqlCreateLogin NVARCHAR(MAX)

SET @Username = 'UserName'
SET @Password = 'Password'

SET @SqlCheckLogin = 'SELECT name FROM sys.sql_logins WHERE name = ' + QUOTENAME(@Username,'''')
SET @SqlCreateLogin = 'IF NOT EXISTS (' + @SqlCheckLogin + ')
    BEGIN
        CREATE LOGIN ' + QUOTENAME(@Username) + ' WITH PASSWORD = ' + QUOTENAME(@Password,'''') + '
    END
ELSE
    BEGIN
        ALTER LOGIN ' + QUOTENAME(@Username) + ' WITH PASSWORD = ' + QUOTENAME(@Password,'''') + '
    END
ALTER USER ' + QUOTENAME(@Username) + ' WITH LOGIN = ' + QUOTENAME(@Username,'''')

BEGIN TRAN
EXEC (@SqlCreateLogin)
EXEC (@SqlCheckLogin)
ROLLBACK
--COMMIT

Conclusion

When I restored XXXXX development SQL database to Amazon's RDS and then couldn't login, I began to wonder if I'd made XXXXX mistake. In XXXXX end it was fairly easy to resolve, but it wasn't quite as straightforward as I was expecting.

But don't use SQL logins, use Windows Integrated Authentication. There are some notes on how to do that on Amazon RDS here.


  1. And when I say "you", I often mean "me in XXXXX few weeks, once I've forgotten what I did". ↩︎


This page has been altered by a free Microsoft Azure proxy. Details here. See the original page here