debian/ubuntu安裝mssql

添加源:linux

debian源:deb [arch=amd64] https://packages.microsoft.com/debian/10/prod buster mainios

ubuntu源:deb [arch=amd64] https://packages.microsoft.com/ubuntu/19.04/prod disco maingit

apt install multiarch-supportgithub

wget http://ftp.cn.debian.org/debian/pool/main/j/jemalloc/libjemalloc1_3.6.0-3_amd64.debweb

wget http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u11_amd64.debsql

dpkg -i libjemalloc1_3.6.0-3_amd64.deb libssl1.0.0_1.0.1t-1+deb8u11_amd64.debshell

 

安裝 SQL ServerInstall SQL Server

要在 Ubuntu 上配置 SQL Server,請在終端中運行如下命令以安裝 mssql-server 包  。To configure SQL Server on Ubuntu, run the following commands in a terminal to install the mssql-server package.數據庫

  1. 導入公共存儲庫 GPG 密鑰:Import the public repository GPG keys:ubuntu

    bash
    wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
  2. 註冊 Microsoft SQL Server Ubuntu 存儲庫:Register the Microsoft SQL Server Ubuntu repository:windows

    bash
    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2017.list)" 

    提示

    若是想試用 SQL Server 2019,需改成註冊預覽版 (2019) 存儲庫  。If you want to try SQL Server 2019 , you must instead register the Preview (2019) repository. 使用如下命令安裝 SQL Server 2019:Use the following command for SQL Server 2019 installations:

    bash
    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)" 
  3. 運行如下命令以安裝 SQL Server:Run the following commands to install SQL Server:

    bash
    sudo apt-get update
    sudo apt-get install -y mssql-server
  4. 包安裝完成後,運行 mssql-conf setup,按照提示設置 SA 密碼並選擇版本。After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition.

    bash
    sudo /opt/mssql/bin/mssql-conf setup

    提示

    如下 SQL Server 2017 版本是免費提供許可的:Evaluation、Developer 和 Express 版。The following SQL Server 2017 editions are freely licensed: Evaluation, Developer, and Express.

    備註

    請確保爲 SA 賬戶指定強密碼(最少 8 個字符,包括大寫和小寫字母、十進制數字和/或非字母數字符號)。Make sure to specify a strong password for the SA account (Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols).

  5. 完成配置後,驗證服務是否正在運行:Once the configuration is done, verify that the service is running:

    bash
    systemctl status mssql-server --no-pager
  6. 若是計劃遠程鏈接,可能還須要在防火牆上打開 SQL Server TCP 端口(默認值爲 1433)。If you plan to connect remotely, you might also need to open the SQL Server TCP port (default 1433) on your firewall.

此時,SQL Server 已在 Ubuntu 計算機上運行,隨時可使用!At this point, SQL Server is running on your Ubuntu machine and is ready to use!

安裝 SQL ServerInstall SQL Server

要在 Ubuntu 上配置 SQL Server,請在終端中運行如下命令以安裝 mssql-server 包  。To configure SQL Server on Ubuntu, run the following commands in a terminal to install the mssql-server package.

  1. 導入公共存儲庫 GPG 密鑰:Import the public repository GPG keys:

    bash
    wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
  2. 爲 SQL Server 2019(預覽版)註冊 Microsoft SQL Server Ubuntu 存儲庫:Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019 preview:

    bash
    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)" 
  3. 運行如下命令,安裝 SQL Server:Run the following commands to install SQL Server:

    bash
    sudo apt-get update
    sudo apt-get install -y mssql-server
  4. 包安裝完成後,運行 mssql-conf setup,按照提示設置 SA 密碼並選擇版本。After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition.

    bash
    sudo /opt/mssql/bin/mssql-conf setup

    備註

    請確保爲 SA 賬戶指定強密碼(最少 8 個字符,包括大寫和小寫字母、十進制數字和/或非字母數字符號)。Make sure to specify a strong password for the SA account (Minimum length 8 characters, including uppercase and lowercase letters, base 10 digits and/or non-alphanumeric symbols).

  5. 完成配置後,驗證服務是否正在運行:Once the configuration is done, verify that the service is running:

    bash
    systemctl status mssql-server --no-pager
  6. 若是計劃遠程鏈接,可能還須要在防火牆上打開 SQL Server TCP 端口(默認值爲 1433)。If you plan to connect remotely, you might also need to open the SQL Server TCP port (default 1433) on your firewall.

此時,SQL Server 2019(預覽版)已在 Ubuntu 計算機上運行,隨時可使用!At this point, SQL Server 2019 preview is running on your Ubuntu machine and is ready to use!

安裝 SQL Server 命令行工具Install the SQL Server command-line tools

若要建立數據庫,則須要使用可在 SQL Server 上運行 Transact-SQL 語句的工具進行鏈接。To create a database, you need to connect with a tool that can run Transact-SQL statements on the SQL Server. 如下步驟安裝 SQL Server 命令行工具:sqlcmdbcpThe following steps install the SQL Server command-line tools: sqlcmd and bcp.

經過下列步驟在 Ubuntu 上安裝 mssql-tools  。Use the following steps to install the mssql-tools on Ubuntu.

  1. 導入公共存儲庫 GPG 密鑰。Import the public repository GPG keys.

    bash
    curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
  2. 註冊 Microsoft Ubuntu 存儲庫。Register the Microsoft Ubuntu repository.

    bash
    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.

    bash
    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:

    bash
    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:

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

    要使 sqlcmd/bcp 能從交互式/非登陸會話的 bash shell 進行訪問,請使用下列命令修改 ~/.bashrc 文件中的 PATH    :To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions, modify the PATH in the ~/.bashrc file with the following command:

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

本地鏈接Connect locally

如下步驟使用 sqlcmd  本地鏈接到新的 SQL Server 實例。The following steps use sqlcmd to locally connect to your new SQL Server instance.

  1. 使用 SQL Server 名稱 (-S),用戶名 (-U) 和密碼 (-P) 的參數運行 sqlcmd  。Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P). 在本教程中,用戶進行本地鏈接,所以服務器名稱爲 localhostIn this tutorial, you are connecting locally, so the server name is localhost. 用戶名爲 SA,密碼是在安裝過程當中爲 SA 賬戶提供的密碼。The user name is SA and the password is the one you provided for the SA account during setup.

    bash
    sqlcmd -S localhost -U SA -P '<YourPassword>' 

    提示

    能夠在命令行上省略密碼,以收到密碼輸入提示。You can omit the password on the command line to be prompted to enter it.

    提示

    若是之後決定進行遠程鏈接,請指定 -S  參數的計算機名稱或 IP 地址,並確保防火牆上的端口 1433 已打開。If you later decide to connect remotely, specify the machine name or IP address for the -S parameter, and make sure port 1433 is open on your firewall.

  2. 若是成功,應會顯示 sqlcmd  命令提示符:1>If successful, you should get to a sqlcmd command prompt: 1>.

  3. 若是鏈接失敗,先嚐試診斷錯誤消息中所述的問題。If you get a connection failure, first attempt to diagnose the problem from the error message. 而後查看鏈接故障排除建議Then review the connection troubleshooting recommendations.

建立和查詢數據Create and query data

下面各部分將逐步介紹如何使用 sqlcmd  新建數據庫、添加數據並運行簡單查詢。The following sections walk you through using sqlcmd to create a new database, add data, and run a simple query.

新建數據庫Create a new database

如下步驟建立一個名爲 TestDB 的新數據庫。The following steps create a new database named TestDB.

  1. 在 sqlcmd  命令提示符中,粘貼如下 Transact-SQL 命令以建立測試數據庫:From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

    SQL
    CREATE DATABASE TestDB 
  2. 在下一行中,編寫一個查詢以返回服務器上全部數據庫的名稱:On the next line, write a query to return the name of all of the databases on your server:

    SQL
    SELECT Name from sys.Databases 
  3. 前兩個命令沒有當即執行。The previous two commands were not executed immediately. 必須在新行中鍵入 GO 才能執行之前的命令:You must type GO on a new line to execute the previous commands:

    SQL
    GO

提示

若要詳細瞭解如何編寫 Transact-SQL 語句和查詢,請參閱教程:編寫 Transact-SQL 語句To learn more about writing Transact-SQL statements and queries, see Tutorial: Writing Transact-SQL Statements.

插入數據Insert data

接下來建立一個新表 Inventory,而後插入兩個新行。Next create a new table, Inventory, and insert two new rows.

  1. 在 sqlcmd  命令提示符中,將上下文切換到新的 TestDB 數據庫:From the sqlcmd command prompt, switch context to the new TestDB database:

    SQL
    USE TestDB 
  2. 建立名爲 Inventory 的新表:Create new table named Inventory:

    SQL
    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT) 
  3. 將數據插入新表:Insert data into the new table:

    SQL
    INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154); 
  4. 要執行上述命令的類型 GOType GO to execute the previous commands:

    SQL
    GO

選擇數據Select data

如今,運行查詢以從 Inventory 表返回數據。Now, run a query to return data from the Inventory table.

  1. 經過 sqlcmd  命令提示符輸入查詢,以返回 Inventory 表中數量大於 152 的行:From the sqlcmd command prompt, enter a query that returns rows from the Inventory table where the quantity is greater than 152:

    SQL
    SELECT * FROM Inventory WHERE quantity > 152; 
  2. 執行命令:Execute the command:

    SQL
    GO

退出 sqlcmd 命令提示符Exit the sqlcmd command prompt

要結束 sqlcmd  會話,請鍵入 QUITTo end your sqlcmd session, type QUIT:

SQL
QUIT

性能最佳作法Performance best practices

在 Linux 上安裝 SQL Server 後,請查看配置 Linux 和 SQL Server 以提升生產性能的最佳作法。After installing SQL Server on Linux, review the best practices for configuring Linux and SQL Server to improve performance for production scenarios. 有關詳細信息,請參閱 Linux 上的 SQL Server 的性能最佳作法和配置指南For more information, see Performance best practices and configuration guidelines for SQL Server on Linux.

跨平臺數據工具Cross-platform data tools

除「sqlcmd」之外,還可使用如下跨平臺工具來管理 SQL Server  :In addition to sqlcmd, you can use the following cross-platform tools to manage SQL Server:

   
Azure Data StudioAzure Data Studio 跨平臺 GUI 數據庫管理實用程序。A cross-platform GUI database management utility.
Visual Studio CodeVisual Studio Code 一種跨平臺 GUI 代碼編輯器,它使用 mssql 擴展運行 Transact-SQL 語句。A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension.
PowerShell CorePowerShell Core 基於 cmdlet 的跨平臺自動化和配置工具。A cross-platform automation and configuration tool based on cmdlets.
mssql-climssql-cli 用於運行 Transact-SQL 命令的跨平臺命令行接口。A cross-platform command-line interface for running Transact-SQL commands.

從 Windows 鏈接Connecting from Windows

Windows 上的 SQL Server 工具鏈接到 Linux 上的 SQL Server 實例,操做方式與鏈接到任何遠程 SQL Server 實例同樣。SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server instance.

若是有一臺能夠鏈接到 Linux 計算機的 Windows 計算機,請從運行 sqlcmd  的 Windows 命令提示符嘗試執行本主題中的相同步驟。If you have a Windows machine that can connect to your Linux machine, try the same steps in this topic from a Windows command-prompt running sqlcmd. 僅驗證所使用的是目標 Linux 計算機名稱或 IP 地址,而非 localhost,並確保 TCP 端口 1433 已打開。Just verify that you use the target Linux machine name or IP address rather than localhost, and make sure that TCP port 1433 is open. 若是從 Windows 進行鏈接存在任何問題,請參閱鏈接故障排除建議If you have any problems connecting from Windows, see connection troubleshooting recommendations.

有關在 Windows 上運行,但鏈接到 Linux 上的 SQL Server 的其餘工具,請參閱:For other tools that run on Windows but connect to SQL Server on Linux, see:

其餘部署方案Other deployment scenarios

有關其餘安裝方案,請參閱如下資源:For other installation scenarios, see the following resources:

   
升級Upgrade 瞭解如何升級 Linux 版 SQL Server 的現有安裝Learn how to upgrade an existing installation of SQL Server on Linux
卸載Uninstall 在 Linux 上卸載 SQL ServerUninstall SQL Server on Linux
無人蔘與安裝Unattended install 瞭解如何編寫無提示安裝腳本Learn how to script the installation without prompts
脫機安裝Offline install 瞭解如何手動下載脫機安裝程序包Learn how to manually download the packages for offline installation

提示

有關常見問題的解答,請參閱 Linux 上的 SQL Server 常見問題解答For answers to frequently asked questions, see the SQL Server on Linux FAQ.

後續步驟Next steps

  
  
  
  

 
  
  
  
  

 

反饋

發送有關如下對象的反饋

相關文章
相關標籤/搜索