SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

时间:2023-03-08 23:30:31
SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

今天在创建数据库的时候突然发现,xp_cmdshell的存储过程不能用了,网上一搜,发现大部分都是只关闭安全配置,然后就有了下文

SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

代码:具体的看注释,值得一提的是==》reconfigure with override,上面一句语句如果不加这句,则只是临时可用,不会影响系统原有配置(可以理解为==》不加就是new和加了就是override

SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

代码贴上:

--创建目录(如果指定的路径不存在就会报错)
exec sp_configure 'show advanced options', --显示高级选项
reconfigure with override--重新配置
exec sp_configure 'xp_cmdshell', --1代表允许,0代表阻止
reconfigure with override
exec xp_cmdshell 'mkdir F:\Work\SQL mkdir E:\SQL'
exec sp_configure 'xp_cmdshell',
reconfigure with override
exec sp_configure 'show advanced options',
reconfigure with override

SQL Server 阻止了对组件“xp_cmdshell”的 过程“sys.xp_cmdshell”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

SQL也贴上吧,比较这玩意总得有个语境吧:

--如果数据库存在就删除
use master
if exists(select * from sysdatabases where Name=N'LawyerBlog')
begin
drop database LawyerBlog
end --创建目录(如果指定的路径不存在就会报错)
exec sp_configure 'show advanced options', --显示高级选项
reconfigure with override--重新配置
exec sp_configure 'xp_cmdshell', --1代表允许,0代表阻止
reconfigure with override
exec xp_cmdshell 'mkdir F:\Work\SQL mkdir E:\SQL'
exec sp_configure 'xp_cmdshell',
reconfigure with override
exec sp_configure 'show advanced options',
reconfigure with override --创建数据库
create database LawyerBlog
on primary --数据库文件,主文件组
(
name='LawyerBlog_Data', --逻辑名
size=10mb, --初始大小
filegrowth=%, --文件增长
maxsize=1024mb, --最大值
filename=N'F:\Work\SQL\LawyerBlog_Data.mdf'--存放路径(包含文件后缀名)
),
filegroup ArticleData --Article文件组(表创建到不同的文件组里面可以分担压力)
(
name='LawyerBlog_Data_Article',
size=10mb,
filegrowth=%,
maxsize=1024mb,
filename=N'E:\SQL\LawyerBlog_Data_Article.ndf'
)
log on --日记
(
name='LawyerBlog_Log1',
size=5mb,
filegrowth=%,
filename=N'F:\Work\SQL\LawyerBlog_log1.ldf'
),
(
name='LawyerBlog_Log2',
size=5mb,
filegrowth=%,
filename=N'E:\SQL\LawyerBlog_log2.ldf'
)
go

扩展:

如果是普通用户要有ALTER SETTINGS权限才能运行sp_configure(一般管理员才有这个权限)

向数据库添加数据文件或日志文件

  1. 连接到数据库引擎。

  2. 在标准菜单栏上,单击“新建查询”

  3. 将以下示例复制并粘贴到查询窗口中,然后单击“执行”。此实例向数据库添加由两个文件组成的文件组。此示例在 AdventureWorks2012 数据库中创建文件组 Test1FG1,然后将两个 5MB 的文件添加到该文件组。

    USE master
    GO
    ALTER DATABASE AdventureWorks2012
    ADD FILEGROUP Test1FG1;
    GO
    ALTER DATABASE AdventureWorks2012
    ADD FILE
    (
    NAME = test1dat3,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\t1dat3.ndf',
    SIZE = 5MB,
    MAXSIZE = 100MB,
    FILEGROWTH = 5MB
    ),
    (
    NAME = test1dat4,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\t1dat4.ndf',
    SIZE = 5MB,
    MAXSIZE = 100MB,
    FILEGROWTH = 5MB
    )
    TO FILEGROUP Test1FG1;
    GO