有时候需要在每台服务器上创建账号密码一致的登录账号(SID一致),可以创建登录名的时候指定密码的哈希值和SID。例如我们搭建Always ON的时候每台服务器的账号需要SID一致。
1.查看对应登录名的sid值和密码的哈希值。(该账号必须是在某台服务器上已经存在)
SELECT name,sid,password_hash FROM
密码的哈希值也可以通过LOGINPROPERTY函数获得。
SELECT LOGINPROPERTY('wangshaibing','PasswordHash')
2.创建登录账号。
CREATE LOGIN wangshaibing WITH PASSWORD=0x0200E54F7B3233FCF59CCB93929AA7A2D1CB5DF3EF05BD688675D75F703B56FFCBA496C7F21DEC9DB325C2F0AD0E1C3C044027B6C404103DAC785AE8B3C689B2CF838C48C1E9 HASHED ,
SID=0xC9C360D6FB75F24CA628DFA0761E11D5,DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF,CHECK_POLICY=ON
3.或者直接用下面脚本
SELECT 'CREATE LOGIN [' + p.name + '] '
+ CASE WHEN p.type IN ( 'U', 'G' ) THEN 'FROM windows '
ELSE ''
END + 'WITH ' + CASE WHEN p.type = 'S'
THEN 'password = '
+ ma)
+ ' hashed, ' + 'sid = '
+ ma)
+ ', check_expiration = '
+ CASE WHEN l.is_expiration_checked > 0
THEN 'ON, '
ELSE 'OFF, '
END + 'check_policy = '
+ CASE WHEN l.is_policy_checked > 0
THEN 'ON, '
ELSE 'OFF, '
END
+ CASE WHEN l.credential_id > 0
THEN 'credential = ' + c.name
+ ', '
ELSE ''
END
ELSE ''
END + 'default_database = '
+ p.default_database_name
+ CASE WHEN LEN) > 0
THEN ', default_language = ' + p.default_language_name
ELSE ''
END
FROM p
LEFT JOIN l ON p.principal_id = l.principal_id
LEFT JOIN c ON l.credential_id = c.credential_id
WHERE p.type IN ( 'S', 'U', 'G' )
AND p.name NOT IN ( 'sa' )
AND p.name NOT LIKE '%##%'
AND p.name NOT LIKE '%NT SERVICE%'
AND p.name NOT LIKE '%NT AUTHORITY%';