Sql server authentication fails using SSMS but works when using sqlcmd - sql-server

I have my SQLServer 2005 Standard instance configured for mixed mode fine. When using SSMS, none of the sql server logins seem to work. Just "Login failed for user '..'" message. But when I use SQLCMD, it works fine. The behavior is not just for one specific login account. All sql server logins including 'sa' fail when tried via SSMS. Windows logins work fine.
Any ideas?
EDIT1:
When connecting via SQLCMD, I do not specify the -S option. Just the -U and -P
Whereas in SSMS, Server name defaults to the machine name.
In SSMS the server seems to be registered as say xxxxN. When I connect using windows authentication, my ##servername returns xxxx_N.
When I try to use the xxxx_N in SSMS it fails to connect altogether.
Edit2:
When trying via SQLCMD if I give the -S option as xxxxN, it works fine too.
Edit3:
My SQLCMD:
C:...>SQLCMD -U sa -P Password (works)
C:...>SQLCMD -S xxxxN -U sa -P Password (works)
C:...>SQLCMD -U test -P Password (works)
C:...>SQLCMD -S xxxxN -U test -P Password (works)
in SSMS:
ServerType: Database Engine
ServerName: xxxxN
AUthentication: Windows Authentication (works)
ServerType: Database Engine
ServerName: xxxxN
AUthentication: Sql Server Authentication
Login: sa
Password: Password (fail)
ServerType: Database Engine
ServerName: xxxxN
AUthentication: Sql Server Authentication
Login: test
Password: Password (fail)
EDIT 4(Answer)
ODBC connections using sql accounts seem to work fine.
SSMS connections from remote machines work fine also.
This HAS TO BE an issue with local SSMS install. I believe a reinstall would fix this..

Your server/instance name is not consistent between the two.
MyMachineName\Express
is the "most common" instance name.
But yours may vary.
Since sqlcmd "works".
Run sqlcmd, and pass it this command
select [MyServerAndMaybeInstanceName] = ##servername
Then you will know what to put into SSMS ! (ta da!)
EDIT:
You say in sqlcmd, you give it
-U and -P
thus you're using Sql Authentication. (and not windows authentication)
Are you providing Sql Authentication credentials when trying through SSMS ?
I see you said "All sql server logins including 'sa' "
thus you are trying Sql Authentication credentials.
Can you just verify you've tried some Sql Authentication creditials, maybe something besides sa.
EDIT:
Here is a guess, but I hit it one time, a long time ago.
In the SSMS connection box, click "Connection Properties"
Find "Network".
And pick TCP/IP as the network protocol.
especially if the machine is a remote machine.
EDIT:
Here is another poor man's test.
Go to Control Panel /Admin Tools / Data Source (ODBC) / System DSN. And try to add a connection there.
You can also play with the protocols there (named-pipes vs tcp/ip, etc) with the "Client Configuration" button in the Wizard.

Related

Is there a PowerShell function to reconnect to a SQL Server after restart?

I have a basic Microsoft SQL Server (SQL Server 2017 on Server 2016 Datacenter) in Azure that's currently being power-managed. Every morning I RDP into the VM, enter the SQL Server Management Studio, and reconnect using my local admin account.
The machines automatically reboot, but is there a way to automate the reconnection process?
I reconnect using a GUI interface with the fields:
Server Type: Database Engine
Server Name: hostname
Authentication: Windows Authentication
User name: localadmin
Password:
I then click connect and boom. It seems like there should be a function, something like:
Reconnect-SQLServer -Type DatabaseEngine -Name hostname
-Authentication Windows -Credentials $Credentials
Is there? I haven't been able to find one. Bear with me, I'm fairly new to this sort of stuff.
Edit: It was my impatience. After a short period of time, the database was online and automatically reconnected. The manual process was not needed. Thanks to those who tried to help out.
You can automate connecting sql server management studio with SSMS.exe flags like:
# use current running credentials for windows authentication
SSMS.exe -E -S hostname -d MyDatabase -nosplash
# use specific user account (will prompt for password)
SSMS.exe -U MyUserName -S hostname -d MyDatabase
It kind of sounds like you need something to just end the power-management? It would depend on how that is configured
I'm not familiar with Citrix, but it looks like there is a powershell module for monitoring/managing it and its database connections with cmdlets like Get-BrokerServerStatus for example:
https://support.citrix.com/article/CTX232979/how-to-connect-site-to-sql-ha-sql-mirror-or-a-new-sql-server-after-migrating-your-database

What are the default SQL Server credentials created via docker?

I'm trying to set up a SQL Server on my raspberry with Ubuntu server 20.04. I've followed both a tutorial and the Microsoft documentation and it runs pretty well.
I've finally run this command :
sudo docker run -e 'MSSQL_PID=developer' -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=XXXX' -p 1433:1433 --name azuresqledge -h azuresqledge -d mcr.microsoft.com/azure-sql-edge:latest
I assumed the login would be "developer" and the password "XXX" but it turns out it's not working (Login failed, err n.18456). I tried to access the SQL Server from my computer with SQL Server Management Studio, but it refuses the connection telling me that the credentials are wrong. What can I do?
I also tried with the login "azuresqledge" in doubt, but without result.
Thanks for your help !
The environment variable MSSQL_SA_PASSWORD specifies the password for the sa account. The MSSQL_PID environment value is unrelated an account; it specifies the SQL Server product edition (Developer edition here).
You can provision other accounts after starting the container by connecting with the sa login and executing statements to create logins, database users, etc.. Alternatively, use the SSMS Object Explorer graphical interface for same.
CREATE LOGIN developer WITH PASSWORD = 'password1';
To expand on Dan's answer above which helped me discover the following...
If you need to use a specific login with a password that doesn't meet the requirements, you can turn off the policy like this:
create login admin with password = 'admin', check_policy = off
and if you want it to have sa permissions...
exec master..sp_addsrvrolemember #loginame = 'admin', #rolename = 'sysadmin'

I forgot my sql server 2008 r2 username an password

I installed an instance of sql server on my machine a while back.
I generally login via SSMS. I currently forgot the username and password for the instance.
Is there a way I can recover it. I searched online including a pervious stack overflow post but didnt
find it helpful. Is there anything I can do to recover my username and password ?
Did you try to Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out
Start the instance of SQL Server in single-user mode by using either
the -m or -f options. Any member of the computer's local
Administrators group can then connect to the instance of SQL Server as
a member of the sysadmin fixed server role.
And then try to do the following steps as explained by Remus Rusanu in this answer.
shutdown MSSQL$EXPRESS service (or whatever the name of your SQL Express service is)
start add the -m and -f startup parameters (or you can start sqlservr.exe -c -sEXPRESS -m -f from console)
connect to DAC: sqlcmd -E -A -S .\EXPRESS or from SSMS use admin:.\EXPRESS
run create login [machinename\username] from windows to create your Windows login in SQL
run sp_addsrvrolemember 'machinename\username', 'sysadmin'; to make urself sysadmin member
restart service w/o the -m -f

Connecting to a SQL Server in a network via Command Line and Windows Authentication

From the system where the server is running, I can login using the following command:
sqlcmd -S "ComputerName\InstanceName" -d "DatabaseName" -i "sql.txt" -s"," -o "result.csv"
I want to connect to this server from another machine, So I tried this
sqlcmd -U "UserName" -S "ComputerName\InstanceName" -d "DatabaseName" -i "sql.txt" -s"," -o "result.csv"
"UserName" above is the default user name of the server machine that has administrative privileges. This user account does not have any password.
After issuing that command, it asks for password. Since there is no password, I hit enter. But I get the following error:
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed for user 'UserName'..
SQLCMD Reference
i met this situation:
sqlcmd -S "server" -d "db" -E -i test.sql
which gives "Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed for user 'DOMAIN\administrator'.."
but when i switched to:
sqlcmd -S server -d db -E -i test.sql
it works fine. so look out for your double quotes. hope this maybe of help.
-U UserName specifies the name of a SQL Server login, not a Windows user. It seems to me you want to use a Windows user. The proper way to do it is to have a domain, run as a domain user and grant appropriate permissions in SQL to domain group of which you are member. Bare that, you can use runas /netonly /user:ComputerName\UserName sqlcmd -E.
The issue is related to differences between SQL and windows authentication.
In case of windows authentication e.g. in domain environment with several untrusted domains it is only option to use runas.exe /netonly /user:YourTargetDomain\SQLUser to run SQLCMD or Ssms.exe (SQL management studio) under the proper SQL User.
Both sqlcmd and ssms can use Windows (also domain) authentication without any credentials SQLCMD -E (UI option in Studio) but process should be run as a local or domain user configured for NT Authentification in SQL. Or provide SQL user credentials (configured directly on SQL server e.g. SA) - SQLCMD -U SQLUserName -P SQLUserPassword (the second UI option in the Studio).
Commands below can be used to connect to SQL server configured with Windows (NT) authentication (e.g. lab domain user) from machine in separate untrusted domain (e.g. from office laptop with your office domain account - not configured to access the SQL server).
To run SSMS (it is possible to create a windows desktop shortcut, path to ssms can be different, depends on used version and installation preferences):
C:\Windows\System32\runas.exe /netonly /user:MyLabDomain\SqlUser "C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\Ssms.exe
To run a query with SQLCMD (in the example, it cleans a table, also can be used as a shortcut):
C:\Windows\System32\runas.exe /netonly /user:MyLabDomain\SqlUser "sqlcmd -S 123.123.12.34 -E -I -Q \"delete FROM TestDB..TestTableToEmpty\""
SSMS or SQLCMD should be installed on machine from which you connect to SQL server.
After run you should enter passwords for the SQL User.
I suppose, these examples may help somebody.

Can't log in to SQL Server 2008 Management Studio

I think something happened to an mdf file on disk.
When I try to start SQL Server Mgmt Studio (SQL Server 2008, Win 7 x64), I get an error:
"Cannot open user default database."
Since I can't connect at all, how do I fix this?
I have tried connecting to other dbs using osql, but always get an error.
Thanks for any workarounds...
The easiest way to do this is via command line. You keep opening connections using SSMS that fail...
Using command line you'll have one connection and delimit the login name correctly: "[SVR\USR]"
sqlcmd -S InstanceName -d master -E -Q"ALTER LOGIN [SVR\USR] WITH DEFAULT_DATABASE = master"
or replace -E with -U SQLLogin -P Password if you have a SQL login handy
When connecting from Management studio, In the "connect to server" dialog box, before connecting, click "Options" and change the "Connect to database" dialog box to "master".
Login as an administrator and change your default database: ALTER LOGIN ... WITH DEFAULT_DATABASE = .... If you're not an administrator, ask an administrator to do it. If you must log in in the database in question, ask an administrator to investigate whi the database is offline.

Resources