RedHat Linux 7.5 安装 mssql-server
1、安装部署所需的依赖包
[root@localhost ~]# yum -y install libatomic bzip2 gdb cyrus-sasl cyrus-sasl-gssapi
Loaded plugins: ulninfo
Resolving Dependencies
--> Running transaction check
---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
---> Package cyrus-sasl.x86_64 0:2.1.26-23.el7 will be installed
---> Package cyrus-sasl-gssapi.x86_64 0:2.1.26-23.el7 will be installed
---> Package gdb.x86_64 0:7.6.1-110.el7 will be installed
---> Package libatomic.x86_64 0:4.8.5-28.0.1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
bzip2 x86_64 1.0.6-13.el7 oel-media 51 k
cyrus-sasl x86_64 2.1.26-23.el7 oel-media 88 k
cyrus-sasl-gssapi x86_64 2.1.26-23.el7 oel-media 41 k
gdb x86_64 7.6.1-110.el7 oel-media 2.4 M
libatomic x86_64 4.8.5-28.0.1.el7 oel-media 48 k
Transaction Summary
===============================================================================================================================================================================================
Install 5 Packages
Total download size: 2.6 M
Installed size: 7.3 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 67 MB/s | 2.6 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : bzip2-1.0.6-13.el7.x86_64 1/5
Installing : cyrus-sasl-2.1.26-23.el7.x86_64 2/5
Installing : cyrus-sasl-gssapi-2.1.26-23.el7.x86_64 3/5
Installing : libatomic-4.8.5-28.0.1.el7.x86_64 4/5
Installing : gdb-7.6.1-110.el7.x86_64 5/5
Verifying : gdb-7.6.1-110.el7.x86_64 1/5
Verifying : libatomic-4.8.5-28.0.1.el7.x86_64 2/5
Verifying : cyrus-sasl-gssapi-2.1.26-23.el7.x86_64 3/5
Verifying : cyrus-sasl-2.1.26-23.el7.x86_64 4/5
Verifying : bzip2-1.0.6-13.el7.x86_64 5/5
Installed:
bzip2.x86_64 0:1.0.6-13.el7 cyrus-sasl.x86_64 0:2.1.26-23.el7 cyrus-sasl-gssapi.x86_64 0:2.1.26-23.el7 gdb.x86_64 0:7.6.1-110.el7 libatomic.x86_64 0:4.8.5-28.0.1.el7
Complete!
2、安装 mssql-server
[root@localhost ~]# rpm -ivh mssql-server-14.0.1000.169-2.x86_64.rpm
warning: mssql-server-14.0.1000.169-2.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
error: Failed dependencies:
libsss_nss_idmap is needed by mssql-server-14.0.1000.169-2.x86_64.rpm
lsof is needed by mssql-server-14.0.1000.169-2.x86_64.rpm
[root@localhost ~]#
这里提示依赖包未安装,安装即可:
[root@localhost ~]# yum -y install lsof libsss_nss_idmap
Loaded plugins: ulninfo
Resolving Dependencies
--> Running transaction check
---> Package libsss_nss_idmap.x86_64 0:1.16.0-19.el7 will be installed
---> Package lsof.x86_64 0:4.87-5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
libsss_nss_idmap x86_64 1.16.0-19.el7 oel-media 147 k
lsof x86_64 4.87-5.el7 oel-media 330 k
Transaction Summary
===============================================================================================================================================================================================
Install 2 Packages
Total download size: 477 k
Installed size: 1.0 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 15 MB/s | 477 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libsss_nss_idmap-1.16.0-19.el7.x86_64 1/2
Installing : lsof-4.87-5.el7.x86_64 2/2
Verifying : lsof-4.87-5.el7.x86_64 1/2
Verifying : libsss_nss_idmap-1.16.0-19.el7.x86_64 2/2
Installed:
libsss_nss_idmap.x86_64 0:1.16.0-19.el7 lsof.x86_64 0:4.87-5.el7
Complete!
解决完依赖包后继续安装:
[root@localhost ~]# rpm -ivh mssql-server-14.0.1000.169-2.x86_64.rpm
warning: mssql-server-14.0.1000.169-2.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mssql-server-14.0.1000.169-2 ################################# [100%]
+--------------------------------------------------------------+
Please run 'sudo /opt/mssql/bin/mssql-conf setup'
to complete the setup of Microsoft SQL Server
+--------------------------------------------------------------+
[root@localhost ~]#
3、配置并启动数据库
[root@localhost ~]# 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)
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...
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.
[root@localhost ~]#
4、检查mssql-server的服务启动状态
[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 Fri 2024-07-26 14:53:41 CST; 2min 17s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 10457 (sqlservr)
CGroup: /system.slice/mssql-server.service
├─10457 /opt/mssql/bin/sqlservr
└─10478 /opt/mssql/bin/sqlservr
Jul 26 14:53:47 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:47.50 spid6s 8 transactions rolled forward in database 'msdb' (4:0). This is an informational messag...s required.
Jul 26 14:53:47 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:47.52 spid6s 0 transactions rolled back in database 'msdb' (4:0). This is an informational message o...s required.
Jul 26 14:53:47 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:47.57 spid9s Polybase feature disabled.
Jul 26 14:53:47 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:47.57 spid9s Clearing tempdb database.
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.22 spid9s Starting up database 'tempdb'.
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.59 spid9s The tempdb database has 1 data file(s).
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.60 spid24s The Service Broker endpoint is in disabled or stopped state.
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.60 spid24s The Database Mirroring endpoint is in disabled or stopped state.
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.62 spid24s Service Broker manager has started.
Jul 26 14:53:48 localhost.localdomain sqlservr[10457]: 2024-07-26 14:53:48.62 spid6s Recovery is complete. This is an informational message only. No user action is required.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
5、配置数据库
可以通过 mssql-conf 命令进行一些简单的配置,如配置监听端口,默认的数据文件目录,日志目录,备份目录等,该命令能够配置的项目如下。
[root@localhost ~]# /opt/mssql/bin/mssql-conf list
coredump.captureminiandfull Capture both mini and full core dumps
coredump.coredumptype Core dump type to capture: mini, miniplus, filtered, full
filelocation.defaultbackupdir Default directory for backup files
filelocation.defaultdatadir Default directory for data files
filelocation.defaultdumpdir Default directory for crash dump files
filelocation.defaultlogdir Default directory for log files
hadr.hadrenabled Allow SQL Server to use availability groups for high availability and disaster recovery
language.lcid Locale identifier for SQL Server to use (e.g. 1033 for US - English)
memory.memorylimitmb SQL Server memory limit (megabytes)
network.forceencryption Force encryption of incoming client connections
network.ipaddress IP address for incoming connections
network.kerberoskeytabfile Kerberos keytab file location
network.tcpport TCP port for incoming connections
network.tlscert Path to certificate file for encrypting incoming client connections
network.tlsciphers TLS ciphers allowed for encrypted incoming client connections
network.tlskey Path to private key file for encrypting incoming client connections
network.tlsprotocols TLS protocol versions allowed for encrypted incoming client connections
sqlagent.databasemailprofile SQL Agent Database Mail profile name
sqlagent.errorlogfile SQL Agent log file path
sqlagent.errorlogginglevel SQL Agent logging level bitmask - 1=Errors, 2=Warnings, 4=Info
telemetry.customerfeedback Telemetry status
telemetry.userrequestedlocalauditdirectory Directory for telemetry local audit cache
6、安装 mssql-tool 工具
服务器上如果想通过 sqlcmd 命令行登入数据库,还需要额外安装一个 mssql-tools 的RPM包,这个安装包里包括sqlcmd和bcp指令(Bulk import-export utility),注意安装mssql-tool,需要先安装msodbcsql包。
安装步骤如下:
[root@localhost ~]# rpm -ivh msodbcsql-13.1.3.0-1.x86_64.rpm
warning: msodbcsql-13.1.3.0-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
error: Failed dependencies:
libodbcinst.so.2()(64bit) is needed by msodbcsql-13.1.3.0-1.x86_64
unixODBC >= 2.3.1 is needed by msodbcsql-13.1.3.0-1.x86_64
[root@localhost ~]#
提示需要的依赖包未安装,直接安装即可:
[root@localhost ~]# yum -y install libodbcinst.so.2 unixODBC
Loaded plugins: ulninfo
Resolving Dependencies
--> Running transaction check
---> Package unixODBC.i686 0:2.3.1-11.el7 will be installed
--> Processing Dependency: libreadline.so.6 for package: unixODBC-2.3.1-11.el7.i686
--> Processing Dependency: libpthread.so.0(GLIBC_2.0) for package: unixODBC-2.3.1-11.el7.i686
--> Processing Dependency: libpthread.so.0 for package: unixODBC-2.3.1-11.el7.i686
--> Processing Dependency: libltdl.so.7 for package: unixODBC-2.3.1-11.el7.i686
--> Processing Dependency: libdl.so.2 for package: unixODBC-2.3.1-11.el7.i686
--> Processing Dependency: libc.so.6(GLIBC_2.7) for package: unixODBC-2.3.1-11.el7.i686
---> Package unixODBC.x86_64 0:2.3.1-11.el7 will be installed
--> Running transaction check
---> Package glibc.i686 0:2.17-222.el7 will be installed
--> Processing Dependency: libfreebl3.so(NSSRAWHASH_3.12.3) for package: glibc-2.17-222.el7.i686
--> Processing Dependency: libfreebl3.so for package: glibc-2.17-222.el7.i686
---> Package libtool-ltdl.i686 0:2.4.2-22.el7_3 will be installed
---> Package readline.i686 0:6.2-10.el7 will be installed
--> Processing Dependency: libtinfo.so.5 for package: readline-6.2-10.el7.i686
--> Running transaction check
---> Package ncurses-libs.i686 0:5.9-14.20130511.el7_4 will be installed
--> Processing Dependency: libstdc++.so.6(GLIBCXX_3.4) for package: ncurses-libs-5.9-14.20130511.el7_4.i686
--> Processing Dependency: libstdc++.so.6(CXXABI_1.3) for package: ncurses-libs-5.9-14.20130511.el7_4.i686
--> Processing Dependency: libstdc++.so.6 for package: ncurses-libs-5.9-14.20130511.el7_4.i686
--> Processing Dependency: libgcc_s.so.1(GCC_3.0) for package: ncurses-libs-5.9-14.20130511.el7_4.i686
--> Processing Dependency: libgcc_s.so.1 for package: ncurses-libs-5.9-14.20130511.el7_4.i686
---> Package nss-softokn-freebl.i686 0:3.34.0-2.0.1.el7 will be installed
--> Running transaction check
---> Package libgcc.i686 0:4.8.5-28.0.1.el7 will be installed
---> Package libstdc++.i686 0:4.8.5-28.0.1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================================================================
Installing:
unixODBC i686 2.3.1-11.el7 oel-media 410 k
unixODBC x86_64 2.3.1-11.el7 oel-media 412 k
Installing for dependencies:
glibc i686 2.17-222.el7 oel-media 4.2 M
libgcc i686 4.8.5-28.0.1.el7 oel-media 108 k
libstdc++ i686 4.8.5-28.0.1.el7 oel-media 316 k
libtool-ltdl i686 2.4.2-22.el7_3 oel-media 49 k
ncurses-libs i686 5.9-14.20130511.el7_4 oel-media 316 k
nss-softokn-freebl i686 3.34.0-2.0.1.el7 oel-media 206 k
readline i686 6.2-10.el7 oel-media 188 k
Transaction Summary
===============================================================================================================================================================================================
Install 2 Packages (+7 Dependent packages)
Total download size: 6.2 M
Installed size: 20 M
Downloading packages:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 20 MB/s | 6.2 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : libgcc-4.8.5-28.0.1.el7.i686 1/9
Installing : nss-softokn-freebl-3.34.0-2.0.1.el7.i686 2/9
Installing : glibc-2.17-222.el7.i686 3/9
Installing : libtool-ltdl-2.4.2-22.el7_3.i686 4/9
Installing : libstdc++-4.8.5-28.0.1.el7.i686 5/9
Installing : unixODBC-2.3.1-11.el7.x86_64 6/9
Installing : ncurses-libs-5.9-14.20130511.el7_4.i686 7/9
Installing : readline-6.2-10.el7.i686 8/9
Installing : unixODBC-2.3.1-11.el7.i686 9/9
Verifying : glibc-2.17-222.el7.i686 1/9
Verifying : readline-6.2-10.el7.i686 2/9
Verifying : libgcc-4.8.5-28.0.1.el7.i686 3/9
Verifying : unixODBC-2.3.1-11.el7.x86_64 4/9
Verifying : libtool-ltdl-2.4.2-22.el7_3.i686 5/9
Verifying : libstdc++-4.8.5-28.0.1.el7.i686 6/9
Verifying : ncurses-libs-5.9-14.20130511.el7_4.i686 7/9
Verifying : unixODBC-2.3.1-11.el7.i686 8/9
Verifying : nss-softokn-freebl-3.34.0-2.0.1.el7.i686 9/9
Installed:
unixODBC.i686 0:2.3.1-11.el7 unixODBC.x86_64 0:2.3.1-11.el7
Dependency Installed:
glibc.i686 0:2.17-222.el7 libgcc.i686 0:4.8.5-28.0.1.el7 libstdc++.i686 0:4.8.5-28.0.1.el7 libtool-ltdl.i686 0:2.4.2-22.el7_3 ncurses-libs.i686 0:5.9-14.20130511.el7_4
nss-softokn-freebl.i686 0:3.34.0-2.0.1.el7 readline.i686 0:6.2-10.el7
Complete!
继续安装 msodbcsql:
[root@localhost ~]# rpm -ivh msodbcsql-13.1.3.0-1.x86_64.rpm
warning: msodbcsql-13.1.3.0-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Preparing... ################################# [100%]
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
Updating / installing...
1:msodbcsql-13.1.3.0-1 ################################# [100%]
[root@localhost ~]#
然后在安装 mssql-tool:
[root@localhost ~]# rpm -ivh mssql-tools-14.0.2.0-1.x86_64.rpm
warning: mssql-tools-14.0.2.0-1.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Preparing... ################################# [100%]
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
Updating / installing...
1:mssql-tools-14.0.2.0-1 ################################# [100%]
[root@localhost ~]#
配置 mssql-tool 环境:
将 /opt/mssql-tools/bin/ 添加到 PATH 环境变量:
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
7、通过 sqlcmd 登录数据库并管理数据库
sql server 基本命令
使用 sqlcmd 本地连接到 SQL Server 实例:
sqlcmd 连接参数选项:
-S -服务名称
-U -用户名
注意:本地连接服务器名称为 localhost。 用户名为 SA,密码是在安装过程中为 SA 帐户提供的密码。
使用 sqlcmd 连接数据库:
[root@localhost bin]# ./sqlcmd-13.0.1.0 -S localhost -U sa
Password: 输入之前配置的密码
1>
注意:如果连接成功,会显示 sqlcmd 命令提示符:1>
注意:如果是远程连接,请指定 -S 参数的 IP 地址,并确保端口 1433 已打开。
8、SQL Server基本命令使用示例
创建数据库:
> create database ZZHDB
> go
查看数据库列表:
> select * from SysDatabases
> go
1> select * from SysDatabases
2> go
name dbid sid mode status status2 crdate reserved category cmptlevel filename version
------------------ ------ ------ ----------- ----------- ----------------------- ----------------------- ----------- --------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------
master 1 0x01 0 65544 1090520064 2003-04-08 09:13:36.390 1900-01-01 00:00:00.000 0 140 /var/opt/mssql/data/master.mdf 869
tempdb 2 0x01 0 65544 1090520064 2024-07-26 14:53:48.600 1900-01-01 00:00:00.000 0 140 /var/opt/mssql/data/tempdb.mdf 869
model 3 0x01 0 65536 1090519040 2003-04-08 09:13:36.390 1900-01-01 00:00:00.000 0 140 /var/opt/mssql/data/model.mdf 869
msdb 4 0x01 0 65544 1627390976 2017-08-22 19:39:22.887 1900-01-01 00:00:00.000 0 140 /var/opt/mssql/data/MSDBData.mdf 869
ZZHDB 5 0x01 0 65536 1627389952 2024-07-26 15:23:41.363 1900-01-01 00:00:00.000 0 140 /var/opt/mssql/data/ZZHDB.mdf 869
查看数据库有那些数据表:
1> use msdb
2> go
Changed database context to 'msdb'.
1> select * from sysobjects where xtype='u'
2> go
输出的数据表太多这里省略。
查看数据表数据:
1> use ZZHDB
2> create table zzh_tbs (id int,name nvarchar(50))
2> go
1> insert into zzh_tbs values(1,'zzh');
2> insert into zzh_tbs values(2,'ahern');
3> select * from zzh_tbs;
4> go
(1 rows affected)
(1 rows affected)
id name
----------- --------------------------------------------------
1 zzh
2 ahern
(2 rows affected)
注意:要执行上述连续输入的 inser、select 等命令的类型 为GO。