快速入门: 安装 SQL Server 和 Red Hat 上创建数据库Quickstart: Install SQL Server and create a database on Red Hat

本主题适用于: 是SQL Server (仅限 Linux)没有Azure SQL 数据库没有Azure SQL 数据仓库没有并行数据仓库 THIS TOPIC APPLIES TO: yesSQL Server (Linux only)noAzure SQL DatabasenoAzure SQL Data WarehousenoParallel Data Warehouse

在本快速入门教程,你首先安装 SQL Server 2017 上 Red Hat Enterprise Linux (RHEL) 7.3 +。In this quickstart, you first install SQL Server 2017 on Red Hat Enterprise Linux (RHEL) 7.3+. 然后使用 sqlcmd 连接,以创建第一个数据库并运行查询。Then connect with sqlcmd to create your first database and run queries.


本教程需要用户输入和 internet 连接。This tutorial requires user input and an internet connection. 如果你有兴趣无人参与脱机安装过程,请参阅在 Linux 上的 SQL Server 安装指南If you are interested in the unattended or offline installation procedures, see Installation guidance for SQL Server on Linux.


你必须 RHEL 7.3 或 7.4 机至少 2 GB的内存。You must have a RHEL 7.3 or 7.4 machine with at least 2 GB of memory.

若要在您自己的计算机上安装 Red Hat Enterprise Linux,请转到http://access.redhat.com/products/red-hat-enterprise-linux/evaluationTo install Red Hat Enterprise Linux on your own machine, go to http://access.redhat.com/products/red-hat-enterprise-linux/evaluation. 你还可以在 Azure 中创建 RHEL 虚拟机。You can also create RHEL virtual machines in Azure. 请参阅创建和使用 Azure CLI 管理 Linux Vm,并使用--image RHEL对的调用中az vm createSee Create and Manage Linux VMs with the Azure CLI, and use --image RHEL in the call to az vm create.

其他系统要求,请参阅在 Linux 上的 SQL Server 的系统需求For other system requirements, see System requirements for SQL Server on Linux.

安装 SQL ServerInstall SQL Server

若要在 RHEL 上配置 SQL Server,在安装的终端运行以下命令mssql server包:To configure SQL Server on RHEL, run the following commands in a terminal to install the mssql-server package:


如果你已经安装 CTP 或 RC 版本的 SQL Server 自 2017 年,必须注册一个 GA 存储库之前先删除旧的存储库。If you have previously installed a CTP or RC release of SQL Server 2017, you must first remove the old repository before registering one of the GA repositories. 有关详细信息,请参阅从预览存储库的存储库更改到 GA 存储库For more information, see Change repositories from the preview repository to the GA repository.

  1. 下载 Microsoft SQL Server Red Hat 存储库配置文件:Download the Microsoft SQL Server Red Hat repository configuration file:

    sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo


    这是累积更新 (CU) 存储库。This is the Cumulative Update (CU) repository. 有关你的存储库选项和它们之间的差异的详细信息,请参阅为在 Linux 上的 SQL Server 配置存储库For more information about your repository options and their differences, see Configure repositories for SQL Server on Linux.

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

    sudo yum install -y mssql-server
  3. 软件包安装完成后,运行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.

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


    如果你在本教程中尝试 SQL Server 自 2017 年,自由地授予许可的以下版本: 评估、 开发人员和快速。If you are trying SQL Server 2017 in this tutorial, the following 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).

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

    systemctl status mssql-server
  5. 若要允许远程连接,请在 RHEL 上打开防火墙上的 SQL Server 端口。To allow remote connections, open the SQL Server port on the firewall on RHEL. 默认的 SQL Server 端口为 TCP 1433。The default SQL Server port is TCP 1433. 如果你使用FirewallD适合您的防火墙,你可以使用以下命令:If you are using FirewallD for your firewall, you can use the following commands:

    sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
    sudo firewall-cmd --reload

此时,SQL Server 正在您 RHEL 的计算机上运行并且已准备好使用 !At this point, SQL Server is running on your RHEL 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.

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

    sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
  2. 如果你有以前版本的mssql 工具安装,请删除任何较旧的 unixODBC 程序包。If you had a previous version of mssql-tools installed, remove any older unixODBC packages.

    sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
  3. 运行以下命令以安装mssql 工具与 unixODBC 开发人员包。Run the following commands to install mssql-tools with the unixODBC developer package.

    sudo yum install -y mssql-tools unixODBC-devel
  4. 为方便起见,添加/opt/mssql-tools/bin/到你的PATH境变量。For convenience, add /opt/mssql-tools/bin/ to your PATH environment variable. 这使您可以运行工具,而无需指定完整路径。This enables you to run the tools without specifying the full path. 在登录会话和交互式/非登录会话中运行以下命令以修改PATHRun the following commands to modify the PATH for both login sessions and interactive/non-login sessions:

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


Sqlcmd只是一个用于连接到 SQL Server 并运行查询和执行管理及开发任务的工具。Sqlcmd is just one tool for connecting to SQL Server to run queries and perform management and development tasks. 其他工具包括:Other tools include:

本地连接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.

    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:

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

    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:


插入数据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:

    USE TestDB
  2. 创建名为 Inventory 的新表:Create new table named Inventory:

    CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
  3. 将数据插入新表:Insert data into the new table:

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


选择数据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:

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


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

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


从 Windows 进行连接Connect 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:

其他资源Additional resources

有关其他安装方案,请参阅以下资源: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

若要浏览连接和管理 SQL Server 的其他方法,请浏览以下工具:To explore other ways to connect and manage SQL Server, explore the following tools:

Visual Studio 代码Visual Studio Code 一种跨平台 GUI 代码编辑器,mssql 扩展名运行 TRANSACT-SQL 语句。A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension.
SQL Server 操作 StudioSQL Server Operations Studio 一个跨平台 GUI 数据库管理实用程序。A cross-platform GUI database management utility.
mssql-climssql-cli 跨平台命令行界面中运行 TRANSACT-SQL 命令。A cross-platform command-line interface for running Transact-SQL commands.
SQL Server Management StudioSQL Server Management Studio 一个基于 Windows 的 GUI 数据库管理实用程序可以连接到和管理在 Linux 上的 SQL Server 实例。A Windows-based GUI database management utility that can connect to and manage SQL Server instances on Linux.

若要详细了解如何编写 Transact-SQL 语句和查询,请参阅教程:编写 Transact-SQL 语句To learn more about writing Transact-SQL statements and queries, see Tutorial: Writing Transact-SQL Statements.


有关的常见问题的答案,请参阅Linux 常见问题的 SQL ServerFor answers to frequently asked questions, see the SQL Server on Linux FAQ.

后续步骤Next steps