如何在 Linux 上安装 Microsoft SQL

[ad_1]

在本文中,我们将讨论如何在 Linux 上安装 Microsoft SQL 或 MSSQL。 Microsoft SQL,俗称MSSQL,是微软创建的关系型数据库管理系统。 开源 MySQL 和 PostgreSQL 通常是 Linux 发行版的同义词,但也支持在 Linux 上使用 MSSQL。 MSSQL 提供了一些开源版本没有的特性,并且根据应用程序的需求,它可能是 RDBMS 的正确选择。 在本教程中,我们将介绍如何在 CentOS 7 和 Ubuntu 16.04 上安装 MSSQL。

飞行前检查

  • 您需要验证您的服务器至少有 2GB 内存
  • 这些说明分别在 CentOS 7 和 Ubuntu 16.04 LTS 服务器上以 root 用户身份执行

CentOS 7

第 1 步:添加 MSSQL 2019 预览版存储库

首先,作为最佳实践,确保所有软件包都是最新的:

[email protected] ~]# yum update -y

接下来,我们需要通过添加适当的 repo 来告诉包管理器 yum 在哪里寻找 mssql-server 包:

[email protected] ~]# curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-preview.repo

步骤 2:安装 SQL Server

现在 yum 知道了 MSSQL 存储库,我们可以使用 yum 安装包:

[email protected] ~]# yum install -y mssql-server

步骤 3:配置 MSSQL 服务器

接下来,我们需要使用系统管理员密码配置 SQL,并确认我们要使用的版本。 本教程将使用开发人员版本,选择 2,因为它是免费的:

[email protected] ~]# /opt/mssql/bin/mssql-conf setup
usermod: no changes
Choose an edition of SQL Server:
  1) Evaluation (free, no production use rights, 180-day limit)
  2) Developer (free, no production use rights)
  3) Express (free)
  4) Web (PAID)
  5) Standard (PAID)
  6) Enterprise (PAID) - CPU Core utilization restricted to 20 physical/40 hyperthreaded
  7) Enterprise Core (PAID) - CPU Core utilization up to Operating System Maximum
  8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 2
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Do you accept the license terms? [Yes/No]:Yes

Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...

This is an evaluation version.  There are [116] days left in the evaluation period.
ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.

之后,我们需要验证 mssql 服务是否正在运行:

[email protected] ~]# systemctl status mssql-server

输出应如下所示:

mssql-server.service - Microsoft SQL Server Database Engine
   Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-10-23 20:18:03 EDT; 2min 45s ago
     Docs: https://docs.microsoft.com/en-us/sql/linux
 Main PID: 61529 (sqlservr)
   CGroup: /system.slice/mssql-server.service
           ├─61529 /opt/mssql/bin/sqlservr
           └─61549 /opt/mssql/bin/sqlservr

第 4 步(可选):允许远程连接

如果您希望您的 SQL Server 可以远程访问,则必须打开 SQL Server 端口:

笔记: 谨慎行事。 防火墙已到位,可通过限制对服务器的访问来保证服务器的安全。 除非您打算远程访问 SQL Server,否则没有必要打开此端口。

[email protected] ~]# firewall-cmd --zone=public --add-port=1433/tcp --permanent

添加规则后,我们需要重新加载防火墙规则并验证端口是否打开:

[[email protected] ~]# firewall-cmd --reload
success
[email protected] ~]# firewall-cmd --list-ports
1433/tcp

步骤 5:添加 Microsoft Red Hat 存储库

现在,我们需要一种与 SQL 服务器交互的方法。 首先,让我们添加另一个 repo,以便我们可以使用 yum 安装 SQL Server 命令行工具

[email protected] ~]# curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo

第 6 步:安装和设置 MSSQL Server 命令行工具

现在 yum 知道我们要安装的软件包,我们需要安装它们。 需要注意的是,在安装这些包的过程中,会有几个交互提示来接受许可条款:

[email protected] ~]# yum install -y mssql-tools unixODBC-devel

为了便于使用,我们可以添加路径

/opt/mssql-tools/bin/ 

到服务器上的 PATH 变量,以便我们可以从服务器上的任何位置执行 sql 命令:

[email protected] ~]# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

[email protected] ~]# source ~/.bashrc

最后一步是验证我们是否可以连接到 SQL Server:

[email protected] ~]# sqlcmd -S localhost -U SA
Password:
1>

Ubuntu 18.04 LTS

第 1 步:添加 MSSQL Server Ubuntu 2019 预览版存储库

首先,让我们更新服务器包:

[email protected]:~# apt-get update -y

更新服务器包后,我们需要为要添加的存储库添加 GPG 密钥。 GPG 密钥是 Linux 用户验证文件有效性并确认它们来自可信来源的一种方式:

[email protected]:~# wget -qO- https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

现在 GPG 密钥已经到位,我们可以添加存储库:

[email protected]:~# add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"

我们刚刚添加的存储库需要 HTTPS 连接。 为了确保 apt 可以连接到 repo,我们需要确定它可以通过 https 连接:

[email protected]:~# apt-get install -y apt-transport-https

第 2 步:安装 MSSQL 服务器

现在包含 MSSQL Server 包的 repo 可用,剩下要做的就是确保 apt 知道新的 repo 并安装 MSSQL Server:

apt-get update -y
apt-get install -y mssql-server

步骤 3:配置 MSSQL 服务器

CentOS 7 和 Ubuntu 16.04 的配置步骤相同。 在配置过程中,将有交互式提示选择 SQL Server 版本、接受许可条款并输入 SQL 管理员密码:

[email protected]:~# /opt/mssql/bin/mssql-conf setup
usermod: no changes
Choose an edition of SQL Server:
  1) Evaluation (free, no production use rights, 180-day limit)
  2) Developer (free, no production use rights)
  3) Express (free)
  4) Web (PAID)
  5) Standard (PAID)
  6) Enterprise (PAID) - CPU Core utilization restricted to 20 physical/40 hyperthreaded
  7) Enterprise Core (PAID) - CPU Core utilization up to Operating System Maximum
  8) I bought a license through a retail sales channel and have a product key to enter.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 2
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Do you accept the license terms? [Yes/No]:Yes

Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...

This is an evaluation version.  There are [116] days left in the evaluation period.
ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.

MSSQL Server 现在应该正在运行并启用。 为了验证事实是否如此,我们可以运行以下命令:

[email protected]:~# systemctl status mssql-server --no-pager
* mssql-server.service - Microsoft SQL Server Database Engine
   Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-10-24 00:24:23 EDT; 3min 45s ago
     Docs: https://docs.microsoft.com/en-us/sql/linux
 Main PID: 19446 (sqlservr)
    Tasks: 135
   Memory: 548.5M
      CPU: 12.499s
   CGroup: /system.slice/mssql-server.service
           |-19446 /opt/mssql/bin/sqlservr
           `-19485 /opt/mssql/bin/sqlservr

第 4 步(可选):允许远程连接

如果您打算使用远程连接到您的新 SQL Server,则需要打开 SQL Server 端口:

笔记: 再次谨慎行事。 防火墙已到位,可通过限制对服务器的访问来保证服务器的安全。 除非您打算远程访问 SQL Server,否则没有必要打开此端口。

为了保持我们的防火墙交互简洁,请安装 ufw,也称为 Uncomplicated Firewall:

[email protected]:~# apt-get install -y ufw

安装后,必须启用ufw。 您将看到一条警告,表明您的 SSH 连接可能被中断。 如果您的 SSH 会话断开连接,请重新登录并继续:

[email protected]:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
firewall is active and enabled on system startup

现在 ufw 已就位并启用,是时候允许流量通过端口 1433:

[email protected]:~# ufw allow 1433
Rule added
Rule added (v6)

第 5 步:安装和设置 MSSQL Server 命令行工具

首先,和之前一样,我们需要为包含 MSSQL 命令行工具的 repo 添加一些新的 GPG 密钥:

[email protected]:~# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

现在我们可以添加存储库:

[email protected]:~# curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | tee /etc/apt/sources.list.d/msprod.list

之后,更新 apt 并安装命令行工具:

[email protected]:~# apt-get update -y
[email protected]:~# apt-get install -y mssql-tools unixodbc-dev

在安装过程中应该有一个或两个交互式提示来接受许可证,如下所示:

让我们可以轻松地在服务器上的任何位置执行 sqlcmd:

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

最后,是时候验证我们是否可以在本地连接到 MSSQL Server:

[email protected]:~# sqlcmd -S localhost -U SA
Password:
1>

今天就开始吧!

您是否需要在 Linux 上设置备用数据库系统,例如 MSSQL? 在配置现有数据库或排除相关问题时需要帮助让您感到沮丧? 我们有一些业内最优秀的人才为 Liquid Web 工作,我们一年 365 天、每天 24 小时都在等待证明这一点! 我们可以随时介入,为您提供解决问题所需的帮助。

[ad_2]

Related Posts