在 Linux 上安裝 SQL Server 命令列工具 sqlcmd 和 bcpInstall sqlcmd and bcp the SQL Server command-line tools on Linux

適用於: 是SQL Server (僅限 Linux) 否Azure SQL Database 否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 中將 /opt/mssql-tools/bin/ 新增至您的 PATH 環境變數。Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    若要讓登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改您在 ~/.bash_profile 檔案中的 PATHTo 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
    

    若要讓互動式/非登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改 ~/.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 中將 /opt/mssql-tools/bin/ 新增至您的 PATH 環境變數。Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    若要讓登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改您在 ~/.bash_profile 檔案中的 PATHTo 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
    

    若要讓互動式/非登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改 ~/.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 中將 /opt/mssql-tools/bin/ 新增至您的 PATH 環境變數。Optional: Add /opt/mssql-tools/bin/ to your PATH environment variable in a bash shell.

    若要讓登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改您在 ~/.bash_profile 檔案中的 PATHTo 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
    

    若要讓互動式/非登入工作階段的 Bash Shell 可存取 sqlcmd/bcp,請使用下列命令修改 ~/.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.com 中。These 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. 如果您使用不同電腦來下載套件,將套件移至 Linux 電腦的其中一種方式是使用 scp 命令。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.