Linux 上的 SQL Server Express 初体验

· 415 words · 2 minute read

本文不讨论 SQL Server 和 MySQL、PostgreSQL 和 Oracle 的对比问题。只是一个类似「开箱评测」的文章。因此,亦会包含个人情感和价值观。如有不适,可以关闭。

博主对 SQL Server 垂涎欲滴了许久,但由于对 Windows 知识浅薄,不懂 Windows 运维,遂未曾在 Windows 上成功安装过 SQL Server 并投入生产。然而,博主自认为对 GNU / Linux 有些许了解,而且 SQL Server 也支持 Linux 系统。如此一来,便可在熟悉的 Linux 环境下安装 SQL Server,省去了维护 Windows 的麻烦。本文作为体验文,主要讲解安装的过程和初使用的感受。

对于 Arch Linux,SQL Server 已包含在 AUR 中了:mssql-server,只需安装即可。

安装的过程还算顺利,由于是闭源软件,只得从官方下载二进制并打包。SQL Server 很大,写作时,仅官方 RPM 就有 200MiB 之大,装好后甚至占用了 1GiB 的空间。

安装后,需要运行脚本进行配置。主要配置项包含版本(Express、Developer、Web、Datacenter 等等)、密码等等。

使用 Root 运行 /opt/mssql/bin/mssql-conf setup 配置:

$ sudo /opt/mssql/bin/mssql-conf setup
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=2109348&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): 3

选择版本,这里是使用 Express。

By choosing a PAID edition, you are verifying that you have the appropriate number of licenses in place to install and run this software. 难道没有激活验证么…

The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=2104294&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:

设置管理员(sa)密码。

Configuring SQL Server...
 
The licensing PID was successfully processed. The new edition is [Express Edition].
ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink /etc/systemd/system/multi-user.target.wants/mssql-server.service → /usr/lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.

随后便会自动配置并启用 systemd 服务(pacman 用户很不习惯倒是)。

看一下服务:

$ systemctl status mssql-server.service
● 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 xxx; 2min 23s ago
       Docs: https://docs.microsoft.com/en-us/sql/linux
   Main PID: 25776 (sqlservr)
      Tasks: 144
     Memory: 633.5M
     CGroup: /system.slice/mssql-server.service
             ├─25776 /opt/mssql/bin/sqlservr
             └─25812 /opt/mssql/bin/sqlservr
 
sqlservr[25812]: [92B blob data]
sqlservr[25812]: [158B blob data]
sqlservr[25812]: [155B blob data]
sqlservr[25812]: [61B blob data]
sqlservr[25812]: [96B blob data]
sqlservr[25812]: [66B blob data]
sqlservr[25812]: [96B blob data]
sqlservr[25812]: [100B blob data]
sqlservr[25812]: [71B blob data]
sqlservr[25812]: [124B blob data]

随后,便可使用工具进行维护了。本人推荐 SSMS(SQL Server Management Studio),可以图形化建表、维护等等,功能齐全,但仅限 Windows。Azure Data Studio 是另一个选择,使用 Electron,Linux 可以用,但功能一般。当然,也可以安装 AUR mssql-tools 使用命令行管理。

以上便是初体验了,总体来说感觉不错,有了熟悉的 Linux 环境和 systemd,一切皆显得得心应手。配合 SSMS,可以方便地维护数据库。然而,SQL 和 SQL Server 都是很复杂的语言和程序,仍需要系统学习一番,才好下结论。