How do I grant myself admin access to a local SQL Server instance?

后端 未结 6 1535
一个人的身影
一个人的身影 2020-12-04 06:49

I installed SQL Server 2008 R2 to my local machine. But, I can\'t create a new database because of rights (or lack of).

\"CREATE DATABASE PERMISSION D

6条回答
  •  余生分开走
    2020-12-04 07:10

    Open a command prompt window. If you have a default instance of SQL Server already running, run the following command on the command prompt to stop the SQL Server service:

    net stop mssqlserver
    

    Now go to the directory where SQL server is installed. The directory can for instance be one of these:

    C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn
    C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Binn
    

    Figure out your MSSQL directory and CD into it as such:

    CD C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn
    

    Now run the following command to start SQL Server in single user mode. As SQLCMD is being specified, only one SQLCMD connection can be made (from another command prompt window).

    sqlservr -m"SQLCMD"
    

    Now, open another command prompt window as the same user as the one that started SQL Server in single user mode above, and in it, run:

    sqlcmd
    

    And press enter. Now you can execute SQL statements against the SQL Server instance running in single user mode:

    create login [<>] from windows;
    
    -- For older versions of SQL Server:
    EXEC sys.sp_addsrvrolemember @loginame = N'<>', @rolename = N'sysadmin';
    
    -- For newer versions of SQL Server:
    ALTER SERVER ROLE [sysadmin] ADD MEMBER [<>];
    
    GO
    

    Source.

    UPDATED Do not forget a semicolon after ALTER SERVER ROLE [sysadmin] ADD MEMBER [<>]; and do not add extra semicolon after GO or the command never executes.

提交回复
热议问题