Search code examples
sql-serverauthenticationrestore

SQL Server 2012 login problem with a restored database


I took a backup and when I tried to restore it on another server, everything seemed to work perfectly, but I can not connect to it from any of my web applications or services.

I get this error:

System.Data.SqlClient.SqlException: Login failed for user 'domain\user'.

I use an Active Directory technical user in my web application.

I ran this line due to its user:

USE database;    
ALTER USER [domain\user] WITH LOGIN = [domain\user]

I tried restore it in SSMS and with this code, too:

USE master;
GO

RESTORE DATABASE database
FROM DISK = 'U:\BACKUP\database.bak'
WITH RECOVERY,  
MOVE 'database' TO 'U:\DATA\database.mdf',
MOVE 'database_log' TO 'V:\LOG\database_log.ldf';
GO

I added the db_datareader and the db_datawriter and even the db_owner roles to the technical user.

I do not know what the problem is.

UPDATE
The source SQL Server version is 11.0.6567.0, the target version is 11.0.7001.0.

UPDATE 2

I tried this:

EXEC sp_change_users_login 'Update_One', 'domain\user', 'domain\user'

but it throws this error:

Terminating this procedure. The User name 'domain\user' is absent or invalid.


Solution

  • You have to map login (sql server) to user (database).

    After restore, do those command, in that order :

    CREATE LOGIN [domain\user] from windows
    

    and

    USE database;    
    ALTER USER [domain\user] WITH LOGIN = [domain\user]