当前位置:首页 » 《随便一记》 » 正文

Linux环境安装 SQL Server数据库以及使用

22 人参与  2023年05月03日 11:13  分类 : 《随便一记》  评论

点击全文阅读


Linux安装 SQL Server

一、环境二、安装 SQL Server三、安装 SQL Server 命令行工具四、本地连接五、创建和查询数据5.1、新建数据库5.2、插入数据5.3、查询数据5.4、退出 sqlcmd 命令提示符 总结

一、环境

虚拟机:VMware Workstation。系统:Ubuntu 20.04。文件系统:XFS 或 EXT4。内存:至少具有 2 GB 内存。磁盘空间:6 GB以上。处理器速度:2 GHz。处理器核心数:2 个核心。处理器类型:仅兼容 x64。安装SQL Server版本:2019。

如果以前安装了 SQL Server 的社区技术预览版 (CTP) 或候选发布 (RC) 版本,则必须先删除旧存储库。

二、安装 SQL Server

(1)导入公共存储库 GPG 密钥:

wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

输出:

-----BEGIN PGP PUBLIC KEY BLOCK-----Version: GnuPG v1.4.7 (GNU/Linux)mQENBFYxWIwBCADAKoZhZlJxGNGWzqV+1OG1xiQeoowKhssGAKvd+buXCGISZJwTLXZqIcIiLP7pqdcZWtE9bSc7yBY2MalDp9Liu0KekywQ6VVX1T72NPf5Ev6x6DLV7aVWsCzUAF+eb7DC9fPuFLEdxmOEYoPjzrQ7cCnSV4JQxAqhU4T6OjbvRazGl3agOeizPXmRljMtUUttHQZnRhtlzkmwIrUivbfFPD+fEoHJ1+uIdfOzZX8/oKHKLe2jH632kvsNzJFlROVvGLYAk2WRcLu+RjjggixhwiB+Mu/A8Tf4V6b+YppS44q8EvVrM+QvY7LNSOffSO6Slsy9oisGTdfE39nC7pVRABEBAAG0N01pY3Jvc29mdCAoUmVsZWFzZSBzaWduaW5nKSA8Z3Bnc2VjdXJpdHlAbWljcm9zb2Z0LmNvbT6JATUEEwECAB8FAlYxWIwCGwMGCwkIBwMCBBUCCAMDFgIBAh4BAheAAAoJEOs+lK2+EinPGpsH/32vKy29Hg51H9dfFJMx0/a/F+5vKeCeVqimvyTM04C+XENNuSbYZ3eRPHGHFLqeMNGxsfb7C7ZxEeW7J/vSzRgHxm7ZvESisUYRFq2sgkJ+HFERNrqfci45bdhmrUsy7SWw9ybxdFOkuQoyKD3tBmiGfONQMlBaOMWdAsic965rvJsd5zYaZZFI1UwTkFXVKJt3bp3Ngn1vEYXwijGTa+FXz6GLHueJwF0I7ug34DgUkAFvAs8Hacr2DRYxL5RJXdNgj4Jd2/g6T9InmWT0hASljur+dJnzNiNCkbn9KbX7J/qK1IbR8y560yRmFsU+NdCFTW7wY0Fb1fWJ+/KTsC4==J6gs-----END PGP PUBLIC KEY BLOCK-----

(2)注册 SQL Server Ubuntu 存储库:

sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"

输出:

Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal InReleaseGet:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates InRelease [114 kB]                           Get:3 https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial InRelease [8,993 B]                                                       Get:4 https://packages.microsoft.com/ubuntu/20.04/mssql-server-2019 focal InRelease [3,057 B]                                               Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-backports InRelease [108 kB]      Get:6 https://packages.microsoft.com/ubuntu/20.04/mssql-server-2019 focal/main all Packages [717 B]Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security InRelease [114 kB]Get:8 https://packages.microsoft.com/ubuntu/20.04/mssql-server-2019 focal/main amd64 Packages [8,083 B]Get:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 Packages [2,463 kB]Get:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main Translation-en [420 kB]                                                                                               Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/main amd64 c-n-f Metadata [16.4 kB]                                                                                        Get:12 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/restricted amd64 Packages [1,713 kB]                                                                                       Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/restricted Translation-en [242 kB]                                                                                         Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/restricted amd64 c-n-f Metadata [620 B]                                                                                    Get:15 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe amd64 Packages [1,045 kB]                                                                                         Get:16 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe Translation-en [246 kB]                                                                                           Get:17 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-updates/universe amd64 c-n-f Metadata [24.1 kB]                                                                                    Get:18 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/main amd64 Packages [2,082 kB]                                                                                            Get:19 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/main Translation-en [338 kB]                                                                                              Get:20 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/main amd64 c-n-f Metadata [12.5 kB]                                                                                       Get:21 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/restricted amd64 Packages [1,605 kB]                                                                                      Get:22 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/restricted Translation-en [227 kB]                                                                                        Get:23 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/restricted amd64 c-n-f Metadata [620 B]                                                                                   Get:24 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/universe amd64 Packages [820 kB]                                                                                          Get:25 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/universe Translation-en [164 kB]                                                                                          Get:26 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal-security/universe amd64 c-n-f Metadata [17.5 kB]                                                                                   Fetched 11.8 MB in 1min 1s (193 kB/s)                                                                                                                                                      Reading package lists... Done

(3)运行以下命令以安装 SQL Server:

sudo apt-get updatesudo apt-get install -y mssql-server

输出:

Fetched 269 MB in 1min 33s (2,903 kB/s)                                                                                                                                                    Preconfiguring packages ...Selecting previously unselected package libdw1:amd64.(Reading database ... 72772 files and directories currently installed.)Preparing to unpack .../00-libdw1_0.176-1.1build1_amd64.deb ...Unpacking libdw1:amd64 (0.176-1.1build1) ...Selecting previously unselected package libbabeltrace1:amd64.Preparing to unpack .../01-libbabeltrace1_1.5.8-1build1_amd64.deb ...Unpacking libbabeltrace1:amd64 (1.5.8-1build1) ...Selecting previously unselected package gdb.Preparing to unpack .../02-gdb_9.2-0ubuntu1~20.04.1_amd64.deb ...Unpacking gdb (9.2-0ubuntu1~20.04.1) ...Selecting previously unselected package gdbserver.Preparing to unpack .../03-gdbserver_9.2-0ubuntu1~20.04.1_amd64.deb ...Unpacking gdbserver (9.2-0ubuntu1~20.04.1) ...Selecting previously unselected package libatomic1:amd64.Preparing to unpack .../04-libatomic1_10.3.0-1ubuntu1~20.04_amd64.deb ...Unpacking libatomic1:amd64 (10.3.0-1ubuntu1~20.04) ...Selecting previously unselected package libc++abi1-10:amd64.Preparing to unpack .../05-libc++abi1-10_1%3a10.0.0-4ubuntu1_amd64.deb ...Unpacking libc++abi1-10:amd64 (1:10.0.0-4ubuntu1) ...Selecting previously unselected package libc++1-10:amd64.Preparing to unpack .../06-libc++1-10_1%3a10.0.0-4ubuntu1_amd64.deb ...Unpacking libc++1-10:amd64 (1:10.0.0-4ubuntu1) ...Selecting previously unselected package libcc1-0:amd64.Preparing to unpack .../07-libcc1-0_10.3.0-1ubuntu1~20.04_amd64.deb ...Unpacking libcc1-0:amd64 (10.3.0-1ubuntu1~20.04) ...Selecting previously unselected package libsasl2-modules-gssapi-mit:amd64.Preparing to unpack .../08-libsasl2-modules-gssapi-mit_2.1.27+dfsg-2ubuntu0.1_amd64.deb ...Unpacking libsasl2-modules-gssapi-mit:amd64 (2.1.27+dfsg-2ubuntu0.1) ...Selecting previously unselected package libc++1:amd64.Preparing to unpack .../09-libc++1_1%3a10.0-50~exp1_amd64.deb ...Unpacking libc++1:amd64 (1:10.0-50~exp1) ...Selecting previously unselected package libc6-dbg:amd64.Preparing to unpack .../10-libc6-dbg_2.31-0ubuntu9.9_amd64.deb ...Unpacking libc6-dbg:amd64 (2.31-0ubuntu9.9) ...Selecting previously unselected package libsss-nss-idmap0.Preparing to unpack .../11-libsss-nss-idmap0_2.2.3-3ubuntu0.10_amd64.deb ...Unpacking libsss-nss-idmap0 (2.2.3-3ubuntu0.10) ...Selecting previously unselected package mssql-server.Preparing to unpack .../12-mssql-server_15.0.4298.1-1_amd64.deb ...Unpacking mssql-server (15.0.4298.1-1) ...Setting up libdw1:amd64 (0.176-1.1build1) ...Setting up libc6-dbg:amd64 (2.31-0ubuntu9.9) ...Setting up libatomic1:amd64 (10.3.0-1ubuntu1~20.04) ...Setting up libbabeltrace1:amd64 (1.5.8-1build1) ...Setting up libc++abi1-10:amd64 (1:10.0.0-4ubuntu1) ...Setting up gdbserver (9.2-0ubuntu1~20.04.1) ...Setting up libsss-nss-idmap0 (2.2.3-3ubuntu0.10) ...Setting up libsasl2-modules-gssapi-mit:amd64 (2.1.27+dfsg-2ubuntu0.1) ...Setting up libcc1-0:amd64 (10.3.0-1ubuntu1~20.04) ...Setting up libc++1-10:amd64 (1:10.0.0-4ubuntu1) ...Setting up gdb (9.2-0ubuntu1~20.04.1) ...Setting up libc++1:amd64 (1:10.0-50~exp1) ...Setting up mssql-server (15.0.4298.1-1) ...+--------------------------------------------------------------+Please run 'sudo /opt/mssql/bin/mssql-conf setup'to complete the setup of Microsoft SQL Server+--------------------------------------------------------------+Processing triggers for man-db (2.9.1-1) ...Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

(4)包安装完成后,运行 mssql-conf setup,按照提示设置 SA 密码并选择版本。SQL Server 免费提供许可的版本:

Evaluation 版。Developer 版。Express 版。
sudo /opt/mssql/bin/mssql-conf setup

请记住为 SA 帐户指定强密码。 需要最小长度为 8 个字符,包括大写和小写字母、十进制数字和/或非字母数字符号。

设置过程:

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 athttps://go.microsoft.com/fwlink/?LinkId=2109348&clcid=0x409Use of PAID editions of this software requires separate licensing through aMicrosoft Volume Licensing program.By choosing a PAID edition, you are verifying that you have the appropriatenumber of licenses in place to install and run this software.Enter your edition(1-8): 3The license terms for this product can be found in/usr/share/doc/mssql-server or downloaded from:https://go.microsoft.com/fwlink/?LinkId=2104294&clcid=0x409The privacy statement can be viewed at:https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409Do you accept the license terms? [Yes/No]:yesEnter the SQL Server system administrator password: Confirm the SQL Server system administrator password: Configuring SQL Server...The licensing PID was successfully processed. The new edition is [Express Edition].ForceFlush is enabled for this instance. ForceFlush feature is enabled for log durability.Created symlink /etc/systemd/system/multi-user.target.wants/mssql-server.service → /lib/systemd/system/mssql-server.service.Setup has completed successfully. SQL Server is now starting.
SQL Server的版本选择:Express (free)。Do you accept the license terms? [Yes/No]:yes。SQL Server密码:Fly123456。

(5)完成配置后,验证服务是否正在运行:

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 2023-03-30 03:08:42 UTC; 2min 19s ago       Docs: https://docs.microsoft.com/en-us/sql/linux   Main PID: 54788 (sqlservr)      Tasks: 121     Memory: 796.3M     CGroup: /system.slice/mssql-server.service             ├─54788 /opt/mssql/bin/sqlservr             └─55080 /opt/mssql/bin/sqlservr

三、安装 SQL Server 命令行工具

若要创建数据库,则需要使用可在 SQL Server 上运行 Transact-SQL 语句的工具进行连接。
(1)安装 curl:

sudo apt-get updatesudo apt install curl

(2)导入公共存储库 GPG 密钥。

curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc

输出:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100   983  100   983    0     0   2482      0 --:--:-- --:--:-- --:--:--  2482-----BEGIN PGP PUBLIC KEY BLOCK-----Version: GnuPG v1.4.7 (GNU/Linux)mQENBFYxWIwBCADAKoZhZlJxGNGWzqV+1OG1xiQeoowKhssGAKvd+buXCGISZJwTLXZqIcIiLP7pqdcZWtE9bSc7yBY2MalDp9Liu0KekywQ6VVX1T72NPf5Ev6x6DLV7aVWsCzUAF+eb7DC9fPuFLEdxmOEYoPjzrQ7cCnSV4JQxAqhU4T6OjbvRazGl3agOeizPXmRljMtUUttHQZnRhtlzkmwIrUivbfFPD+fEoHJ1+uIdfOzZX8/oKHKLe2jH632kvsNzJFlROVvGLYAk2WRcLu+RjjggixhwiB+Mu/A8Tf4V6b+YppS44q8EvVrM+QvY7LNSOffSO6Slsy9oisGTdfE39nC7pVRABEBAAG0N01pY3Jvc29mdCAoUmVsZWFzZSBzaWduaW5nKSA8Z3Bnc2VjdXJpdHlAbWljcm9zb2Z0LmNvbT6JATUEEwECAB8FAlYxWIwCGwMGCwkIBwMCBBUCCAMDFgIBAh4BAheAAAoJEOs+lK2+EinPGpsH/32vKy29Hg51H9dfFJMx0/a/F+5vKeCeVqimvyTM04C+XENNuSbYZ3eRPHGHFLqeMNGxsfb7C7ZxEeW7J/vSzRgHxm7ZvESisUYRFq2sgkJ+HFERNrqfci45bdhmrUsy7SWw9ybxdFOkuQoyKD3tBmiGfONQMlBaOMWdAsic965rvJsd5zYaZZFI1UwTkFXVKJt3bp3Ngn1vEYXwijGTa+FXz6GLHueJwF0I7ug34DgUkAFvAs8Hacr2DRYxL5RJXdNgj4Jd2/g6T9InmWT0hASljur+dJnzNiNCkbn9KbX7J/qK1IbR8y560yRmFsU+NdCFTW7wY0Fb1fWJ+/KTsC4==J6gs-----END PGP PUBLIC KEY BLOCK-----

(3)注册 Ubuntu 存储库。

curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

(4)更新源列表,并使用 unixODBC 开发人员包运行安装命令。

sudo apt-get updatesudo apt-get install mssql-tools unixodbc-dev

通过运行以下命令将 mssql-tools 更新至最新版本:

sudo apt-get updatesudo apt-get install mssql-tools

(5)将 /opt/mssql-tools/bin/ 添加到 PATH环境变量,以便可从 bash shell 访问 sqlcmd 或 bcp。

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

或者

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrcsource ~/.bashrc

四、本地连接

使用 SQL Server 名称 (-S)、用户名 (-U) 和密码 (-P) 的相应参数运行 sqlcmd。 用户进行本地连接,服务器名称为 localhost。 用户名为 sa,密码是在安装过程中为 SA 帐户提供的密码。

sqlcmd -S localhost -U sa -P '<YourPassword>'

可以在命令行上省略密码,以收到密码输入提示。

如果以后决定进行远程连接,请为 -S 参数指定计算机名称或 IP 地址,并确保防火墙上的端口 1433 已打开。

(1)如果成功,应会显示 sqlcmd 命令提示符:

1>

(2)如果连接失败,先尝试诊断错误消息中所述的问题。 然后查看连接故障排除建议。

五、创建和查询数据

5.1、新建数据库

(1)在 sqlcmd 命令提示符中创建一个名为 TestDB 的新数据库。

CREATE DATABASE TestDB;

(2)查询以返回服务器上所有数据库的名称:

SELECT Name from sys.databases;

(3)前两个命令没有立即执行。 必须在新行中键入 GO 才能执行以前的命令:

GO

输出:

Name                                                                                                                            --------------------------------------------------------------------------------------------------------------------------------master                                                                                                                          tempdb                                                                                                                          model                                                                                                                           msdb                                                                                                                            testDB                                                                                                                          (5 rows affected)

5.2、插入数据

(1)将上下文切换到新的 testDB 数据库:

USE testDB;

(2)创建名为 dbo.Inventory 的新表:

CREATE TABLE dbo.Inventory (   id INT, name NVARCHAR(50),   quantity INT);

(3)将数据插入新表:

INSERT INTO dbo.Inventory VALUES (1, 'banana', 150);INSERT INTO dbo.Inventory VALUES (2, 'orange', 154);

(4)要执行上述命令的类型 GO:

GO

5.3、查询数据

(1)返回 dbo.Inventory 表中数量大于 152 的行:

SELECT * FROM dbo.Inventory WHERE quantity > 152;

(2)执行go命令:

GO

输出:

id          name                                               quantity   ----------- -------------------------------------------------- -----------          2 orange                                                     154(1 rows affected)

5.4、退出 sqlcmd 命令提示符

要结束 sqlcmd 会话,请键入 QUIT:

QUIT

注意,没有分号。

总结

如果有一台可以连接到 Linux 计算机的 Windows 计算机,请从运行 sqlcmd 的 Windows 命令提示符尝试执行本主题中的相同步骤。 必须使用目标 Linux 计算机名称或 IP 地址,而不是 localhost,并确保 TCP 端口 1433 已在 SQL Server 计算机上打开。 如果从 Windows 进行连接存在任何问题,请参阅连接故障排除建议。
在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/60383.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1