About Me

My photo
I know the last digit of PI

Thursday, September 15, 2011

Forget to add administrators group to SQL server 2008

Yep shit happens...
If you forget to add the administrator group (or at least the current user) to admin groups, and do not know the SA password then you have only two options
- reinstall SQL server
- use single-user-mode to add the accounts

Ok now the single-user-mode procedure.
0) login with Administrator (exactly the Administrator user, with another administrator account there will be problems)
1) open services.msc
2) go the "SQL Server (xxx)" service and stop it
3) open "Sql server configuration manager" from the start menu and make sure that the protocols "Shared memory", "Named pipes", "TCP/IP" are enabled
4) Go back to the services console right click "SQL Server (xxx)" and select properties and then enter "-m" as parameter. Click start
5) Now the server is started in single-user mode
6) Open command prompt and execute following command "sqlcmd -S \ -E", where -S specifies server and instance name, -E specifies to use windows credentials

sqlcmd -S MYMACHINE\SQLEXPRESS -E

7) Execute following commands for each windows account that you want to add

CREATE LOGIN [MYMACHINE\Administrator] FROM WINDOWS
GO
exec sp_addsrvrolemember @loginame='MYMACHINE\Administrator', @rolename = 'sysadmin'
GO

8) Stop the SQL service and restart it without -m parameter.
9) Now you are able to log-in with Administrator user and you do have administrator rights on SQL server

Monday, September 12, 2011

Using older Java on Windows

When JDK 7 is installed it became the default java on the machine. However if you try to set different JDK (by JAVA_HOME and PATH variables) it won't change. The reason is that there is a c:\windows\system32\java.exe wrapper that reads the system registry and executes the java. If you try to change
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion to 1.6 you will get the following error:


C:\Windows\System32>java -help
Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'

has value '1.6', but '1.7' is required.
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.


I found two solutions on this problem:
1) use -version:1.6 as parameter

java -version:1.6 -jar myprogram.jar

It will run the jar with version 1.6
Of course you can set it into some system property like JAVA_OPT (for tomcat)

2) Add JAVA_HOME environment variable, and put %JAVA_HOME%\bin before %SystemRoot%\system32.
So the JAVA_HOME\bin\java.exe will be found before the c:\windows\system32\java.exe wrapper.