为 SQL 服务器 Linux 版本安装 CLR

Install a CLR for SQL Server Linux edition

我有一个 C# 文件,可以在 Windows 上完美地编译和安装。我可以使用以下命令在 Linux 上使用 mcs 编译它而不会出错:

mcs -reference:System.Data.dll -target:library -out:tests/RegEx.dll tests/regex.cs

我已验证该文件在与源相同的目录中存在并且具有 775 权限。接下来我尝试使用以下命令将它安装在服务器上:

/opt/mssql-tools/bin/sqlcmd -P Password12! -S localhost -U SA -Q "CREATE ASSEMBLY Regex from '$TRAVIS_BUILD_DIR/tests/RegEx.dll' WITH PERMISSION_SET = SAFE"

但是,我收到错误消息:

CREATE ASSEMBLY failed because it could not open the physical file '/home/travis/build/Beakerboy/sqlsrv/tests/RegEx.dll': 2(The system cannot find the file specified.).

我担心路径可能需要是“Windows 格式”,并发现一个建议甚至可能需要“C:\”。我接下来尝试了这个,但仍然找不到文件:

/opt/mssql-tools/bin/sqlcmd -P Password12! -S localhost -U SA -Q "CREATE ASSEMBLY Regex from 'c:\home\travis\build\Beakerboy\sqlsrv\tests\RegEx.dll' WITH PERMISSION_SET = SAFE"

有人对如何格式化它有什么建议吗?我的服务器安装的完整 travis 脚本在 GitHub

根据我的记忆,除非在过去的一年中发生了某些变化,否则我相信您只能使用十六进制字节/VARBINARY 选项创建程序集,而不是从文件系统。到目前为止,我无法找到它的文档,但我确实记得在 SQL 服务器 Linux 出来时读过它(限制是没有从文件系统加载,只有 SAFE 程序集,等)。(O.P。能够从 DLL 中加载它,所以有些东西确实发生了变化,或者我记错了)。

如果在 Windows 上编译,我创建了一个命令行实用程序来将 DLL 转换为正确的格式:BinaryFormatter。我一直想更新项目,以便它在 Linux 上本地运行,但还没有解决这个问题(如果有人有时间,可能会在那里使用一些帮助 😸)。

无论如何,如果您需要 RegEx 函数(以及更多),您可以通过下载并安装 SQL# library that I created as it does work on SQL Server on Linux. It has most, if not all, of the RegEx methods available in .NET, plus a few extra. And, it handles security properly in that all assemblies are signed, thus not requiring either enabling TRUSTWORTHY (a bad practice) 或禁用 [​​=13=].[=16= 来更轻松地完成此操作]

@David Browne 有正确的建议,尝试不同的文件路径。

这是我的 Dockerfile:

FROM mcr.microsoft.com/mssql/server:2019-CU10-ubuntu-20.04

RUN set -eux; \
    curl -fSL "wget https://github.com/Beakerboy/drupal-sqlsrv-regex/releases/download/1.0/RegEx.dll"; \
    mv RegEx.dll /var/opt/mssql/data/; \

CLR 加载了:

sqlcmd -P Password12! -S localhost -U SA -d mydrupalsite -Q "EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'clr strict security', 0; RECONFIGURE; EXEC sp_configure 'clr enable', 1; RECONFIGURE"

sqlcmd -P Password12! -S localhost -U SA -d mydrupalsite -Q "CREATE ASSEMBLY Regex from '/var/opt/mssql/data/RegEx.dll' WITH PERMISSION_SET = SAFE"

sqlcmd -P Password12! -S localhost -U SA -d mydrupalsite -Q "CREATE FUNCTION dbo.REGEXP(@pattern NVARCHAR(100), @matchString NVARCHAR(100)) RETURNS bit EXTERNAL NAME Regex.RegExCompiled.RegExCompiledMatch"