Linux上如何安装Microsoft SQL

分享于 

14分钟阅读

CentOS

  繁體

在本文中,我们讨论如何在Linux上安装Microsoft SQL或MSSQL ,MSSQL提供了它开源对应的一些特性,根据应用程序需求,它可能是RDBMS的正确选择,在本教程中,我们介绍如何在CentOS 和Ubuntu上安装MSSQL 。

检查

你的服务器至少有2GB内存,以root用户在CentOS和Ubuntu 服务器上执行。

centos

步骤1:添加MSSQL 2019预览库

首先,最好确保所有软件包都是最新的:

root@centos ~]# yum update -y

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

root@centos ~]# 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 repo,我们可以使用yum来安装软件包:

root@centos ~]# yum install -y mssql-server

步骤3:配置MSSQL Server

接下来,我们需要使用系统管理员密码配置SQL并确认要使用的版本,本教程将免费使用Developer版本的,选择2:


root@centos ~]# /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.

然后,需要验证sql mssql服务是否正在运行:

root@centos ~]# 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,否则不需要打开此端口。

root@centos ~]# firewall-cmd --zone=public --add-port=1433/tcp --permanent

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


[root@centos ~]# firewall-cmd --reload
success
root@centos ~]# firewall-cmd --list-ports
1433/tcp

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

现在我们需要一种与SQL Server互动的方法。 首先,添加另一个repo,这样就可以使用yum来安装SQL Server命令行工具,

root@centos ~]# curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo

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

现在yum知道要安装的包了,我们需要安装它们,要注意的是,在安装这些软件包期间,需要有两个交互式提示来接受许可条款:

root@centos ~]# yum install -y mssql-tools unixODBC-devel

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

/opt/mssql-tools/bin/ 

返回到服务器上的路径变量,以便我们可以从服务器上的位置执行sql命令:


root@centos ~]# echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

root@centos ~]# source ~/.bashrc

root@centos ~]# sqlcmd -S localhost -U SA
Password:
1>

Ubuntu 18.04

步骤1:添加MSSQL Server Ubuntu 2019预览库

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

root@ubuntu1604:~# apt-get update -y

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

t@ubuntu1604:~# wget -qO- https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

现在GPG密钥已经就绪,我们可以添加存储库了:

root@ubuntu1604:~# add-apt-repository"$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-preview.list)"

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

root@ubuntu1604:~# apt-get install -y apt-transport-https

步骤2:安装MSSQL Server

现在包含MSSQL Server软件包的存储库已可用,剩下要做的就是确保apt知道新的存储库并安装MSSQL Server :


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

步骤3:配置MSSQL Server

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


root@ubuntu1604:~# /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.

sql Server现在应该正在运行并启用,


root@ubuntu1604:~# 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,否则不需要打开此端口。

root@ubuntu1604:~# apt-get install -y ufw

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


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

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


root@ubuntu1604:~# ufw allow 1433
Rule added
Rule added (v6)

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

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

root@ubuntu1604:~# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

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

root@ubuntu1604:~# curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | tee /etc/apt/sources.list.d/msprod.list

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


root@ubuntu1604:~# apt-get update -y
t@ubuntu1604:~# apt-get install -y mssql-tools unixodbc-dev

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

轻松地在服务器上的位置执行sql cmd :


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

最后,验证我们是否可以在本地连接到MSSQL服务器:


root@ubuntu1604:~# sqlcmd -S localhost -U SA
Password:
1>

micr  Microsoft  
相关文章