Add login and connect to SQL with SQL Server Authe

2019-02-23 17:42发布

问题:

I want to add a user in SQL Server 2008 so I can use SQL Server Authentication instead Windows Authentication for connecting to SQL, and have tried this code to create a user with login:

CREATE login [newLog] with password = 'passnewLognewLog'

I get it done, but when I want to connect to SQL Server using SQL Authentication, I get this message

Cannot connect to "Mydb"
additional information: Login failed for user 'newLog'. (Microsoft SQL Server, Error: 18456 )

What am I missing here?

回答1:

After creating the login, you need to add the user to the database. This example is from the sql server documentation for CREATE USER:

CREATE LOGIN AbolrousHazem 
    WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
USE AdventureWorks2008R2;
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO 

Edit

To test, I ran this T-SQL:

create login Foo with password ='f00';
go

use TestDB
create user Foo for Login Foo
go

and opened a connection successfully using this connection string:

"Server=<server>; user id=Foo; password=f00; initial catalog=TestDB"


回答2:

  1. You need to create a user for the login, e.g.

    CREATE USER [newLog] FOR LOGIN [newLog] WITH DEFAULT_SCHEMA=[dbo]
    
  2. Check that mixed authentication is enabled: https://stackoverflow.com/a/1719476/188926