Linux下安装MS-SqlServer数据库有两种方式,第一种是使用rpm安装包安装,第二种是用dnf(yum)源镜像安装。


条件要求:RHEL 7.3、7.4、7.5、7.6 或 8 计算机(内存至少为至少 2 GB) 。如果以前安装了 SQL Server 的 CTP 或 RC 版本,则必须先删除旧存储库,然后再执行这些步骤。


一、RPM方式安装:

1、RPM文件下载地址:wget https://packages.microsoft/rhel/8/mssql-server-2019/mssql-server-15.0.4003.23-3.x86_64.rpm

[root@localhost src]# wget https://packages.microsoft/rhel/8/mssql-server-2019/mssql-server-15.0.4003.23-3.x86_64.rpm
--2020-02-03 11:23:14--  https://packages.microsoft/rhel/8/mssql-server-2019/mssql-server-15.0.4003.23-3.x86_64.rpm
正在解析主机 packages.microsoft (packages.microsoft)... 13.75.127.55
正在连接 packages.microsoft (packages.microsoft)|13.75.127.55|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:220632412 (210M) [application/x-redhat-package-manager]
正在保存至: “mssql-server-15.0.4003.23-3.x86_64.rpm”

mssql-server-15.0.4003.23-3.x86_64.rpm     100%[========================================================================================>] 210.41M  9.30MB/s  用时 24s     

2020-02-03 11:23:38 (8.90 MB/s) - 已保存 “mssql-server-15.0.4003.23-3.x86_64.rpm” [220632412/220632412])

2、安装SQLSERVER和依赖库(根据提示安装gdb和python依赖,和安装环境有关,提示缺少什么就安装什么)

[root@localhost src]# rpm -ivh mssql-server-15.0.4003.23-3.x86_64.rpm 
错误:依赖检测失败:
    gdb 被 mssql-server-15.0.4003.23-3.x86_64 需要
    python2 被 mssql-server-15.0.4003.23-3.x86_64 需要

[root@localhost src]# rpm -ivh mssql-server-15.0.4003.23-3.x86_64.rpm 
Verifying...                          ################################# [100%]
准备中...                          ################################# [100%]
正在升级/安装...
   1:mssql-server-15.0.4003.23-3      ################################# [100%]

+--------------------------------------------------------------+
请运行 "sudo /opt/mssql/bin/mssql-conf setup"
完成 Microsoft SQL Server 的设置
+--------------------------------------------------------------+

具体设置见“二、dnf(yum)方式安装SQL Server”第三步


二、dnf(yum)方式安装SQL Server:

1、下载 Microsoft SQL Server 2019 Red Hat 存储库配置文件:

sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft/config/rhel/8/mssql-server-2019.repo

2、运行以下命令以安装 SQL Server:

sudo yum install -y mssql-server

3、包安装完成后,运行 mssql-conf setup,按照提示设置 SA 密码并选择版本。

 sudo /opt/mssql/bin/mssql-conf setup
[root@localhost bin]# ./mssql-conf setup
usermod:无改变
选择 SQL Server 的一个版本:
  1) Evaluation (免费,无生产许可,180 天限制)
  2) Developer (免费,无生产许可)
  3) Express (免费)
  4) Web (付费版)
  5) Standard (付费版)
  6) Enterprise (付费版) - CPU 核心利用率限制为 20 个物理/40 个超线程
  7) Enterprise Core (付费版) - CPU 核心利用率达到操作系统最大值
  8) 我通过零售渠道购买了许可证并具有要输入的产品密钥。

可在以下位置找到有关版本的详细信息:
https://go.microsoft/fwlink/?LinkId=2109348&clcid=0x804

使用此软件的付费版本需要通过以下途径获取单独授权
Microsoft 批量许可计划。
选择付费版本即表示你具有适用的
要安装和运行此软件的就地许可证数量。

输入版本(1-8): 2
可以在以下位置找到此产品的许可条款:
/usr/share/doc/mssql-server 或从以下位置下载:
https://go.microsoft/fwlink/?LinkId=2104294&clcid=0x804

可以从以下位置查看隐私声明:
https://go.microsoft/fwlink/?LinkId=853010&clcid=0x804

接受此许可条款吗? [Yes/No]:yes


选择 SQL Server 的语言:
(1) English
(2) Deutsch
(3) Español
(4) Français
(5) Italiano
(6) 日本語
(7) 한국어
(8) Português
(9) Русский
(10) 中文 – 简体
(11) 中文 (繁体)
输入选项 1-11:10
输入 SQL Server 系统管理员密码: 
确认 SQL Server 系统管理员密码: 
正在配置 SQL Server...

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.
安装程序已成功完成。SQL Server 正在启动。

4、完成配置后,验证服务是否正在运行:

systemctl status mssql-server
[root@localhost bin]# 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 Mon 2020-02-03 12:57:18 CST; 1min 43s ago
     Docs: https://docs.microsoft/en-us/sql/linux
 Main PID: 8862 (sqlservr)
    Tasks: 143
   Memory: 634.0M
   CGroup: /system.slice/mssql-server.service
           ├─8862 /opt/mssql/bin/sqlservr
           └─8889 /opt/mssql/bin/sqlservr

2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [79B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [75B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [87B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [96B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [91B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [100B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [73B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [71B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [120B blob data]
2月 03 12:57:23 localhost.localdomain sqlservr[8862]: [124B blob data]

5、若要允许远程连接,请在 RHEL 的防火墙上打开 SQL Server 端口。 默认的 SQL Server 端口为 TCP 1433。 如果为防火墙使用的是 FirewallD,则可以使用以下命令:

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

经常用到的几个命令:

systemctl enable mssql-server  #设置开机自启动
systemctl start mssql-server  #开启服务
systemctl restart mssql-server  #重启服务 
systemctl stop mssql-server  #停止服务
systemctl status mssql-server  #查看服务状态

三、安装 SQL Server 命令行工具


若要创建数据库,则需要使用可在 SQL Server 上运行 Transact-SQL 语句的工具进行连接。 以下步骤将安装 SQL Server 命令行工具:sqlcmd 和 bcp。


1、下载 Microsoft Red Hat 存储库配置文件。

sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft/config/rhel/8/prod.repo
[root@localhost bin]# curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft/config/rhel/8/prod.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   192  100   192    0     0   1371      0 --:--:-- --:--:-- --:--:--  1371

2、如果安装了早期版本的 mssql-tools,请删除所有旧的 unixODBC 包。

sudo yum remove unixODBC-utf16 unixODBC-utf16-devel

3、运行以下命令,以使用 unixODBC 开发人员包安装 mssql-tools。

sudo yum install -y mssql-tools unixODBC-devel
[root@localhost bin]# sudo yum install -y mssql-tools unixODBC-devel
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
packages-microsoft-com-prod                                                                                                                       52 kB/s |  19 kB     00:00    
依赖关系解决。
=================================================================================================================================================================================
 Package                                   Architecture                      Version                                Repository                                              Size
=================================================================================================================================================================================
安装:
 mssql-tools                               x86_64                            17.4.1.1-1                             packages-microsoft-com-prod                            286 k
 unixODBC-devel                            x86_64                            2.3.7-1.rh                             packages-microsoft-com-prod                             42 k
安装依赖关系:
 msodbcsql17                               x86_64                            17.4.2.1-1                             packages-microsoft-com-prod                            913 k
 unixODBC                                  x86_64                            2.3.7-1.rh                             packages-microsoft-com-prod                            213 k

事务概要
=================================================================================================================================================================================
安装  4 软件包

总下载:1.4 M
安装大小:1.4 M
下载软件包:
(1/4): unixODBC-2.3.7-1.rh.x86_64.rpm                                                                                                            699 kB/s | 213 kB     00:00    
(2/4): mssql-tools-17.4.1.1-1.x86_64.rpm                                                                                                         861 kB/s | 286 kB     00:00    
(3/4): unixODBC-devel-2.3.7-1.rh.x86_64.rpm                                                                                                      1.1 MB/s |  42 kB     00:00    
(4/4): msodbcsql17-17.4.2.1-1.x86_64.rpm                                                                                                         1.9 MB/s | 913 kB     00:00    
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                             3.0 MB/s | 1.4 MB     00:00     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                                                                                  1/1 
  安装    : unixODBC-2.3.7-1.rh.x86_64                                                                                                                                       1/4 
  运行脚本: unixODBC-2.3.7-1.rh.x86_64                                                                                                                                       1/4 
  运行脚本: msodbcsql17-17.4.2.1-1.x86_64                                                                                                                                    2/4 
The license terms for this product can be downloaded from
https://aka.ms/odbc17eula and found in
/usr/share/doc/msodbcsql17/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
yes
  安装    : msodbcsql17-17.4.2.1-1.x86_64                                                                                                                                    2/4 
  运行脚本: msodbcsql17-17.4.2.1-1.x86_64                                                                                                                                    2/4 
  运行脚本: mssql-tools-17.4.1.1-1.x86_64                                                                                                                                    3/4 
The license terms for this product can be downloaded from
http://go.microsoft/fwlink/?LinkId=746949 and found in
/usr/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
yes
  安装    : mssql-tools-17.4.1.1-1.x86_64                                                                                                                                    3/4 
  安装    : unixODBC-devel-2.3.7-1.rh.x86_64                                                                                                                                 4/4 
  运行脚本: msodbcsql17-17.4.2.1-1.x86_64                                                                                                                                    4/4 
  运行脚本: unixODBC-devel-2.3.7-1.rh.x86_64                                                                                                                                 4/4 
  验证    : msodbcsql17-17.4.2.1-1.x86_64                                                                                                                                    1/4 
  验证    : mssql-tools-17.4.1.1-1.x86_64                                                                                                                                    2/4 
  验证    : unixODBC-2.3.7-1.rh.x86_64                                                                                                                                       3/4 
  验证    : unixODBC-devel-2.3.7-1.rh.x86_64                                                                                                                                 4/4 

已安装:
  mssql-tools-17.4.1.1-1.x86_64               unixODBC-devel-2.3.7-1.rh.x86_64               msodbcsql17-17.4.2.1-1.x86_64               unixODBC-2.3.7-1.rh.x86_64              

完毕!

4、为方便起见,向 PATH 环境变量添加 /opt/mssql-tools/bin/ 。 这样可以在不指定完整路径的情况下运行这些工具。 运行以下命令以修改登录会话和交互式/非登录会话的路径 :

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

5、检查是否安装成功:输入以下命令sqlcmd

[root@localhost bin]# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 17.4.0001.1 Linux
Copyright (c) 2012 Microsoft. All rights reserved.

usage: sqlcmd            [-U login id]          [-P password]
  [-S server or Dsn if -D is provided] 
  [-H hostname]          [-E trusted connection]
  [-N Encrypt Connection][-C Trust Server Certificate]
  [-d use database name] [-l login timeout]     [-t query timeout]
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]
  [-q "cmdline query"]   [-Q "cmdline query" and exit]
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-K application intent]
  [-M multisubnet failover]
  [-b On error batch abort]
  [-D Dsn flag, indicate -S is Dsn] 
  [-X[1] disable commands, startup script, environment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]
[root@localhost bin]# bcp -v
BCP - Bulk Copy Program for Microsoft SQL Server.
Copyright (C) Microsoft Corporation. All Rights Reserved.
Version: 17.4.0001.1

四、客户端连接


使用 sqlcmd 本地连接到新的 SQL Server 实例,有三种方式,一种直接用Sqlcmd命令行方式,第二种用MSSql客户端,第三种用其他的客户端比如navicat等。


1、使用 SQL Server 名称 (-S),用户名 (-U) 和密码 (-P) 的参数运行 sqlcmd 。 在本教程中,用户进行本地连接,因此服务器名称为 localhost。 用户名为 SA,密码是在安装过程中为 SA 帐户提供的密码。

sqlcmd -S localhost -U SA -P '<YourPassword>'
[root@localhost bin]# sqlcmd -S localhost -U SA -p
Password: 
1> 

2、如果成功,应会显示 sqlcmd 命令提示符:1>,如上图。
3、如果连接失败,先尝试诊断错误消息中所述的问题。
4、使用Navicat 连接时如果提示如下问题:

[IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

安装navicat安装目录下自带的sqlncli_x64.msi,安装后问题就可以解决!

通过windows下的SQL Manager Studio 连接:

五、命令行方式数据库的操作


1、查询系统下的所有数据库

1> select name from sys.databases;
2> go
name                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------
master                                                                                                                          
tempdb                                                                                                                          
model                                                                                                                           
msdb                                                                                                                            
testdb                                                                                                                          

(5 rows affected)

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total        49  avg   49.0 (20.4 xacts per sec.)

2、创建一个数据库为test

1> create database test;
2> go

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total       460  avg   460.0 (2.2 xacts per sec.)

3、我们再次查看系统下的所有数据库

1> select name from sys.databases;
2> go
name                                                                                                                            
--------------------------------------------------------------------------------------------------------------------------------
master                                                                                                                          
tempdb                                                                                                                          
model                                                                                                                           
msdb                                                                                                                            
testdb                                                                                                                          
test                                                                                                                            

(6 rows affected)

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total         2  avg   2.0 (500.0 xacts per sec.)

4、接下来我们需要选择该数据库

1> use test;
2> go
已将数据库上下文更改为 "test"。

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total         1  avg   1.0 (1000.0 xacts per sec.)

5、接下来我们需要创建一张表,表的名称为:testtb

1> create table testtb(id INT,name NVARCHAR(50),quantity INT);
2> go

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total        14  avg   14.0 (71.4 xacts per sec.)

6、然后我们给表中插入数据

1> insert into testtb values(1,'tom',30);
2> insert into testtb values(2,'marry',25);
3> go

(1 rows affected)

(1 rows affected)

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total         9  avg   9.0 (111.1 xacts per sec.)

7、接下来我们查询表中的信息

1> select * from testtb;
2> go
id          name                                               quantity   
----------- -------------------------------------------------- -----------
          1 tom                                                         30
          2 marry                                                       25

(2 rows affected)

Network packet size (bytes): 4096
1 xact[s]:
Clock Time (ms.): total         2  avg   2.0 (500.0 xacts per sec.)

微软的连接:https://docs.microsoft/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-server-ver15

更多推荐

Linux安装MS-SQLSERVER及客户端连接