linux安装sql数据库,Ubuntu:在 Linux 上安装 SQL Server - SQL Server | Microsoft Docs

快速入门:安装 SQL Server 并在 Ubuntu 上创建数据库Quickstart: Install SQL Server and create a database on Ubuntu

04/07/2021

本文内容

适用于:Applies to: 719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server(所有支持的版本)719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server (all supported versions) - Linux适用于:Applies to: 719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server(所有支持的版本)719f28649793c602f9270966b5ed5c39.pngSQL ServerSQL Server (all supported versions) - Linux

本快速入门介绍了如何在 Ubuntu 16.04/18.04 上安装 SQL Server 2017。In this quickstart, you install SQL Server 2017 on Ubuntu 16.04/18.04. 然后使用 sqlcmd 进行连接,创建第一个数据库并运行查询 。You then connect with sqlcmd to create your first database and run queries.

提示

本教程需要用户输入和 Internet 连接。This tutorial requires user input and an internet connection. If you are interested in the unattended or offline installation procedures, see Installation guidance for SQL Server on Linux. 有关受支持平台的列表,请参阅发行说明。For a list of supported platforms, see our Release notes.

本快速入门介绍如何在 Ubuntu 16.04、18.04 或 20.04 上安装 SQL Server 2019。In this quickstart, you install SQL Server 2019 on Ubuntu 16.04, 18.04, or 20.04. 然后使用 sqlcmd 进行连接,创建第一个数据库并运行查询。You then connect with sqlcmd to create your first database and run queries.

备注

自 SQL Server 2019 CU10 起,开始支持 Ubuntu 20.04。Ubuntu 20.04 is supported starting with SQL Server 2019 CU10.

提示

本教程需要用户输入和 Internet 连接。This tutorial requires user input and an internet connection. If you are interested in the unattended or offline installation procedures, see Installation guidance for SQL Server on Linux. 有关受支持平台的列表,请参阅发行说明。For a list of supported platforms, see our Release notes.

先决条件Prerequisites

必须有内存至少为 2GB 的 Ubuntu 16.04 或 18.04 计算机。You must have an Ubuntu 16.04 or 18.04 machine with at least 2 GB of memory.

To install Ubuntu 18.04 on your own machine, go to http://releases.ubuntu.com/bionic/. 还可以在 Azure 中创建 Ubuntu 虚拟机。You can also create Ubuntu virtual machines in Azure.

备注

目前,不支持将适用于 Windows 10 的 Linux 的 Windows 子系统作为安装目标。At this time, the Windows Subsystem for Linux for Windows 10 is not supported as an installation target.

备注

自 SQL Server 2017 CU20 起,开始支持 Ubuntu 18.04。Ubuntu 18.04 is supported starting with SQL Server 2017 CU20. 若要在 Ubuntu 18.04 计算机上按照本文中的说明操作,请务必使用正确的存储库路径 18.04,而不是 16.04。If you want to use the instructions on this article with Ubuntu 18.04, make sure you use the correct repository path, 18.04 instead of 16.04.

若要在较低版本上运行 SQL Server,可以通过修改进行配置。If you are running SQL Server on a lower version, the configuration is possible with modifications.

必须有内存至少为 2GB 的 Ubuntu 16.04、18.04 或 20.04 计算机。You must have an Ubuntu 16.04, 18.04, or 20.04 machine with at least 2 GB of memory.

To install Ubuntu 20.04 on your own machine, go to https://releases.ubuntu.com/20.04/. 还可以在 Azure 中创建 Ubuntu 虚拟机。You can also create Ubuntu virtual machines in Azure.

备注

目前,不支持将适用于 Windows 10 的 Linux 的 Windows 子系统作为安装目标。At this time, the Windows Subsystem for Linux for Windows 10 is not supported as an installation target.

安装 SQL ServerInstall SQL Server

备注

以下用于 SQL Server 2017 的命令指向 Ubuntu 18.04 存储库。The following commands for SQL Server 2017 points to the Ubuntu 18.04 repository. 如果使用的是 Ubuntu 16.04,请将以下路径更改为 /ubuntu/16.04/ 而不是 /ubuntu/18.04/。If you are using Ubuntu 16.04, change the path below to /ubuntu/16.04/ instead of /ubuntu/18.04/.

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

导入公共存储库 GPG 密钥:Import the public repository GPG keys:

wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

注册 Microsoft SQL Server Ubuntu 存储库:Register the Microsoft SQL Server Ubuntu repository:

对于 Ubuntu 16.04:For Ubuntu 16.04:

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

对于 Ubuntu 18.04:For Ubuntu 18.04:

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

提示

如果想安装 SQL Server 2019,必须改为注册 SQL Server 2019 存储库。If you want to install SQL Server 2019 , you must instead register the SQL Server 2019 repository. 使用以下命令安装 SQL Server 2019:Use the following command for SQL Server 2019 installations:

对于 Ubuntu 16.04:For Ubuntu 16.04:

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

对于 Ubuntu 18.04:For Ubuntu 18.04:

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

运行以下命令以安装 SQL Server:Run the following commands to install SQL Server:

sudo apt-get update

sudo apt-get install -y mssql-server

包安装完成后,运行 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.

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

完成配置后,验证服务是否正在运行:Once the configuration is done, verify that the service is running:

systemctl status mssql-server --no-pager

如果计划远程连接,可能还需要在防火墙上打开 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

备注

以下用于 SQL Server 2019 的命令指向 Ubuntu 20.04 存储库。The following commands for SQL Server 2019 points to the Ubuntu 20.04 repository. 如果使用的是 Ubuntu 18.04 或 16.04,请将以下路径更改为 /ubuntu/18.04/ 或 /ubuntu/16.04/,而不是 /ubuntu/20.04/。If you are using Ubuntu 18.04 or 16.04, change the path below to /ubuntu/18.04/ or /ubuntu/16.04/ instead of /ubuntu/20.04/.

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

导入公共存储库 GPG 密钥:Import the public repository GPG keys:

wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

为 SQL Server 2019 注册 Microsoft SQL Server Ubuntu 存储库:Register the Microsoft SQL Server Ubuntu repository for SQL Server 2019:

对于 Ubuntu 16.04:For Ubuntu 16.04:

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

对于 Ubuntu 18.04:For Ubuntu 18.04:

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

对于 Ubuntu 20.04:For Ubuntu 20.04:

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

运行以下命令以安装 SQL Server:Run the following commands to install SQL Server:

sudo apt-get update

sudo apt-get install -y mssql-server

包安装完成后,运行 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.

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

完成配置后,验证服务是否正在运行:Once the configuration is done, verify that the service is running:

systemctl status mssql-server --no-pager

如果计划远程连接,可能还需要在防火墙上打开 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 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 命令行工具:sqlcmd 和 bcp。The 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.

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

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

注册 Microsoft Ubuntu 存储库。Register the Microsoft Ubuntu repository.

对于 Ubuntu 16.04:For Ubuntu 16.04:

curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

对于 Ubuntu 18.04:For Ubuntu 18.04:

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

对于 Ubuntu 20.04:For Ubuntu 20.04:

curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

更新源列表,并使用 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

可选:向 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 文件中的 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:

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.

使用 SQL Server 名称 (-S),用户名 (-U) 和密码 (-P) 的参数运行 sqlcmd 。Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P). 在本教程中,用户进行本地连接,因此服务器名称为 localhost。In 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.

sqlcmd -S localhost -U SA -P ''

提示

可以在命令行上省略密码,以收到密码输入提示。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.

如果成功,应会显示 sqlcmd 命令提示符:1>。If successful, you should get to a sqlcmd command prompt: 1>.

如果连接失败,先尝试诊断错误消息中所述的问题。If you get a connection failure, first attempt to diagnose the problem from the error message.

创建和查询数据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.

在 sqlcmd 命令提示符中,粘贴以下 Transact-SQL 命令以创建测试数据库:From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:

CREATE DATABASE TestDB

在下一行中,编写一个查询以返回服务器上所有数据库的名称:On the next line, write a query to return the name of all of the databases on your server:

SELECT Name from sys.Databases

前两个命令没有立即执行。The previous two commands were not executed immediately. 必须在新行中键入 GO 才能执行以前的命令:You must type GO on a new line to execute the previous commands:

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.

在 sqlcmd 命令提示符中,将上下文切换到新的 TestDB 数据库:From the sqlcmd command prompt, switch context to the new TestDB database:

USE TestDB

创建名为 Inventory 的新表:Create new table named Inventory:

CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)

将数据插入新表:Insert data into the new table:

INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);

要执行上述命令的类型 GO:Type GO to execute the previous commands:

GO

选择数据Select data

现在,运行查询以从 Inventory 表返回数据。Now, run a query to return data from the Inventory table.

通过 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:

SELECT * FROM Inventory WHERE quantity > 152;

执行此命令:Execute the command:

GO

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

要结束 sqlcmd 会话,请键入 QUIT:To end your sqlcmd session, type QUIT:

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.

跨平台数据工具Cross-platform data tools

除“sqlcmd”以外,还可以使用以下跨平台工具来管理 SQL Server :In addition to sqlcmd, you can use the following cross-platform tools to manage SQL Server:

工具Tool

说明Description

跨平台 GUI 数据库管理实用程序。A cross-platform GUI database management utility.

一种跨平台 GUI 代码编辑器,它使用 mssql 扩展运行 Transact-SQL 语句。A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension.

基于 cmdlet 的跨平台自动化和配置工具。A cross-platform automation and configuration tool based on cmdlets.

用于运行 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:

升级:了解如何升级 Linux 版 SQL Server 的现有安装Upgrade: Learn how to upgrade an existing installation of SQL Server on Linux

卸载:在 Linux 上卸载 SQL ServerUninstall: Uninstall 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

提示

For answers to frequently asked questions, see the SQL Server on Linux FAQ.

后续步骤Next steps

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值