使用 sqlcmd 在 MS SQL Server 上执行 Transact-SQL

使用 SA 用户和密码连接到 MS SQL Server 实例。

[root@sqlserver-01 ~]# sqlcmd -S localhost -U SA -P 'JackLi@1234'

我们现在处于 Transact-SQL (T-SQL) 命令提示符处。
执行查询如下:

1> select name from sys.databases;
2> go
name                                                                            
---------------------
master                                                                          
tempdb                                                                          
model                                                                           
msdb                                                                            
(4 rows affected)

现在,在我们的 MS SQL Server 2019 实例上创建一个自定义数据库。

1> create database contacts;
2> go

在联系人数据库中创建一个表。

1> use contacts;
2> create table contacts_list
3> (contact_id char(6) primary key,
4> contact_name char(30));
5> go
Changed database context to 'contacts'.

检查我们新创建的表。

1> select name from sys.tables;
2> go
name                                                                            
----------------------
contacts_list                                                                   
(1 rows affected)

在contacts_list 表中插入一行。

1> use contacts;
2> insert into contacts_list
3> values
4> ('101','JackLi Mansoor');
5> go
Changed database context to 'contacts'.
(1 rows affected)

我们可以使用 sqlcmd 执行任何 Transact-SQL (T-SQL) 命令。
但是为了保持简短,我们没有在本文中展示每个命令。

从 Transact-SQL (T-SQL) 命令提示符退出。

1> quit

我们已经在 CentOS 7 上成功配置了 MS SQL Server 2019.

在 CentOS 7 上安装 MS SQL Server 2019

SQL Server 是由 Microsoft 开发的关系数据库管理系统 (RDBMS)。

MS SQL Server 是一个依赖于平台的 RDBMS,它只支持不同版本的 Microsoft Windows。
但是,微软在 MS SQL Server 2017 中增加了对 Linux 平台的支持。
因此,我们现在可以在 Windows、Linux 和 Docker 平台上安装 MS SQL Server 2017(或者更高版本)。

MS SQL Server 2017 是目前可供下载的稳定版本。
但是,在其预览版 yum 存储库中也提供了 MS SQL Server 2019 版本。

在本文中,我们将在 CentOS 7 机器上安装 MS SQL Server 2019,然后我们将使用 Transact-SQL (T-SQL) 创建一个测试数据库和一个表。

在 CentOS 7 上安装 MS SQL Server 2019

登录到服务器

下载并添加 MS SQL Server 2019(预览版)yum 存储库。

[root@sqlserver-01 ~]# curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-preview.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   240  100   240    0     0     62      0  0:00:03  0:00:03 --:--:--    62

构建yum缓存如下:

[root@sqlserver-01 ~]# yum makecache fast

使用 yum 命令在 CentOS 7 上安装 MS SQL Server 2019.

[root@sqlserver-01 ~]# yum install -y mssql-server

安装 MS SQL Server 2019 后,按照上面 yum 安装程序的建议运行安装程序。

[root@sqlserver-01 ~]# /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):

选择所需的 MS SQL Server 版本。
我们正在安装 MS SQL Server 的开发人员版。

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]:

我们可以在浏览器中查看许可和隐私声明。
阅读并同意许可条款以继续。

Do you accept the license terms? [Yes/No]:Yes
Enter the SQL Server system administrator password:
Set a Strong System Administrator (SA) Password for MS SQL Server Instance.
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...
This is an evaluation version.  There are [167] 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.

检查 MS SQL Server 服务的状态。

[root@sqlserver-01 ~]# systemctl status mssql-server.service

为了允许远程连接,我们必须在 Linux 防火墙中允许 MS SQL Server 默认端口 1433/tcp。

[root@sqlserver-01 ~]# firewall-cmd --permanent --add-port=1433/tcp
success
[root@sqlserver-01 ~]# firewall-cmd --reload
success
欢迎来到之路教程(on itroad-com)

在 CentOS 7 上安装 SQL Server 工具

我们已经配置了 MS SQL Server 2019,我们可以在基于 Windows 的客户端上使用 SQL Server Management Studio (SSMS) 来连接和使用我们的 MS SQL Server 2019.

但是,我们还安装了 MS SQL Server 命令行实用程序,因此我们可以直接在我们的数据库服务器上执行数据库管理任务。

下载并安装 Microsoft yum 存储库以安装 SQL Server 工具。

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

为新添加的 yum 存储库构建 yum 缓存。

[root@sqlserver-01 ~]# yum makecache fast

使用 yum 命令安装 SQL Server 工具。

[root@sqlserver-01 ~]# yum install -y mssql-tools

SQL Server 工具已安装在 /opt/mssql/bin/ 目录中。

[root@sqlserver-01 ~]# ls /opt/mssql-tools/bin/
bcp  sqlcmd

将此目录添加到 PATH 环境变量以方便访问。

[root@sqlserver-01 ~]# cat > /etc/profile.d/mssql-tools.sh << EOF
> #!/bin/bash
> export PATH=$PATH:/opt/mssql-tools/bin
> EOF

已在 CentOS 7 上安装 SQL Server 工具。

日期:2020-09-17 00:11:18 来源:oir作者:oir