Add login and connect to SQL with SQL Server Authentication

a 夏天 提交于 2019-12-20 01:37:08

问题


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




回答3:


Try the following. They worked for me when I had this connection issue.

 -When you are adding SQL user, make sure 'enforce password policy' is unchecked.
 -Make sure that user is mapped to the database with proper permissions.
 -on SSMS, right click server and then go to Security tab. Select 'SQL Server and Windows Authentication mode' option.
 -restart PC.


来源:https://stackoverflow.com/questions/6643720/add-login-and-connect-to-sql-with-sql-server-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!