本文介绍在RHEL/CentOS 8操作系统上安装Microsoft SQL Server(MS SQL)的方法,它是Microsoft的一个关系数据库系统,从SQL Server 2017开始,它可用于生产RHEL和其它Linux发行版上的工作负载。在CentOS/RHEL 8上运行MS SQL Server的最低系统要求是:最小内存为3GB、CPU处理器最低速度为1.4GHz(建议>=2GHz)、SQL Server至少需要10GB的可用硬盘空间。
一、添加Microsoft SQL Server 2017存储库
Red Hat存储库上提供了Microsoft SQL数据库服务器软件包,需要手动添加:
sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo
关闭Linux Firewalls和selinux
查看状态: systemctl status firewalld
临时关闭: systemctl stop firewalld
开机禁用: systemctl disable firewalld
重新加载:firewall-cmd --reload
关闭selinux:vim /etc/sysconfig/selinux
SELINUX=enforcing 改为 SELINUX=disabled
二、在RHEL/CentOS 8上安装MS SQL server
SQL Server 2017使用python2和OpenSSL 1.0,你需要在不解析依赖关系的情况下安装软件包:
sudo yum -y install python2 compat-openssl10
sudo alternatives --set python /usr/bin/python2
下载mssql-server安装包
我这里是把下载地址拷贝到浏览器直接下载,用yum下载的话太慢,下载好后,拷贝到Linux上即可:
https://packages.microsoft.com/rhel/7/mssql-server-2017/mssql-server-14.0.3294.2-27.x86_64.rpm
找到安装包的位置,安装
sudo rpm -Uvh --nodeps mssql-server*rpm
源码:
[root@linuxprobe MSSQL]# rpm -Uvh --nodeps mssql-server*rpm
warning: mssql-server-14.0.3294.2-27.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mssql-server-14.0.3294.2-27 ################################# [100%]
+--------------------------------------------------------------+
Please run 'sudo /opt/mssql/bin/mssql-conf setup'
to complete the setup of Microsoft SQL Server
+--------------------------------------------------------------+
SQL Server needs to be restarted in order to apply this setting. Please run
'systemctl restart mssql-server.service'.
三、安装SQL Server命令行工具
使用unixODBC开发包安装mssql-tools:
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
sudo yum -y install mssql-tools unixODBC-devel
源码:
[root@linuxprobe MSSQL]# yum -y install mssql-tools unixODBC-devel
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos,
: subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
* epel: mirrors.njupt.edu.cn
packages-microsoft-com-prod | 3.0 kB 00:00
packages-microsoft-com-prod/primary_db | 261 kB 00:13
Resolving Dependencies
--> Running transaction check
---> Package mssql-tools.x86_64 0:17.5.2.1-1 will be installed
--> Processing Dependency: msodbcsql17 >= 17.3.0.0 for package: mssql-tools-17.5.2.1-1.x86_64
---> Package unixODBC-devel.x86_64 0:2.3.7-1.rh will be installed
--> Processing Dependency: unixODBC = 2.3.7 for package: unixODBC-devel-2.3.7-1.rh.x86_64
--> Running transaction check
---> Package msodbcsql17.x86_64 0:17.5.2.1-1 will be installed
---> Package unixODBC.x86_64 0:2.3.7-1.rh will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mssql-tools x86_64 17.5.2.1-1 packages-microsoft-com-prod 223 k
unixODBC-devel x86_64 2.3.7-1.rh packages-microsoft-com-prod 42 k
Installing for dependencies:
msodbcsql17 x86_64 17.5.2.1-1 packages-microsoft-com-prod 802 k
unixODBC x86_64 2.3.7-1.rh packages-microsoft-com-prod 213 k
Transaction Summary
================================================================================
Install 2 Packages (+2 Dependent packages)
Total download size: 1.3 M
Installed size: 3.8 M
Downloading packages:
warning: /var/cache/yum/x86_64/$releasever/packages-microsoft-com-prod/packages/mssql-tools-17.5.2.1-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Public key for mssql-tools-17.5.2.1-1.x86_64.rpm is not installed
(1/4): mssql-tools-17.5.2.1-1.x86_64.rpm | 223 kB 00:01
(2/4): unixODBC-2.3.7-1.rh.x86_64.rpm | 213 kB 00:00
(3/4): unixODBC-devel-2.3.7-1.rh.x86_64.rpm | 42 kB 00:00
(4/4): msodbcsql17-17.5.2.1-1.x86_64.rpm | 802 kB 00:02
--------------------------------------------------------------------------------
Total 495 kB/s | 1.3 MB 00:02
Retrieving key from https://packages.microsoft.com/keys/microsoft.asc
Importing GPG key 0xBE1229CF:
Userid : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
Fingerprint: bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
From : https://packages.microsoft.com/keys/microsoft.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : unixODBC-2.3.7-1.rh.x86_64 1/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
Installing : msodbcsql17-17.5.2.1-1.x86_64 2/4
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-17.5.2.1-1.x86_64 3/4
Installing : unixODBC-devel-2.3.7-1.rh.x86_64 4/4
Verifying : mssql-tools-17.5.2.1-1.x86_64 1/4
Verifying : msodbcsql17-17.5.2.1-1.x86_64 2/4
Verifying : unixODBC-devel-2.3.7-1.rh.x86_64 3/4
Verifying : unixODBC-2.3.7-1.rh.x86_64 4/4
Installed:
mssql-tools.x86_64 0:17.5.2.1-1 unixODBC-devel.x86_64 0:2.3.7-1.rh
Dependency Installed:
msodbcsql17.x86_64 0:17.5.2.1-1 unixODBC.x86_64 0:2.3.7-1.rh
Complete!
确认安装是否成功,运行rpm -qi mssql-server命令查看:
[root@linuxprobe MSSQL]# rpm -qi mssql-server
Name : mssql-server
Version : 14.0.3294.2
Release : 27
Architecture: x86_64
Install Date: Sat 18 Apr 2020 11:48:52 AM CST
Group : Unspecified
Size : 1019732362
License : Commercial
Signature : RSA/SHA256, Fri 10 Apr 2020 04:56:19 AM CST, Key ID eb3e94adbe1229cf
Source RPM : mssql-server-14.0.3294.2-27.src.rpm
Build Date : Fri 10 Apr 2020 03:59:06 AM CST
Build Host : hls-cent3-prod-build-cent73-00
Relocations : (not relocatable)
Summary : Microsoft SQL Server Relational Database Engine
Description :
The mssql-server package contains the Microsoft SQL Server Relational Database Engine.
注:可以看出版本和我们下载的安装包版本一致!
四、初始化MS SQL数据库引擎
程序包安装完成后,运行mssql-conf setup并按照提示设置SA密码并选择版本:
sudo /opt/mssql/bin/mssql-conf setup
各个选项说明:
1.选择要使用的版本
我选择2,即开发人员(免费,没有生产使用权)
Choose an edition of SQL Server:
- Evaluation (free, no production use rights, 180-day limit)
- Developer (free, no production use rights)
- Express (free)
- Web (PAID)
- Standard (PAID)
- Enterprise (PAID)
- Enterprise Core (PAID)
- I bought a license through a retail sales channel and have a product key to enter.
2.接受许可条款
The license terms for this product can be found in
/usr/share/doc/mssql-server
Do you accept the license terms? [Yes/No]:Yes
3.设置SQL Server系统管理员密码
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server…
sqlservr: This program requires a machine with at least 2000 megabytes of memory.
/opt/mssql/bin/sqlservr: This program requires a machine with at least 2000 megabytes of memory.
Initial setup of Microsoft SQL Server failed. Please consult the ERRORLOG
in /var/opt/mssql/log for more information.
源码:
[root@linuxprobe MSSQL]# /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)
7) Enterprise Core (PAID)
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...
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-server服务
启动mssql-server服务:
sudo systemctl start mssql-server
启用它以在系统引导时启动
sudo systemctl enable mssql-server
将/opt/mssql/bin/添加到$PATH变量:
echo 'export PATH=$PATH:/opt/mssql/bin' | sudo tee /etc/profile.d/mssql.sh
源文件以在当前shell会话中开始使用MSSQL可执行二进制文件:
source /etc/profile.d/mssql.sh
六、测试连接 SQL Server
打开本地的Navicat 创建连接,然后测试连接
这里用的账号是sa,密码是我们在安装的过程中设置的密码,如下图:
注意:当我们打开数据的时候,会发现啥都没有:
没关系,开始创建数据库即可:
显示数据库用户:
select name from sysusers;
go
创建测试数据库:
CREATE DATABASE testDB
SELECT Name from sys.Databases
GO
USE testDB
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
GO
SELECT * FROM Inventory LIMIT 1;
删除数据库:
drop database testDB;
go
至此,安装并测试SQL Server内容结束
参考:
https://ywnz.com/linuxysjk/4607.html
https://ywnz.com/linuxysjk/4062.html
https://www.cnblogs.com/guarderming/p/10375557.html