在 Linux 上安装 sqlcmd 和 bcp SQL Server 命令行工具Install sqlcmd and bcp the SQL Server command-line tools on Linux

适用对象:是SQL Server(仅 Linux)否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库APPLIES TO: yesSQL Server (Linux only) noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

通过以下步骤安装命令行工具、Microsoft ODBC 驱动程序及其依赖项。The following steps install the command-line tools, Microsoft ODBC drivers, and their dependencies. mssql-tools 包包含:The mssql-tools package contains:

  • sqlcmd:命令行查询实用工具。sqlcmd: Command-line query utility.
  • bcp:批量导入-导出实用工具。bcp: Bulk import-export utility.

为你的平台安装工具:Install the tools for your platform:

本文介绍如何安装命令行工具。This article describes how to install the command-line tools. 如果正在寻找有关如何使用 sqlcmdbcp 的示例,请参阅本主题末尾的链接If you are looking for examples of how to use sqlcmd or bcp, see the links at the end of this topic.

在 RHEL 7 上安装工具Install tools on RHEL 7

通过以下步骤在 Red Hat Enterprise Linux 上安装 mssql-toolsUse the following steps to install the mssql-tools on Red Hat Enterprise Linux.

  1. 进入超级用户模式。Enter superuser mode.

    sudo su
    
  2. 下载 Microsoft Red Hat 存储库配置文件。Download the Microsoft Red Hat repository configuration file.

    curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
    
  3. 退出超级用户模式。Exit superuser mode.

    exit
    
  4. 如果安装了早期版本的 mssql-tools,请删除所有旧的 unixODBC 包。If you had a previous version of mssql-tools installed, remove any older unixODBC packages.

    sudo yum remove mssql-tools unixODBC-utf16-devel
    
  5. 运行以下命令,以使用 unixODBC 开发人员包安装 mssql-toolsRun the following commands to install mssql-tools with the unixODBC developer package.

    sudo yum install mssql-tools unixODBC-devel
    

    备注

    若要将 mssql-tools 更新至最新版本,请运行以下命令:To update to the latest version of mssql-tools run the following commands:

    sudo yum check-update
    sudo yum update mssql-tools
    
  6. 可选:向 bash shell 中的 PATH 环境变量添加 /opt/mssql-tools/bin/Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    要使 sqlcmd/bcp 能从登陆会话的 bash shell 进行访问,请使用下列命令修改 ~/.bash_profile 文件中的 PATH :To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    

    若要使 sqlcmd/bcp 能从交互式/非登录会话的 bash shell 进行访问,请使用以下命令修改 ~/.bashrc 文件中的 PATHTo make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    

在 Ubuntu 16.04 上安装工具Install tools on Ubuntu 16.04

通过以下步骤在 Ubuntu 上安装 mssql-toolsUse the following steps to install the mssql-tools on Ubuntu.

  1. 导入公共存储库 GPG 密钥。Import the public repository GPG keys.

    curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
    
  2. 注册 Microsoft Ubuntu 存储库。Register the Microsoft Ubuntu repository.

    curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
    
  3. 更新源列表,并使用 unixODBC 开发人员包运行安装命令。Update the sources list and run the installation command with the unixODBC developer package.

    sudo apt-get update 
    sudo apt-get install mssql-tools unixodbc-dev
    

    备注

    若要将 mssql-tools 更新至最新版本,请运行以下命令 :To update to the latest version of mssql-tools run the following commands:

    sudo apt-get update 
    sudo apt-get install mssql-tools 
    
  4. 可选:向 bash shell 中的 PATH 环境变量添加 /opt/mssql-tools/bin/Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    要使 sqlcmd/bcp 能从登陆会话的 bash shell 进行访问,请使用下列命令修改 ~/.bash_profile 文件中的 PATH :To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    

    若要使 sqlcmd/bcp 能从交互式/非登录会话的 bash shell 进行访问,请使用以下命令修改 ~/.bashrc 文件中的 PATHTo make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    

在 SLES 12 上安装工具Install tools on SLES 12

通过以下步骤在 SUSE Linux Enterprise Server 上安装 mssql-toolsUse the following steps to install the mssql-tools on SUSE Linux Enterprise Server.

  1. 将 Microsoft SQL Server 存储库添加到 Zypper。Add the Microsoft SQL Server repository to Zypper.

    sudo zypper addrepo -fc https://packages.microsoft.com/config/sles/12/prod.repo 
    sudo zypper --gpg-auto-import-keys refresh
    
  2. 使用 unixODBC 开发人员包安装 mssql-toolsInstall mssql-tools with the unixODBC developer package.

    sudo zypper install mssql-tools unixODBC-devel
    

    备注

    若要将 mssql-tools 更新至最新版本,请运行以下命令:To update to the latest version of mssql-tools run the following commands:

    sudo zypper refresh
    sudo zypper update mssql-tools
    
  3. 可选:向 bash shell 中的 PATH 环境变量添加 /opt/mssql-tools/bin/Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    要使 sqlcmd/bcp 能从登陆会话的 bash shell 进行访问,请使用下列命令修改 ~/.bash_profile 文件中的 PATH :To make sqlcmd/bcp accessible from the bash shell for login sessions, modify your PATH in the ~/.bash_profile file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
    

    若要使 sqlcmd/bcp 能从交互式/非登录会话的 bash shell 进行访问,请使用以下命令修改 ~/.bashrc 文件中的 PATHTo make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:

    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    

在 macOS 上安装工具Install tools on macOS

macOS 上目前提供 sqlcmdbcp 的预览版。A preview of sqlcmd and bcp is now available on macOS. 有关详细信息,请参阅公告For more information, see the announcement.

如果尚未安装 Homebrew,请进行安装:Install Homebrew if you don't have it already:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

若要为 Mac El Capitan 和 Sierra 安装工具,请使用以下命令:To install the tools for Mac El Capitan and Sierra, use the following commands:

# brew untap microsoft/mssql-preview if you installed the preview version 
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install mssql-tools
#for silent install: 
#HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=y brew install mssql-tools

DockerDocker

如果在 Docker 容器中运行 SQL Server,则 SQL Server 命令行工具已包含在 SQL Server Linux 容器映像中。If you run SQL Server in a Docker container, the SQL Server command-line tools are already included in the SQL Server Linux container image. 如果使用交互式 bash shell 附加到正在运行的容器,则可以在本地运行这些工具。If you attach to a running container with an interactive bash shell, you can run the tools locally.

脱机安装Offline installation

如果 Linux 计算机不能访问之前部分中使用的在线存储库,则可以直接下载包文件。If your Linux machine does not have access to the online repositories used in the previous sections, you can download the package files directly. 这些包位于 Microsoft 存储库中,网址为 https://packages.microsoft.comThese packages are located in the Microsoft repository, https://packages.microsoft.com.

提示

如果通过之前部分中的步骤成功安装了包,则无需下载或者手动安装以下包。If you successfully installed with the steps in the previous sections, you do not need to download or manually install the package(s) below. 这仅适用于脱机的情况。This is only for the offline scenario.

  1. 首先,找到并复制适用于你的 Linux 分发版的 mssql-tools 包:First, locate and copy the mssql-tools package for your Linux distribution:

    Linux 分发版Linux distribution mssql-tools 包位置mssql-tools package location
    Red HatRed Hat https://packages.microsoft.com/rhel/7.3/prod
    SLESSLES https://packages.microsoft.com/sles/12/prod
    Ubuntu 16.04Ubuntu 16.04 https://packages.microsoft.com/ubuntu/16.04/prod/pool/main/m/mssql-tools
  2. 此外,找到并复制 msodbcsql 包,它是一个依赖项。Also locate and copy the msodbcsql package, which is a dependency. msodbcsql 包对 unixODBC-devel(Red Hat 和 SLES)或 unixodbc-dev (Ubuntu) 也具有依赖项。The msodbcsql package also has a dependency on either unixODBC-devel (Red Hat and SLES) or unixodbc-dev (Ubuntu). 下表列出了 msodbcsql 包的所在位置:The location of the msodbcsql packages are listed in the following table:

    Linux 分发版Linux distribution ODBC 包位置ODBC packages location
    Red HatRed Hat https://packages.microsoft.com/rhel/7.3/prod
    SLESSLES https://packages.microsoft.com/sles/12/prod
    Ubuntu 16.04Ubuntu 16.04 msodbcsqlmsodbcsql
    unixodbc-devunixodbc-dev
  3. 将下载的包移至 Linux 计算机Move the downloaded packages to your Linux machine. 如果使用了不同的计算机下载包,则可以通过“scp”命令将包移至你的 Linux 计算机 。If you used a different machine to download the packages, one way to move the packages to your Linux machine is with the scp command.

  4. 安装包:安装 mssql-toolsmsodbc 包。Install the and packages: Install the mssql-tools and msodbc packages. 如果遇到任何依赖项错误,请忽略,直到出现下一步操作。If you get any dependency errors, ignore them until the next step.

    平台Platform 包安装命令Package install commands
    Red HatRed Hat sudo yum localinstall msodbcsql-<version>.rpm
    sudo yum localinstall mssql-tools-<version>.rpm
    SLESSLES sudo zypper install msodbcsql-<version>.rpm
    sudo zypper install mssql-tools-<version>.rpm
    UbuntuUbuntu sudo dpkg -i msodbcsql_<version>.deb
    sudo dpkg -i mssql-tools_<version>.deb
  5. 解决缺少依赖项的问题:此时可能会出现缺少依赖项的情况。Resolve missing dependencies: You might have missing dependencies at this point. 如果没有,可以跳过此步骤。If not, you can skip this step. 在某些情况下,必须手动查找并安装这些依赖项。In some cases, you must manually locate and install these dependencies.

    对于 RPM 包,可通过以下命令检查必需的依赖项:For RPM packages, you can inspect the required dependencies with the following commands:

    rpm -qpR msodbcsql-<version>.rpm
    rpm -qpR mssql-tools-<version>.rpm
    

    对于 Debian 包,如果能够访问包含这些依赖项的已批准存储库,则最简单的解决办法是使用 apt-get 命令:For Debian packages, if you have access to approved repositories containing those dependencies, the easiest solution is to use the apt-get command:

    sudo apt-get -f install
    

    备注

    此命令还会完成 SQL Server 包的安装。This command completes the installation of the SQL Server packages as well.

    如果此命令对 Debian 包不起作用,可通过以下命令检查必需的依赖项:If this does not work for your Debian package, you can inspect the required dependencies with the following commands:

    dpkg -I msodbcsql_<version>_amd64.deb | grep "Depends:"
    dpkg -I mssql-tools_<version>_amd64.deb | grep "Depends:"
    

后续步骤Next steps

有关如何使用 sqlcmd 连接到 SQL Server 并创建数据库的示例,请参阅以下快速入门之一:For an example of how to use sqlcmd to connect to SQL Server and create a database, see one of the following quickstarts:

有关如何使用 bcp 批量导入和导出数据的示例,请参阅将数据批量复制到 Linux 上的 SQL ServerFor an example of how to use bcp to bulk import and export data, see Bulk copy data to SQL Server on Linux.