創建用戶登錄注意事項
密碼是區分大小寫的。
只有創建SQL Server登錄時,才支持對密碼預先進行哈希運算。
如果指定MUST_CHANGE,則CHECK_EXPIRATION和 CHECK_POLICY必須設置為 ON。 否則,該語句將失敗。
不支持CHECK_POLICY=OFF和 CHECK_EXPIRATION=ON的組合。
如果CHECK_POLICY設置為OFF,將對lockout_time進行重置,並將CHECK_EXPIRATION設置為OFF。
只有在Windows Server 2003及更高版本上才會強制執行CHECK_EXPIRATION 和 CHECK_POLICY。
從證書或非對稱密鑰創建的登錄名僅用於代碼簽名。不能用於連接到 SQL Server。僅當master中已存在證書或非對稱密鑰時,才能從證書或非對稱密鑰創建登錄名。
有關用於傳輸登錄名的腳本,請參閱如何在 SQL Server 2005 和 SQL Server 2008 的實例之間傳輸登錄名和密碼。
自動創建登錄名將啟用新的登錄名,並授予它服務器級CONNECT SQL 權限。
服務器的身份驗證模式必須匹配登錄名類型才能允許訪問。
有關設計權限系統的信息,請參閱 Getting Started with Database Engine Permissions。
只有具有針對服務器的ALTER ANY LOGIN權限或securityadmin固定服務器角色的成員身份的用戶才可創建登錄。
如果使用CREDENTIAL選項,則還需要對此服務器的ALTER ANY CREDENTIAL權限。
使用SSMS數據庫管理工具創建用戶登錄
1、連接數據庫-》展開安全性-》選擇登錄名-》選擇新建登錄名。
2、在登錄名-新建彈出框-》點擊常規-》輸入登錄名-》選擇sqlserver身份驗證-》輸入密碼-》選擇不強制實施密碼過期策略-》選擇默認數據庫-》選擇默認語言。
3、在登錄名-新建彈出框-》點擊服務器角色-》選擇要向角色授予的安全特權。
4、在登錄名-新建彈出框-》點擊用戶映射-》選擇用戶可以登錄的數據庫-》選擇用戶擁有的登錄數據庫的權限。
5、在登錄名-新建彈出框-》點擊安全對象-》點擊搜索添加安全對象-》安全對象添加完成后選擇安全對象權限。
6、在登錄名-新建彈出框-》點擊狀態-》選擇默認即可。
7、點擊確定-》查看添加結果。
使用T-SQL腳本創建用戶登錄
語法
--聲明數據庫引用
use testss;
go
創建用戶登錄
create login login_name
with
password={ 'password' | hashed_password hashed }
[must_change][,]
[sid=0x14585E90117152449347750164BA00A7][,]
[default_database=database_name][,]
[default_language=language][,]
[check_expiration={ on | off }][,]
[check_policy={ on | off }][,]
[credential=credential_name]
語法注釋
--login_name
--指定創建的登錄名。有四種類型的登錄:SQLServer登錄、Windows登錄、證書映射登錄和非對稱密鑰映射登錄。
--在創建從Windows域帳戶映射的登錄名時,必須以[<domainName>\<login_name>]格式使用Windows 2000之前的用戶登錄名。
--不能使用login_name@DomainName格式的UPN。
--有關示例,請參閱本文后面的示例D。身份驗證登錄的類型為sysname,它必須符合標識符規則,且不能包含“\”。
--Windows登錄名可以包含“\”。Active Directory用戶的登錄名需少於21個字符。
--password='password*'
--僅適用於SQL Server登錄。指定正在創建的登錄名的密碼。應使用強密碼。
--有關詳細信息,請參閱強密碼和密碼策略。從SQL Server 2012 (11.x)開始,存儲的密碼信息使用 SHA-512 加鹽密碼進行計算。
--密碼是區分大小寫的。密碼應始終至少包含 8 個字符,並且不能超過128個字符。
--密碼可以包含 a-z、A-Z、0-9 和大多數非字母數字字符。 密碼不能包含單引號或 login_name。
--password=hashed_password
--僅適用於hashed關鍵字。指定要創建的登錄名的密碼的哈希值。
--hashed僅適用於SQL Server登錄。指定在password參數后輸入的密碼已經過哈希運算。
--如果未選擇此選項,則在將作為密碼輸入的字符串存儲到數據庫中之前,對其進行哈希運算。
--此選項應僅用於在服務器之間遷移數據庫。切勿使用hashed選項創建新的登錄名。hashed選項不能用於SQL 7或更早版本創建的哈希。
--must_change
--僅適用於SQL Server登錄。如果包括此選項,則SQL Server將在首次使用新登錄時提示用戶輸入新密碼。
--sid=sid
--用於重新創建登錄名。僅適用於SQL Server身份驗證登錄,不適用於Windows身份驗證登錄。指定新SQL Server身份驗證登錄的sid。
--如果未使用此選項,SQL Server將自動分配sid。sid結構取決於SQL Server版本。 QL Server登錄sid:基於GUID的16 字節(binary(16))文本值。 例如,sid 0x14585E90117152449347750164BA00A7。
--default_database=database
--指定將指派給登錄名的默認數據庫。如果未包括此選項,則默認數據庫將設置為master。
--default_language=language
--指定將指派給登錄名的默認語言。如果未包括此選項,則默認語言將設置為服務器的當前默認語言。即使將來服務器的默認語言發生更改,登錄名的默認語言也仍保持不變。
--check_expiration={ on | off }
--僅適用於SQL Server登錄。 指定是否應對此登錄帳戶強制實施密碼過期策略。 默認值為off。
--check_policy={ on | off }
--僅適用於SQL Server登錄。 指定應對此登錄強制實施運行SQL Server 計算機的 Windows 密碼策略。 默認值為on。
--如果 Windows 策略要求強密碼,密碼必須至少包含以下四個特點中的三個:
--大寫字符 (A-Z)。
--小寫字符 (a-z)。
--數字 (0-9)。
--一個非字母數字字符,如空格、、@、*、^、%、!、$、# 或 &。
--credential=credential_name
--將映射到新SQL Server登錄的憑據名稱。 該憑據必須已存在於服務器中。當前此選項只將憑據鏈接到登錄名。憑據不能映射到系統管理員(sa)登錄名。
示例
--聲明數據庫引用
use testss;
go
--創建登錄用戶
create login testuser
with
password='123456',
--must_change,
--sid=0x14585E90117152449347750164BA00A7,
default_database=master,
--default_language=language,
check_expiration=off,
check_policy=off
--credential=[sysadmin]
go
--可以添加多個服務器角色
--創建服務器角色, 服務器角色用於向用戶授權服務器范圍內的安全特權
--alter server role [bulkadmin] add member testuser;
--go
--alter server role [dbcreator] add member testuser;
--go
--alter server role [diskadmin] add member testuser;
--go
--alter server role [processadmin] add member testuser;
--go
--alter server role [securityadmin] add member testuser;
--go
--alter server role [serveradmin] add member testuser;
--go
--alter server role [setupadmin] add member testuser;
--go
alter server role [sysadmin] add member testuser;
go
--創建用戶映射,映射到此登錄名的用戶
use [model]
go
create user testuser for login testuser;
go
--use [msdb]
--go
--create user testuser for login testuser;
--go
--use [ReportServer]
--go
--create user testuser for login testuser;
--go
--use [ReportServerTempDB]
--go
--create user testuser for login testuser;
--go
--use [tempdb]
--go
--create user testuser for login testuser;
--go
use [testss]
go
create user testuser for login testuser;
go
----創建用戶登錄數據庫
--use [master]
--go
--create user [testuser] for login [testuser];
--go
----創建用戶登錄多個數據庫
--use [testss]
--go
--create user [testuser] for login [testuser];
--go
----聲明數據庫應用
--use [testss]
--go
----授予不安全的程序集
--grant unsafe assembly to testuser;
--go
----授予查看服務器狀態
--grant view server state to testuser;
--go
----授予查看任意定義
--grant view any definttion to testuser;
--go
----授予查看任意數據庫
--grant view any database to testuser;
--go
----授予創建DDL事件通知
--grant create ddl event notification to testuser;
--go
----授予創建端點
--grant create endpoint to testuser;
--go
----授予創建服務器角色
--grant create server role to testuser;
--go
----授予創建跟蹤事件通知
--grant create trace event notification to testuser;
--go
----授予創建可用性組
--grant create availability group to testuser;
--go
----授予創建任意數據庫
--grant create any database to testuser;
--go
----授予更改服務器狀態
--grant alter server state to testuser;
--go
----授予更改跟蹤
--grant alter trace to testuser;
--go
----授予更改任何服務器角色
--grant alter any server role to testuser;
--go
----授予更改任何可用性組
--grant alter any availability group to testuser;
--go
----授予更改任意登錄名
--grant alter any login to testuser;
--go
----授予更改任意端點
--grant alter any endpoint to testuser;
--go
----授予更改任意服務器審核
--grant alter any server audit to testuser;
--go
----授予更改任意權限
--grant alter any connection to testuser;
--go
----授予更改任意連接服務器
--grant alter any linked server to testuser;
--go
----授予更改任意憑據
--grant alter any credential to testuser;
--go
----授予更改任意事件會話
--grant alter any event session to testuser;
--go
----授予更改任意事件通知
--grant alter any event notification to testuser;
--go
----授予更改任意數據庫
--grant alter any database to testuser;
--go
----授予更改設置
--grant alter settings to testuser;
--go
----授予更改資源
--grant alter resources to testuser;
--go
----授予關閉
--grant shutdown to testuser;
--go
----授予管理大容量操作
--grant administer bulk operations to testuser;
--go
----授予控制服務器
--grant control server to testuser;
--go
----授予連接SQL
--grant connect sql to testuser;
--go
----授予外部訪問程序集
--grant external access assembly to testuser;
--go
----授予驗證服務器
--grant authenticate server to testuser;
--go
--設置是否允許連接到數據庫引擎
--deny connect sql to testuser;
--go
--是否允許登錄
--alter login testuser disable;
--go
----用戶狀態
----聲明默認數據庫引用
--use [testuser]
--go
----是否允許用戶連接到數據庫引擎
--deny connect sql to [testuser];
--go
----是否允許登錄
--alter login [testuser] disable
--go
示例結果
使用新創建的用戶名登錄
1、在對象資源管理器中-》點擊新建連接。
2、在連接到服務器彈出框-》選擇sqlserver身份驗證-》輸入用戶名和密碼-》點擊連接。
3、查看結果。