Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add login and connect to SQL with SQL Server Authentication

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?

like image 292
edgarmtze Avatar asked Jul 10 '11 21:07

edgarmtze


People also ask

How do I configure SQL Server to allow SQL authentication?

In SQL Server Management Studio Object Explorer, right-click the server, and then click Properties. On the Security page, under Server authentication, select the new server authentication mode, and then click OK.

How do I use Windows Authentication to connect to SQL Server?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.


1 Answers

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"
like image 108
Jeff Ogata Avatar answered Dec 05 '22 13:12

Jeff Ogata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!