在 Linux 上安装 SQL Server 机器学习服务(Python 和 R)Install SQL Server Machine Learning Services (Python and R) on Linux

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) - Linux适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) - Linux

本文指导你在 Linux 上安装 SQL Server 机器学习服务This article guides you in the installation of SQL Server Machine Learning Services on Linux. 可使用机器学习服务在数据库中执行 Python 和 R 脚本。Python and R scripts can be executed in-database using Machine Learning Services.

备注

默认情况下,机器学习服务安装在 SQL Server 大数据群集上。Machine Learning Services is installed by default on SQL Server Big Data Clusters. 有关详细信息,请参阅在大数据群集上使用机器学习服务(Python 和 R)For more information, see Use Machine Learning Services (Python and R) on Big Data Clusters

安装前清单Pre-install checklist

  • 安装 Linux 上的 SQL Server,然后验证安装。Install SQL Server on Linux and verify the installation.

  • 查看 SQL Server Linux 存储库中的 Python 和 R 扩展。Check the SQL Server Linux repositories for the Python and R extensions. 如果已为数据库引擎安装配置了源存储库,则可以使用相同的存储库注册运行 mssql-mlservices 包安装命令 。If you already configured source repositories for the database engine install, you can run the mssql-mlservices package install commands using the same repo registration.

    你可以安装 SQL Server on Red Hat Enterprise Linux (RHEL)、SUSE Linux Enterprise Server (SLES) 和 Ubuntu。You can install SQL Server on Red Hat Enterprise Linux (RHEL), SUSE Linux Enterprise Server (SLES), and Ubuntu. 有关详细信息,请参阅 Linux 上的 SQL Server 安装指南中的“受支持的平台”部分For more information, see the Supported platforms section in the Installation guidance for SQL Server on Linux.

  • (仅限 R)Microsoft R Open (MRO) 为 SQL Server 中的 R 功能提供 R 基础发行版,要使用通过机器学习服务安装的 RevoScaleR、MicrosoftML 和其他 R 包,则该基础发行版是一种先决条件。(R only) Microsoft R Open (MRO) provides the base R distribution for the R feature in SQL Server and is a prerequisite for using RevoScaleR, MicrosoftML, and other R packages installed with Machine Learning Services.

    • 所需版本为 MRO 3.5.2。The required version is MRO 3.5.2.
    • 从以下两种安装 MRO 的方法中进行选择:Choose from the following two approaches to install MRO:
      • 从 MRAN 下载 MRO tarball,解压缩并运行其 install.sh 脚本。Download the MRO tarball from MRAN, unpack it, and run its install.sh script. 如果要使用此方法,可以按照 MRAN 上的安装说明进行操作。You can follow the installation instructions on MRAN if you want this approach.
      • 如下所述注册 packages.microsoft.com 存储库以安装 MRO 发行版:microsoft-r-open-mro 和 microsoft-r-open-mkl。Register the packages.microsoft.com repo as described below to install the MRO distribution: microsoft-r-open-mro and microsoft-r-open-mkl.
    • 若要了解如何安装 MRO,请参阅下面的安装部分。See the installation sections below for how to install MRO.
  • 应安装用于运行 T-SQL 命令的工具。You should have a tool for running T-SQL commands.

    • 你可使用免费的 Azure Data Studio 数据库工具,该工具在 Linux、Windows 和 macOS 上运行。You can use Azure Data Studio, a free database tool that runs on Linux, Windows, and macOS.

包列表Package list

在已连接 Internet 的设备上,将使用针对每个操作系统的包安装程序独立于数据库引擎下载和安装相应的包。On an internet-connected device, packages are downloaded and installed independently of the database engine using the package installer for each operating system. 下表介绍了所有可用包,但对于 R 和 Python,需要指定提供完整功能安装或最小功能安装的包。The following table describes all available packages, but for R and Python, you specify packages that provide either the full feature installation or the minimum feature installation.

可用安装包:Available installation packages:

包名称Package name 适用对象Applies-to 说明Description
mssql-server-extensibilitymssql-server-extensibility AllAll 用于运行 Python 和 R 的扩展性框架。Extensibility framework used to run Python and R.
microsoft-openmpimicrosoft-openmpi Python、RPython, R Rev* 库在 Linux 上进行并行化所使用的消息传递接口。Message passing interface used by the Rev* libraries for parallelization on Linux.
mssql-mlservices-pythonmssql-mlservices-python PythonPython Anaconda 和 Python 的开放源代码发行版。Open-source distribution of Anaconda and Python.
mssql-mlservices-mlm-pymssql-mlservices-mlm-py PythonPython 完全安装 。Full install. 提供用于图像特征化和文本情绪分析的 revoscalepy、microsoftml、预先训练的模型。Provides revoscalepy, microsoftml, pre-trained models for image featurization and text sentiment analysis.
mssql-mlservices-packages-pymssql-mlservices-packages-py PythonPython 最小安装 。Minimum install. 提供 revoscalepy 和 microsoftml。Provides revoscalepy and microsoftml.
不包括预先训练的模型。Excludes pre-trained models.
microsoft-r-open*microsoft-r-open* RR 由三个包组成的 R 的开放源代码发行版。Open-source distribution of R, composed of three packages.
mssql-mlservices-mlm-rmssql-mlservices-mlm-r RR 完全安装 。Full install. 提供:用于图像特征化和文本情绪分析的 RevoScaleR、MicrosoftML、sqlRUtils、olapR、预先训练的模型。Provides: RevoScaleR, MicrosoftML, sqlRUtils, olapR, pre-trained models for image featurization and text sentiment analysis.
mssql-mlservices-packages-rmssql-mlservices-packages-r RR 最小安装 。Minimum install. 提供 RevoScaleR、sqlRUtils、MicrosoftML、olapR。Provides RevoScaleR, sqlRUtils, MicrosoftML, olapR.
不包括预先训练的模型。Excludes pre-trained models.

在 RHEL 上安装Install on RHEL

请按照以下步骤在 Red Hat Enterprise Linux (RHEL) 上安装 SQL Server 机器学习服务。Follow the steps below to install SQL Server Machine Learning Services on Red Hat Enterprise Linux (RHEL).

在 RHEL 上安装 MROInstall MRO on RHEL

以下命令可注册提供 MRO 的存储库。The following commands register the repository providing MRO. 注册后,用于安装其他 R 包的命令(如 mssql-mlservices-mml-r)将自动包含作为包依赖项的 MRO。Post-registration, the commands for installing other R packages, such as mssql-mlservices-mml-r, will automatically include MRO as a package dependency.

# Import the Microsoft repository key

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

# Set the location of the package repo at the "prod" directory
# The following command is for version 7.x
# For 6.x, replace 7 with 6 to get that version
rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

# Update packages on your system (optional)
yum update

Python 和 R 的安装选项:Installation Options for Python and R:

  • 根据你的要求安装语言支持(一种或多种语言)。Install language support based on your requirements (single or multiple languages).
  • 完全安装 提供了所有可用功能,包括经过预先训练的机器学习模型。The full installation provides all available features the including pre-trained machine learning models.
  • 最小安装 不包括模型,但仍具备所有功能。The minimal installation excludes the models but still has all of the functionality.

提示

如果可以,请在安装之前运行 yum clean all 以刷新系统中的包。If possible, run yum clean all to refresh packages on the system prior to installation.

完全安装Full installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • 扩展(Python、R)Extensions (Python, R)
  • 机器学习库Machine learning libraries
  • 用于 Python 和 R 的预训练模型Pre-Trained models for Python and R
# Install as root or sudo
# Add everything (all R, Python)
# Be sure to include -9.4.7* in mlsservices package names
sudo yum install mssql-mlservices-mlm-py-9.4.7*
sudo yum install mssql-mlservices-mlm-r-9.4.7*

最小安装Minimum installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • 核心 Revo* 库Core Revo* libraries
  • 机器学习库Machine learning libraries
# Install as root or sudo
# Minimum install of R, Python extensions
# Be sure to include -9.4.6* in mlsservices package names
sudo yum install mssql-mlservices-packages-py-9.4.7*
sudo yum install mssql-mlservices-packages-r-9.4.7*

在 Ubuntu 上安装Install on Ubuntu

请按照以下步骤在 Ubuntu 上安装 SQL Server 机器学习服务。Follow the steps below to install SQL Server Machine Learning Services on Ubuntu.

在 Ubuntu 上安装 MROInstall MRO on Ubuntu

以下命令可注册提供 MRO 的存储库。The following commands register the repository providing MRO. 注册后,用于安装其他 R 包的命令(如 mssql-mlservices-mml-r)将自动包含作为包依赖项的 MRO。Post-registration, the commands for installing other R packages, such as mssql-mlservices-mml-r, will automatically include MRO as a package dependency.

# Install as root
sudo su

# Optionally, if your system does not have the https apt transport option
apt-get install apt-transport-https

# Set the location of the package repo the "prod" directory containing the distribution.
# This example specifies 16.04. Replace with 14.04 if you want that version
wget https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb

# Register the repo
dpkg -i packages-microsoft-prod.deb

# Update packages on your system (required), including MRO installation
sudo apt-get update

Python 和 R 的安装选项:Installation Options for Python and R:

  • 根据你的要求安装语言支持(一种或多种语言)。Install language support based on your requirements (single or multiple languages).
  • 完全安装 提供了所有可用功能,包括经过预先训练的机器学习模型。The full installation provides all available features the including pre-trained machine learning models.
  • 最小安装 不包括模型,但仍具备所有功能。The minimal installation excludes the models but still has all of the functionality.

提示

如果可以,请在安装之前运行 apt-get update 以刷新系统中的包。If possible, run apt-get update to refresh packages on the system prior to installation.

完全安装Full installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • Python 扩展Python extensions
  • R 扩展R extensions
  • 机器学习库Machine learning libraries
  • 用于 Python 和 R 的预训练模型Pre-trained models for Python and R
# Install as root or sudo
# Add everything (all R, Python)
# There is no asterisk in this full install
sudo apt-get install mssql-mlservices-mlm-py 
sudo apt-get install mssql-mlservices-mlm-r 

最小安装Minimum installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • 核心 Revo* 库Core Revo* libraries
  • 机器学习库Machine learning libraries
# Install as root or sudo
# Minimum install of R, Python
# No asterisk
sudo apt-get install mssql-mlservices-packages-py
sudo apt-get install mssql-mlservices-packages-r

在 SLES 上安装Install on SLES

请按照以下步骤在 SUSE Linux Enterprise Server (SLES) 上安装 SQL Server 机器学习服务。Follow the steps below to install SQL Server Machine Learning Services on SUSE Linux Enterprise Server (SLES).

在 SLES 上安装 MROInstall MRO on SLES

以下命令可注册提供 MRO 的存储库。The following commands register the repository providing MRO. 注册后,用于安装其他 R 包的命令(如 mssql-mlservices-mml-r)将自动包含作为包依赖项的 MRO。Post-registration, the commands for installing other R packages, such as mssql-mlservices-mml-r, will automatically include MRO as a package dependency.

# Install as root
sudo su

# Set the location of the package repo at the "prod" directory containing the distribution
# This example is for SLES12, the only supported version of SLES in Machine Learning Server
zypper ar -f https://packages.microsoft.com/sles/12/prod packages-microsoft-com

# Update packages on your system (optional)
zypper update

Python 和 R 的安装选项:Installation Options for Python and R:

  • 根据你的要求安装语言支持(一种或多种语言)。Install language support based on your requirements (single or multiple languages).
  • 完全安装 提供了所有可用功能,包括经过预先训练的机器学习模型。The full installation provides all available features the including pre-trained machine learning models.
  • 最小安装 不包括模型,但仍具备所有功能。The minimal installation excludes the models but still has all of the functionality.

完全安装Full installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • Python 和 R 的扩展Extensions for Python and R
  • 机器学习库Machine learning libraries
  • 用于 Python 和 R 的预训练模型Pre-trained models for Python and R
# Install as root or sudo
# Add everything (all R, Python)
sudo zypper install mssql-mlservices-mlm-py
sudo zypper install mssql-mlservices-mlm-r

最小安装Minimum installation

包括:Includes:

  • 开放源代码 PythonOpen-source Python
  • 开放源代码 ROpen-source R
  • 可扩展性框架Extensibility framework
  • Microsoft-openmpiMicrosoft-openmpi
  • 核心 Revo* 库Core Revo* libraries
  • 机器学习库Machine learning libraries
# Install as root or sudo
# Minimum install of R, Python extensions
sudo zypper install mssql-mlservices-packages-py
sudo zypper install mssql-mlservices-packages-r

安装后配置(必需)Post-install config (required)

其他配置主要通过 mssql-conf 工具完成。Additional configuration is primarily through the mssql-conf tool.

  1. 包安装完成后,运行 mssql-conf 安装程序,按照提示设置 SA 密码并选择版本。After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition. 仅在尚未在 Linux 上配置 SQL Server 的情况下执行此步骤。Perform this step only if you have not configured SQL Server on Linux yet.

    sudo /opt/mssql/bin/mssql-conf setup
    
  2. 接受开放源代码 Python 和 R 扩展的许可协议。Accept the licensing agreements for open-source Python and R extensions. 使用以下命令:Use the following command:

    # Run as SUDO or root
    # Use set + EULA 
    sudo /opt/mssql/bin/mssql-conf set EULA accepteulaml Y
    

    安装程序检测到 mssql-mlservices 包,并在运行 mssql-conf setup 时提示接受 EULA(如果以前未接受)。Setup detects the mssql-mlservices packages and prompts for EULA acceptance (if not previously accepted) when mssql-conf setup is run. 有关 EULA 参数的详细信息,请参阅使用 mssql-conf 工具配置 SQL ServerFor more information about EULA parameters, see Configure SQL Server with the mssql-conf tool.

  3. 启用出站网络访问。Enable outbound network access. 默认情况下,出站网络访问处于禁用状态。Outbound network access is disabled by default. 若要启用出站请求,请使用 mssql-conf 工具设置“outboundnetworkaccess”布尔属性。To enable outbound requests, set the "outboundnetworkaccess" Boolean property using the mssql-conf tool. 有关详细信息,请参阅使用 mssql-conf 配置 Linux 上的 SQL ServerFor more information, see Configure SQL Server on Linux with mssql-conf.

    # Run as SUDO or root
    # Enable outbound requests over the network
    sudo /opt/mssql/bin/mssql-conf set extensibility outboundnetworkaccess 1
    
  4. (仅适用于 R 功能集成)请设置“MKL_CBWR”环境变量,以确保从 Intel 数学核心函数库 (MKL) 计算得到一致的输出结果For R feature integration only, set the MKL_CBWR environment variable to ensure consistent output from Intel Math Kernel Library (MKL) calculations.

    • 在用户的主目录中编辑或创建文件 .bash_profile,并将行 export MKL_CBWR="AUTO" 添加到该文件中。Edit or create a file .bash_profile in your user home directory, adding the line export MKL_CBWR="AUTO" to the file.

    • 通过在 bash 命令提示符处键入 source .bash_profile 来执行此文件。Execute this file by typing source .bash_profile at a bash command prompt.

  5. 重启 SQL Server Launchpad 服务和数据库引擎实例,以从 INI 文件中读取更新后的值。Restart the SQL Server Launchpad service and the database engine instance to read the updated values from the INI file. 修改与扩展性有关的设置时,系统将显示一条通知消息。A notification message is displayed when an extensibility-related setting is modified.

    systemctl restart mssql-launchpadd
    
    systemctl restart mssql-server.service
    
  6. 使用 Azure Data Studio 或使用运行 Transact-SQL 的其他工具(如 SQL Server Management Studio,仅限 Windows)来启用外部脚本执行。Enable external script execution using Azure Data Studio or another tool like SQL Server Management Studio (Windows only) that runs Transact-SQL.

    EXEC sp_configure 'external scripts enabled', 1 
    RECONFIGURE WITH OVERRIDE 
    
  7. 再次重启 Launchpad 服务。Restart the Launchpad service again.

验证安装Verify installation

可在 /opt/mssql/mlservices/libraries/RServer 中找到 R 库(MicrosoftML、RevoScaleR 及其他库)。R libraries (MicrosoftML, RevoScaleR, and others) can be found at /opt/mssql/mlservices/libraries/RServer.

可在 /opt/mssql/mlservices/libraries/PythonServer 中找到 Python 库(microsoftml 和 revoscalepy)。Python libraries (microsoftml and revoscalepy) can be found at /opt/mssql/mlservices/libraries/PythonServer.

验证安装:To validate installation:

  • 运行 T-SQL 脚本,该脚本执行使用查询工具调用 Python 或 R 的系统存储过程。Run a T-SQL script that executes a system stored procedure invoking Python or R using a query tool.

  • 请执行以下 SQL 命令以测试 SQL Server 中的 R 执行。Execute the following SQL command to test R execution in SQL Server. 遇到错误?Errors? 尝试重启服务 sudo systemctl restart mssql-server.serviceTry a service restart, sudo systemctl restart mssql-server.service.

    EXEC sp_execute_external_script   
    @language =N'R', 
    @script=N' 
    OutputDataSet <- InputDataSet', 
    @input_data_1 =N'SELECT 1 AS hello' 
    WITH RESULT SETS (([hello] int not null)); 
    GO 
    
  • 请执行以下 SQL 命令以测试 SQL Server 中的 Python 执行。Execute the following SQL command to test Python execution in SQL Server.

    EXEC sp_execute_external_script  
    @language =N'Python', 
    @script=N' 
    OutputDataSet = InputDataSet; 
    ', 
    @input_data_1 =N'SELECT 1 AS hello' 
    WITH RESULT SETS (([hello] int not null)); 
    GO 
    

无人参与的安装Unattended installation

对数据库引擎使用无人参与安装,添加 mssql-mlservices 和 EULA 的包。Using the unattended install for the Database Engine, add the packages for mssql-mlservices and EULAs.

对开放源代码 R 和 Python 发行版使用其中某个 mlservices 特定的 EULA 参数:Use one of the mlservices-specific EULA parameters for the open-source R and Python distributions:

sudo /opt/mssql/bin/mssql-conf setup accept-eula-ml

使用 mssql-con 工具配置 Linux 上的 SQL Server 中记录了完整的 EULA。The complete EULA is documented at Configure SQL Server on Linux with the mssql-conf tool.

脱机安装Offline installation

有关安装包的步骤,请按照脱机安装说明进行操作。Follow the Offline installation instructions for steps on installing the packages. 找到下载网站,然后使用下面的包列表下载特定的包。Find your download site, and then download specific packages using the package list below.

提示

一些包管理工具提供可帮助确定包依赖项的命令。Several of the package management tools provide commands that can help you determine package dependencies. 对于 yum,请使用 sudo yum deplist [package]For yum, use sudo yum deplist [package]. 对于 Ubuntu,请在使用 sudo apt-get install --reinstall --download-only [package name] 之后使用 dpkg -I [package name].debFor Ubuntu, use sudo apt-get install --reinstall --download-only [package name] followed by dpkg -I [package name].deb.

下载站点Download site

通过 https://packages.microsoft.com/ 下载包。Download packages from https://packages.microsoft.com/. Python 和 R 的所有 mlservices 包都与数据库引擎包位于相同位置。All of the mlservices packages for Python and R are colocated with database engine package. mlservices 包的基础版本为 9.4.6。Base version for the mlservices packages is 9.4.6. 回想一下,microsoft-r-open 包位于其他存储库中。Recall that the microsoft-r-open packages are in a different repository.

RHEL/7 路径RHEL/7 paths

程序包Package 下载位置Download location
mssql/mlservices 包mssql/mlservices packages https://packages.microsoft.com/rhel/7/mssql-server-2019/
microsoft-r-open 包microsoft-r-open packages https://packages.microsoft.com/rhel/7/prod/

Ubuntu/16.04 路径Ubuntu/16.04 paths

程序包Package 下载位置Download location
mssql/mlservices 包mssql/mlservices packages https://packages.microsoft.com/ubuntu/16.04/mssql-server-2019/pool/main/m/
microsoft-r-open 包microsoft-r-open packages https://packages.microsoft.com/ubuntu/16.04/prod/pool/main/m/

SLES/12 路径SLES/12 paths

程序包Package 下载位置Download location
mssql/mlservices 包mssql/mlservices packages https://packages.microsoft.com/sles/12/mssql-server-2019/
microsoft-r-open 包microsoft-r-open packages https://packages.microsoft.com/sles/12/prod/

选择要使用的扩展,下载特定语言所需的包。Select extensions you want to use and download the packages necessary for a specific language. 文件名在后缀中包含平台信息。The filenames include platform information in the suffix.

包列表Package list

根据想要使用的扩展,下载特定语言所需的包。Depending on which extensions you want to use, download the packages necessary for a specific language. 精确的文件名在后缀中包含平台信息,但以下文件名应足够接近,可用于确定要获取的文件。Exact filenames include platform information in the suffix, but the file names below should be close enough for you to determine which files to get.

# Core packages 
mssql-server-15.0.1000
mssql-server-extensibility-15.0.1000

# R
microsoft-openmpi-3.0.0
microsoft-r-open-mkl-3.5.2
microsoft-r-open-mro-3.5.2
mssql-mlservices-packages-r-9.4.7.64
mssql-mlservices-mlm-r-9.4.7.64


# Python
microsoft-openmpi-3.0.0
mssql-mlservices-python-9.4.7.64
mssql-mlservices-packages-py-9.4.7.64
mssql-mlservices-mlm-py-9.4.7.64

后续步骤Next steps

Python 开发人员可以通过以下教程了解如何将 Python 与 SQL Server 一起使用:Python developers can learn how to use Python with SQL Server by following these tutorials:

R 开发人员可以开始使用一些简单的示例,并了解 R 如何与 SQL Server 协同工作的基础知识。R developers can get started with some simple examples, and learn the basics of how R works with SQL Server. 有关下一步,请参阅以下链接:For your next step, see the following links: