How to change sa password in SQL Server 2008 express? - sql-server

I have installed SQL Server 2008 express and logging in through windows authentication, it doesn't allow me to do anything. How do i change 'sa' password or gain full privilege in my local computers 2008 express ? I am using windows 7.

If you want to change your 'sa' password with SQL Server Management Studio, here are the steps:
Login using Windows Authentication and ".\SQLExpress" as Server Name
Change server authentication mode - Right click on root, choose Properties, from Security tab select "SQL Server and Windows Authentication mode", click OK
Set sa password - Navigate to Security > Logins > sa, right click on it, choose Properties, from General tab set the Password (don't close the window)
Grant permission - Go to Status tab, make sure the Grant and Enabled radiobuttons are chosen, click OK
Restart SQLEXPRESS service from your local services (Window+R > services.msc)

You need to follow the steps described in Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out and add your own Windows user as a member of sysadmin:
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

I didn't know the existing sa password so this is what I did:
Open Services in Control Panel
Find the "SQL Server (SQLEXPRESS)" entry and select properties
Stop the service
Enter "-m" at the beginning of the "Start parameters" fields. If there are other parameters there already add a semi-colon after -m;
Start the service
Open a Command Prompt
Enter the command:
osql -S YourPcName\SQLEXPRESS -E
(change YourPcName to whatever your PC is called).
At the prompt type the following commands:
alter login sa enable
go
sp_password NULL,'new_password','sa'
go
quit
Stop the "SQL Server (SQLEXPRESS)" service
Remove the "-m" from the Start parameters field
Start the service

This may help you to reset your sa password for SQL 2008 and 2012
EXEC sp_password NULL, 'yourpassword', 'sa'

This is what worked for me:
Close all Sql Server referencing apps.
Open Services in Control Panel.
Find the "SQL Server (SQLEXPRESS)" entry and select properties.
Stop the service (all Sql Server services).
Enter "-m" at the Start parameters" fields.
Start the service (click on Start button on General Tab).
Open a Command Prompt (right click, Run as administrator if needed).
Enter the command:
osql -S localhost\SQLEXPRESS -E
(or change localhost to whatever your PC is called).
At the prompt type the following commands:
CREATE LOGIN my_Login_here WITH PASSWORD = 'my_Password_here'
go
sp_addsrvrolemember 'my_Login_here', 'sysadmin'
go
quit
Stop the "SQL Server (SQLEXPRESS)" service.
Remove the "-m" from the Start parameters field (if still there).
Start the service.
In Management Studio, use the login and password you just created.
This should give it admin permission.

Related

CREATE DATABASE permission denied in database 'master'. Sql Server

TITLE: Microsoft SQL Server Management Studio
Create failed for Database 'myDB'. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=14.0.17119.0+((SSMS_Rel).170517-0412)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Create+Database&LinkId=20476
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
CREATE DATABASE permission denied in database 'master'. (Microsoft SQL Server, Error: 262)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.2269&EvtSrc=MSSQLServer&EvtID=262&LinkId=20476
BUTTONS:
OK
I don't have an admin account outside of 'sa'(ı dont know the password). This account deauthorize(ı can't authorize). What can I do?
PREREQUISITES:
Administrative privileges needed on Host Server.
Downtime of approximately 10 minutes needed because SQL Server would be started in ‘Single User’ mode.
PROCEDURE:
RDP to the server with the account which has ‘Admin’ rights on the Hosting Windows box. Using a service account is good idea. -> skip this if your server is a local machine
Open ‘SQL Server Configuration Manager’ :
Start>All Programs>Microsoft SQL Server 2008 (or 2005)>Configuration Tools>SQL Server Configuration
Manager
Start SQL Services in ‘Single User Mode:
Right Click on SQL Services and click ‘properties’
Go ‘Advanced’ tab then open ‘Start up Parameters’
Write ‘-m’ at the end, then click ‘OK’.
This would open a dialog box informing that SQL Services need to be restarted for this. Click ‘OK’.
Stop dependent services like SQL Agent. Restart SQL Services.
Connect to SQL instance using SQLCMD
Right Click on Command prompt, select ‘Run as Administrator’ (in Windows 2003 and lower, run as administrator is not available and not required)
Now connect to SQL instance using SQLCMD, write below command:
SQLCMD –E –S <instance name>
Now create a windows login (i.e. group account used by DB team), use below command:
CREATE LOGIN [Login_name] FROM WINDOWS WITH DEFAULT_DATABASE=[master]
GO
Now grant ‘sysadmin’ access to this newly created login, use below command:
EXEC master..sp_addsrvrolemember #loginame = 'login_name', #rolename = 'sysadmin'
GO
Restart SQL Services in multi user mode. For doing that, remove ‘-m’ from Startup parameters and restart SQL
Services.

Sql server authentication fails using SSMS but works when using sqlcmd

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.

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

SQL Server 2008 R2 Express permissions -- cannot create database or modify users

Recently just upgraded to SQL Server 2008 R2 Express. When I attempt to create a database after logging in using Windows Authentication with my id myuser I receive this error:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
CREATE DATABASE permission denied in database 'master'. RESTORE HEADERONLY is terminating abnormally Error 262
If I try to add the sysadmin role to myuser, this is the error I receive:
Add member failed for ServerRole 'sysadmin'. (Microsoft.SqlServer.Smo)
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
User does not have permission to perform this action Error 15247
If I try to add this role to my user with T-SQL, using this command,
EXEC sp_addsrvrolemember 'ziffenergy\myuser', 'sysadmin';
GO
Here is the error I receive:
Msg 15247, Level 16, State 1, Procedure sp_addsrvrolemember, Line 29
User does not have permission to perform this action.
Does anyone have any suggestions? It seems that I can't do anything with database on the local machine. Please note that I am the administrator on the Windows 7 workstation I am using, and if I try to create or modify databases and/or users on our network IT Test database server using SQL Server Management Studio, I can do that with no problem.
Coming late to the party, but I found this fantastic step-by-step guide on getting control of your SQLExpress instance if you don't have your sa password. I used this process to not only reset my sa password, but I also added my domain account to all the available server roles. I can now create databases, alter logins, do bulk operations, backups/restores, etc using my normal login.
To summarize, you use SQL Server Configuration Manager to put your instance into single-user mode. This elevates you to sysadmin when you connect, allowing you the ability to set everything up.
Edit: I've copied the steps below - kudos to the original author of the link above.
Log on to the computer as an Administrator (or Any user with administrator privileges)
Open "SQL Server Configuration Manager"
Click "SQL Server Services" on the left pane
Stop "SQL Server" and "SQL Server Agent" instance on the right pane if it is running
Run the SQL Express in single-user mode by right clicking on "SQL Server" instance -> Properties (on the right pane of SQL Server Configuration Manager).
Click Advanced Tab, and look for "Startup Parameters". Change the "Startup Parameters" so that the new value will be -m; (without the <>)
example:
from: -dc:\Program Files\Microsoft SQL.............(til end of string)
to: -m;-dc:\Program Files\Microsoft SQL.............(til end of string)
Start the SQL Server
Open your MS SQL Server Management Studio and log on to the SQL server with "Windows Authentication" as the authentication mode. Since we have the SQL Server running on single user mode, and you are logged on to the computer with Administrator privileges, you will have a "sysadmin" access to the database.
Expand the "Security" node on MS SQL Server Management Studio on the left pane
Expand the "Logins" node
Double-click the 'sa' login
Change the password by entering a complex password if "Enforce password policy" is ticked, otherwise, just enter any password.
Make sure that "sa" Account is "enabled" by clicking on Status on the left pane. Set the radio box under "Login" to "Enabled"
Click "OK"
Back on the main window of MS SQL Server Management Studio, verify if SQL Server Authentication is used by right clicking on the top most node in the left pane (usually ".\SQLEXPRESS (SQL Server )") and choosing properties.
Click "Security" in the left pane and ensure that "SQL Server and Windows Authentication mode" is the one selected under "Server authentication"
Click "OK"
Disconnect from MS SQL Server Management Studio
Open "Sql Server Configuration Manager" again and stop the SQL Server instance.
Right-click on SQL Server instance and click on "Advanced" tab. Again look for "Startup Parameters" and remove the "-m;" that you added earlier.
Click "OK" and start the SQL Server Instance again
You should now be able to log on as "sa" using the new password that you have set in step 12.
You may be an administrator on the workstation, but that means nothing to SQL Server. Your login has to be a member of the sysadmin role in order to perform the actions in question. By default, the local administrators group is no longer added to the sysadmin role in SQL 2008 R2. You'll need to login with something else (sa for example) in order to grant yourself the permissions.
In SSMS 2012, you'll have to use:
To enable single-user mode, in SQL instance properties, DO NOT go to "Advance" tag, there is already a "Startup Parameters" tag.
Add "-m;" into parameters;
Restart the service and logon this SQL instance by using windows authentication;
The rest steps are same as above. Change your windows user account permission in security or reset SA account password.
Last, remove "-m" parameter from "startup parameters";
I followed the steps in killthrush's answer and to my surprise it did not work. Logging in as sa I could see my Windows domain user and had made them a sysadmin, but when I tried logging in with Windows auth I couldn't see my login under logins, couldn't create databases, etc. Then it hit me. That login was probably tied to another domain account with the same name (with some sort of internal/hidden ID that wasn't right). I had left this organization a while back and then came back months later. Instead of re-activating my old account (which they might have deleted) they created a new account with the same domain\username and a new internal ID. Using sa I deleted my old login, re-added it with the same name and added sysadmin. I logged back in with Windows Auth and everything looks as it should. I can now see my logins (and others) and can do whatever I need to do as a sysadmin using my Windows auth login.
I have 2 accounts on my windows machine and I was experiencing this problem with one of them. I did not want to use the sa account, I wanted to use Windows login. It was not immediately obvious to me that I needed to simply sign into the other account that I used to install SQL Server, and add the permissions for the new account from there
(SSMS > Security > Logins > Add a login there)
Easy way to get the full domain name you need to add there open cmd echo each one.
echo %userdomain%\%username%
Add a login for that user and give it all the permissons for master db and other databases you want. When I say "all permissions" make sure NOT to check of any of the "deny" permissions since that will do the opposite.
I got the same problem till I found this.
I open a new query and run the following
RESTORE DATABASE <YourDatabase>
FROM DISK='<the path to your backup file>\<YourDatabase>.bak'
then right-click on Databases -> Restore Database -> select Device -> click ... -> ADD your dataset -> Click Ok

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