sql server for linux 安装 - HbnKing/InstallationNotes GitHub Wiki

2017年10月,发布了SQL Server 2017,下面会写如何在Linux下安装SQL Server 2017,总体过程类似,但有些地方有所不同。

SQL SERVER 2017可以在多种Linux上安装,这里主要讲在CentOS上的安装,支持的版本是 Red Hat Enterprise Linux 7.3 或 7.4 ,我用的是CentOS 7.4,如果在低版本,比如:CentOS 7.2上安装,可能会出现各种问题。

安装CentOS 7.4的步骤,可以参考Linux下安装SQL Server 2016(准备篇),和安装CentOS 7.2 类似。

顺便说一句,CentOS做的越来越棒了,必须给个赞。

安装过程

整个安装过程非常简单。

可以参考微软的详细文档: 在 Linux 上的 SQL Server 安装指南,包含了在多个Linux分支上安装的方法。

本文用root身份来执行,如果不是root账号,需要在下面的各种命令前面,加上 sudo,就用模拟root账号运行各种命令。

(1)下载 Microsoft SQL Server Red Hat 存储库配置文件

curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo (2)安装 SQL Server

yum install -y mssql-server

(3)软件包安装完成后,运行mssql conf 安装命令(和2016的安装命令有区别),进行配置。

/opt/mssql/bin/mssql-conf setup 根据提示:

第1,选择版本,这里选择输入 1,就是评估版。

第2,接受条款。

第3,选择语言,这里选择的10,就是简体中文。

第4,输入SA的密码,密码要求是至少8位,包含数字、大小写字母、符号的,强安全性。

第5步,配置程序会创建mssql-server服务,设置开机自启动。

[root@localhost ~]# /opt/mssql/bin/mssql-conf setup 选择 SQL Server 的一个版本:

  1) Evaluation (免费,无生产许可,180 天限制)
  2) Developer (免费,无生产许可)
  3) Express (免费)
  4) Web (付费版)
  5) Standard (付费版)
  6) Enterprise (付费版)
  7) Enterprise Core (付费版)
  8) 我通过零售渠道购买了许可证并具有要输入的产品密钥。

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

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

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

可以从以下位置查看隐私声明: https://go.microsoft.com/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 密码策略要求,因为它不够复杂。
        密码必须至少包含 8 个字符,并包含以下四种字符集中的任意三种: 大写字母、小写字母、数字和符号。
输入 SQL Server 系统管理员密码: 
确认 SQL Server 系统管理员密码: 
正在配置 SQL Server...

Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. 安装程序已成功完成。SQL Server 正在启动。 (4)配置完后,要验证一下服务是否启动了

从命令的输出看,msql-server服务成功启动,主进程sqlservr,进程号3601

[root@localhost ~]# 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 一 2018-01-29 13:34:58 CST; 6min ago
     Docs: https://docs.microsoft.com/en-us/sql/linux
 Main PID: 3601 (sqlservr)
   CGroup: /system.slice/mssql-server.service
           ├─3601 /opt/mssql/bin/sqlservr
           └─3631 /opt/mssql/bin/sqlservr

1月 29 13:35:04 localhost sqlservr[3601]: 2018-01-29 13:35:04.94 spid11s   ....
1月 29 13:35:04 localhost sqlservr[3601]: 2018-01-29 13:35:04.94 spid11s   ....
1月 29 13:35:05 localhost sqlservr[3601]: 2018-01-29 13:35:05.75 spid11s   ....
1月 29 13:35:06 localhost sqlservr[3601]: 2018-01-29 13:35:06.08 spid11s   ....
1月 29 13:35:06 localhost sqlservr[3601]: 2018-01-29 13:35:06.10 spid22s   ....
1月 29 13:35:06 localhost sqlservr[3601]: 2018-01-29 13:35:06.10 spid22s   ....
1月 29 13:35:06 localhost sqlservr[3601]: 2018-01-29 13:35:06.19 spid22s   ....
1月 29 13:35:06 localhost sqlservr[3601]: 2018-01-29 13:35:06.28 spid6s    ....
1月 29 13:40:09 localhost sqlservr[3601]: 2018-01-29 13:40:09.61 spid52    ....
1月 29 13:40:09 localhost sqlservr[3601]: 2018-01-29 13:40:09.73 spid52    ....
Hint: Some lines were ellipsized, use -l to show in full.

通过其他命令验证一次,确实启动成功了:

[root@localhost ~]# ps -ef | grep mssql
mssql     3601     1  0 13:34 ?        00:00:00 /opt/mssql/bin/sqlservr
mssql     3631  3601  4 13:34 ?        00:00:23 /opt/mssql/bin/sqlservr
root      4113  3045  0 13:43 pts/0    00:00:00 grep --color=auto mssql

(5)如果要从远程连接SQL Server,那么打开防火墙上的SQL Server端口,默认端口是1433

[root@localhost ~]# firewall-cmd --zone=public --add-port=1433/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success

这样就安装成功了 后面讲 如何连接

连接数据库

(1)设置防火墙

要连接数据库,首先要打开防火墙上1433端口,也就是,增加tcp端口1433到公共区域,并且永久生效。

[root@localhost Desktop]# firewall-cmd --zone=public --add-port=1433/tcp --permanent
success
[root@localhost Desktop]# firewall-cmd --reload
success

(2)下载客户端工具的源、安装客户端工具

可以参考这个文章:安装sql server的客户端工具

也是非常简单的,先下载,然后安装。

[root@localhost Desktop]# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   193  100   193    0     0    106      0  0:00:01  0:00:01 --:--:--   106

[root@localhost Desktop]# yum install -y mssql-tools
Loaded plugins: fastestmirror, langpacks
packages-microsoft-com-prod                                                                         | 2.9 kB  00:00:00     
packages-microsoft-com-prod/primary_db                                                              | 4.6 kB  00:00:01     
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package mssql-tools.x86_64 0:14.0.1.246-1 will be installed
--> Processing Dependency: msodbcsql for package: mssql-tools-14.0.1.246-1.x86_64
--> Running transaction check
---> Package msodbcsql.x86_64 0:13.0.1.0-1 will be installed
--> Processing Dependency: unixODBC-utf16 for package: msodbcsql-13.0.1.0-1.x86_64
--> Processing Dependency: libodbcinst.so.2()(64bit) for package: msodbcsql-13.0.1.0-1.x86_64
--> Running transaction check
---> Package unixODBC-utf16.x86_64 0:2.3.1-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================
 Package                     Arch                Version                    Repository                                Size
===========================================================================================================================
Installing:
 mssql-tools                 x86_64              14.0.1.246-1               packages-microsoft-com-prod              249 k
Installing for dependencies:
 msodbcsql                   x86_64              13.0.1.0-1                 packages-microsoft-com-prod              3.8 M
 unixODBC-utf16              x86_64              2.3.1-1                    packages-microsoft-com-prod              329 k

Transaction Summary
===========================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 4.4 M
Installed size: 4.4 M
Downloading packages:
(1/3): mssql-tools-14.0.1.246-1.x86_64.rpm                                                          | 249 kB  00:00:03     
(2/3): unixODBC-utf16-2.3.1-1.x86_64.rpm                                                            | 329 kB  00:00:01     
(3/3): msodbcsql-13.0.1.0-1.x86_64.rpm                                                              | 3.8 MB  00:00:21     
---------------------------------------------------------------------------------------------------------------------------
Total                                                                                      211 kB/s | 4.4 MB  00:00:21     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : unixODBC-utf16-2.3.1-1.x86_64                                                                           1/3 
The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746838 and found in
/usr/share/doc/msodbcsql/LICENSE.TXT . By entering 'YES',
you indicate that you accept the license terms.

Do you accept the license terms? (Enter YES or NO)
yes
Please enter YES or NO
Do you accept the license terms? (Enter YES or NO)
YES
  Installing : msodbcsql-13.0.1.0-1.x86_64                                                                             2/3 
The license terms for this product can be downloaded from
http://go.microsoft.com/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
  Installing : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3 
  Verifying  : msodbcsql-13.0.1.0-1.x86_64                                                                             1/3 
  Verifying  : unixODBC-utf16-2.3.1-1.x86_64                                                                           2/3 
  Verifying  : mssql-tools-14.0.1.246-1.x86_64                                                                         3/3 

Installed:
  mssql-tools.x86_64 0:14.0.1.246-1                                                                                        

Dependency Installed:
  msodbcsql.x86_64 0:13.0.1.0-1                               unixODBC-utf16.x86_64 0:2.3.1-1                              

Complete!

(3)连接sql sever 这里用sqlcmd来连接sql server,下面是一些命令行参数。 必要时 创建软连接

[root@localhost ~]# ln -sfn /opt/mssql-tools/bin/sqlcmd /usr/bin/sqlcmd

[root@localhost Desktop]# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 14.0.0001.246 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]

这里的-S是指定服务器名称,-U指定用户名,回车后会提示输入密码。 接下来查询当前是哪个库、创建了一个叫test的库、创建了一个叫tb的表、查询tb表的记录数。

[root@localhost Desktop]# sqlcmd -S localhost -U sa
Password: 

1> select db_name();
2> go
                                                                                                                                
--------------------------------------------------------------------------------------------------------------------------------
master                                                                                                                          

(1 rows affected)
1> create database test;
2> go
1> use test;
2> go
Changed database context to 'test'.
1> select * into tb from sys.tables;
2> go

(1 rows affected)
1> select count(*) from tb;
2> go
           
-----------
          1

(1 rows affected)
1> 

创建 数据库

1> show tables ;
2> go
Msg 2812, Level 16, State 62, Server h2, Line 1
Could not find stored procedure 'show'.
1> go
1> select table_name();
2> go
Msg 195, Level 15, State 10, Server h2, Line 1
'table_name' is not a recognized built-in function name.
1> 
1> 
1> create table  sst
2> (  
3>   id int primary key,  
4>   name  varchar(50),  
5>   weight   int  
6> )  
7> go
1> create table  sst
2> ;
3> go