欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL Server正确删除Windows认证用户的方法

程序员文章站 2022-09-04 08:09:49
前言 在sql server数据库中,有时候会建立一些windows认证的账号(域账号),例如,我们公司习惯给开发人员和support同事开通nt账号权限,如果有离职或负责事宜变更的...

前言

在sql server数据库中,有时候会建立一些windows认证的账号(域账号),例如,我们公司习惯给开发人员和support同事开通nt账号权限,如果有离职或负责事宜变更的话,那么要如何正确的删除这些windows认证账号呢?这篇文章就是来探讨一下如何正确的删除windows认证账号。如下所示:

下面这种方式,仅仅是删除登录名(login),然而并没有删除用户(user)

use [master]
go

drop login [xxx\xxxx]
go

你删除登录名的时候,就会遇到类似下面的告警信息:

deleting server logins does not delete the database users associated with the logins. to complete the process, delete the users in each database. it may be necessary to first transfer the ownership of schemas to new users.

也就是说,虽然你删除了登录名,但是对应用户数据库或系统数据库相关的user权限并没有清理,在sql server中登录名(server login)跟数据库的用户(database user)是分离开来,但是又有关联的。所以正确的姿势: 在删除登录名(login)后,还必须去每个数据库,删除对应的用户(user). 在删除登录名前必须检查,有那些作业的owner或数据库的owner的为该windows认证账号(nt账号),否则后面就会遇到一些问题:

1:如果删除windows认证用户前,没有修改作业的owner(如果此作业的owner为此windows用户的话,那么删除windows认证用户后,作业就会报类似下面这种错误。

the job failed. the owner (xx\xxx) of job syspolicy_purge_history does not have server access.

所以在删除windows认证用户前,必须检查并修改作业的owner,避免这种情况出现。

2:删除windows认证用户前,确认是否有数据库的owner为此windows认证用户。否则删除登录名时会报错

msg 15174, level 16, state 1, line 4

login 'xxx\xxxx' owns one or more database(s). change the owner of the database(s) before dropping the login.

msg 15174, level 16, state 1, line 4

登录名 'xxx\xxx' 拥有一个或多个数据库。在删除该登录名之前,请更改相应数据库的所有者。

必须修改数据库的owner后(一般将数据库的owner改为sa),才能删除登录名

sp_changedbowner 'sa'

3:有时候删除用户时,报下面错误,必须修改后,才能删除对应的用户。

遇到下面错误:

msg 15138, level 16, state 1, line 3

数据库主体在该数据库中拥有 架构,无法删除。

msg 15138, level 16, state 1, line 3

the database principal owns a schema in the database, and cannot be dropped.

SQL Server正确删除Windows认证用户的方法

use yoursqldba;

go

alter authorization on schema::[db_owner] to [dbo];

use [yoursqldba]

go

drop user [xxx\konglb];

go

当然要根据实际情况来处理

use [userdatabase];

go

alter authorization on schema::[xxx] to [dbo];

另外一种是用户创建的schema,这个根上面情况没有差别。

所以正确的删除登录名,可以用脚本生成对应的sql(当然也可以执行对应的sql,但是这种高位操作,建议生成脚本,人工判断后,手工执行)

declare @login_name sysname;
set @login_name='gfg1\chenzhenh'

select d.name as database_name,
owner_sid as owner_sid ,
l.name as database_owner
from sys.databases d
left join sys.syslogins l on l.sid = d.owner_sid
where l.name=@login_name;



select 'use ' + d.name + char(10) 
+ 'go' + char(10)
+ 'exec dbo.sp_changedbowner @loginame =n''sa'', @map = false' as change_db_owner_cmd
from sys.databases d
left join sys.syslogins l on l.sid = d.owner_sid
where l.name = @login_name;


select j.job_id as job_id 
,j.name as job_name 
,case when [enabled] =1 then 'enabled'
else 'disabled' end as job_enabled 
,l.name as job_owner 
,j.category_id as job_category_id
,c.name as job_category_name
,[description] as job_description 
,date_created as date_created 
,date_modified as date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.syscategories c on j.category_id = c.category_id
inner join sys.syslogins l on l.sid = j.owner_sid
where l.name= @login_name
order by j.name



declare @job_owner nvarchar(32);

set @job_owner='sa';

select 'exec msdb.dbo.sp_update_job @job_name=n''' +j.name + ''', @owner_login_name=n''' + rtrim(ltrim(@job_owner)) + ''';' as change_job_owner_cmd
from msdb.dbo.sysjobs j
inner join msdb.dbo.syscategories c on j.category_id = c.category_id
inner join sys.syslogins l on l.sid = j.owner_sid
where l.name = @login_name
order by j.name


select '
use [master]
go
drop login ' + quotename(@login_name) + 
'
go
' as drop_login_user;

然后删除用户(user),此脚本也可以清理那些登录名已经删除,但是对应的user没有清理的windows 认证用户。此脚本可能有一些逻辑上的bug,个人也是fix掉了一些bug后,才发布这篇博客。如果遇到什么bug,可以留言反馈。

declare @database_id int;
declare @database_name sysname;
declare @cmdtext nvarchar(max);
declare @prc_text nvarchar(max);
declare @rowindex int;
declare @user_name nvarchar(128);


if object_id('tempdb.dbo.#databases') is not null
drop table dbo.#databases;

create table #databases
(
database_id int,
database_name sysname
)


insert into #databases
select database_id ,
name
from sys.databases
where name not in ( 'master', 'tempdb', 'model', 'msdb',
'distribution', 'reportserver',
'reportservertempdb', 'yoursqldba' )
and state = 0; --state_desc=online 


create table #removed_user
(
username sysname
)

--开始循环每一个用户数据库(排除了上面相关数据库)
while 1= 1
begin


select top 1 @database_name= database_name 
from #databases
order by database_id;

if @@rowcount =0 
break;


set @cmdtext = 'use ' + @database_name + ';' +char(10)

select @cmdtext += 'insert into #removed_user
select name from sys.sysusers
where sid not in (select sid from sys.syslogins where isntname=1 and name like ''gfg1%'')
and isntname=1 and name not in (''nt authority\system'')' + char(10);

exec sp_executesql @cmdtext

select @database_name as database_name;

select j.job_id as job_id 
,j.name as job_name 
,case when [enabled] =1 then 'enabled'
else 'disabled' end as job_enabled 
,l.name as job_owner 
,j.category_id as job_category_id
,c.name as job_category_name
,[description] as job_description 
,date_created as date_created 
,date_modified as date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.syscategories c on j.category_id = c.category_id
inner join sys.syslogins l on l.sid = j.owner_sid
inner join #removed_user r on l.name = r.username
order by j.name;


select d.name as database_name ,
l.name as database_owner ,
d.create_date as create_date ,
d.collation_name as collcation_name ,
d.state_desc as state_desc
from sys.databases d
inner join sys.syslogins l on d.owner_sid = l.sid
inner join #removed_user r on r.username = l.name


set @cmdtext = 'use ' + @database_name + ';' +char(10)

set @cmdtext += 'select * from sys.schemas s
inner join #removed_user r on s.name =r.username collate database_default' + char(10);

exec sp_executesql @cmdtext;


set @cmdtext = 'use ' + @database_name + ';' +char(10)

set @cmdtext += 'select * from sys.objects where schema_id in (select s.schema_id from sys.schemas s inner join #removed_user r on s.name =r.username collate database_default);'

exec sp_executesql @cmdtext;

set @cmdtext = 'use ' + @database_name + ';' +char(10)
set @cmdtext += 'select ''use ' + @database_name + ';'' + char(10) +''go'' + char(10) +''alter authorization on schema::'' +quotename(s.name) +'' to [dbo];'' as change_schema_cmd from sys.schemas s
inner join #removed_user r on s.name =r.username collate database_default ' + char(10);

exec sp_executesql @cmdtext, n'@database_name sysname',@database_name ;

set @cmdtext = 'use ' + @database_name + ';' +char(10)
set @cmdtext += 'select ''use ' + @database_name + ';'' + char(10) +''go'' + char(10) +''alter authorization on schema::'' +quotename(s.schema_name) +'' to [dbo];'' as change_schema_cmd
from information_schema.schemata s
inner join #removed_user r on s.schema_owner =r.username collate database_default' + char(10);

exec sp_executesql @cmdtext, n'@database_name sysname',@database_name ;

select 'use ' + quotename(@database_name) + char(10)
+ 'go ' + char(10)
+ 'drop user ' + quotename(username) +';' + char(10)
+ 'go' as drop_user_cmd
from #removed_user;


truncate table #removed_user;


delete from #databases where database_name=@database_name;

end

drop table #databases;
drop table #removed_user;

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。