Login failed for user sql

2019-08-19 19:49发布

问题:

i have a query that retrieve data from 2 differents server, but once i execute the query i am getting this error

Login failed for user 'usr1'

i am running the query in server1, with username: usr1

 select userid, m.name, Cardid, m.dircode, 
 from [Server1].Database1.dbo.member m
 JOIN [Server2].Database2.dbo.GetUserDetails p 
 on p.cprno = m.cardid 
 WHERE PerUserName='mftrame'

i already provide usr1 read permission on [Server2].Database2.dbo.GetUserDetails

also how can i check if i added a link to server1 into server2

回答1:

From http://support.microsoft.com/kb/889615:

This problem occurs when the SQL Serversecurity authentication is set to Windows only, and one of the following conditions is true: You are trying to connect to a SQL Server database with a SQL Server login.[...]

You need to enable SQL-Server logins on the other sql server.
Most-likely, the server currently only allows integrated-security logins.

Also check with SSMS if it actually works.
The SQL-Server account will also need to have the rights to access the database.

SELECT 
     ss.server_id
    ,ss.name
    ,ss.product
    ,ss.provider
    ,ss.data_source


    ,ss.is_linked
    ,ss.is_remote_login_enabled
    ,ss.is_rpc_out_enabled
    ,ss.is_data_access_enabled
    ,ss.uses_remote_collation

    ,ss.is_remote_proc_transaction_promotion_enabled

    ,ll.local_principal_id
    ,ll.uses_self_credential
    ,ll.remote_name

    ,sp.type_desc
    ,sp.default_database_name
    ,sp.default_language_name
FROM sys.linked_logins as ll 

LEFT JOIN sys.server_principals  as sp
    ON sp.[principal_id] = local_principal_id 

LEFT JOIN sys.servers AS ss 
    ON ss.server_id = ll.server_id

To enable mixed-mode authentication:

USE master
/* Mixed Authenication script SR 01-14-10, can update SQL authentication for instances.
works with SQL 05/08 not tested with SQL 05 instances*/
DECLARE @INSTANCEID VARCHAR(30) 
DECLARE @STRVERSION VARCHAR(30)
DECLARE @SQLVERSION VARCHAR(30)
DECLARE @CMD VARCHAR(2000)
SET @SQLVERSION = (SELECT CONVERT(VARCHAR(20),(SERVERPROPERTY('productversion'))))
SET @INSTANCEID = ((SELECT CAST(SERVERPROPERTY('InstanceName') AS VARCHAR)))

IF (SELECT REPLACE(LEFT(@SQLVERSION,2),'.','')) = 10 
BEGIN
SET @STRVERSION = 'MSSQL10'
END
ELSE
IF (SELECT REPLACE(LEFT(@SQLVERSION,2),'.','')) = 9
BEGIN
SET @STRVERSION = 'MSSQL'
END

IF @INSTANCEID IS NULL AND @STRVERSION = 'MSSQL'
BEGIN
SET @INSTANCEID = 1
END
ELSE
IF @INSTANCEID IS NULL AND @STRVERSION = 'MSSQL10'
BEGIN
SET @INSTANCEID = 'MSSQLSERVER'
END


SET @CMD = 'xp_regwrite ' + 'N' + '''HKEY_LOCAL_MACHINE''' + ',' + ' N' + '''Software\Microsoft\Microsoft SQL Server\'+ @STRVERSION +'.'+ @INSTANCEID + '\MSSQLServer'''+','+' N'+'''LoginMode'''+', '+'REG_DWORD'+','+ ' 2' --2 is mixed auth.

--EXEC(@CMD)
PRINT @CMD
--PRINT 'A restart of SQL is required for authentication changes to take effect'

Maybe an example does help:

To access table t_users on database catalog cor_basic on server cordb2005 with user ApertureWebServicesDE (on cordb2005) and password MY_TOP_SECRET_PASSWORD you would use:

EXEC master.dbo.sp_addlinkedserver 
    @server = N'RemoteDB'
   ,@srvproduct = 'OLE DB Provider for SQL'
   ,@provider = N'SQLNCLI'
   ,@datasrc = 'CORDB2005'
   ,@catalog = 'COR_Basic'

GO

EXEC master.dbo.sp_addlinkedsrvlogin 
@rmtsrvname = N'RemoteDB'
   ,@useself = false
   --,@locallogin = 'LocalIntegrationUser'
   ,@locallogin = NULL 
   ,@rmtuser = N'ApertureWebServicesDE'
   ,@rmtpassword = N'MY_TOP_SECRET_PASSWORD'
GO




select * 
from RemoteDB.COR_Basic.dbo.t_users 

This of course preconditions that the login of ApertureWebServicesDE on CORDB2005 works, and that ApertureWebServicesDE has sufficient rights to access database COR_Basic and do a select on T_Users.

Restart required for the above changes to take effect !