如何在 Ubuntu 16.04 上安装和配置 MS SQL(Beta)
在这篇文章中,我们将学习如何在 Ubuntu 上安装和配置 MS SQL。微软最近宣布他们计划发布适用于 Linux 的 MS SQL。特别是 Red Hat Enterprises Linux、Centos 和 Ubuntu 的 Beta 版本,但仅限于 64 位版本。
先决条件
- 一台至少具有 4 GB RAM 和 30 GB 硬盘空间的机器。
- 一台安装了 Ubuntu 16.04 的机器。
- 具有 root 权限的用户或 root 用户。
在 Ubuntu 上安装 MS SQL Server
由于 MS SQL 在默认的 Ubuntu 存储库中不可用,因此我们需要将 MS SQL 存储库详细信息添加到本地机器存储库中。
我们需要将 GPG 密钥添加到本地机器。
$ sudo curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add – % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 983 100 983 0 0 253 0 0:00:03 0:00:03 --:--:-- 253 OK
然后,我们将 Microsoft 存储库添加到本地 Ubuntu 机器。
$ sudo curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server.list | sudo tee /etc/apt/sources.list.d/mssql-server.list % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 87 100 87 0 0 95 0 --:--:-- --:--:-- --:--:-- 95 deb [arch=amd64] https://packages.microsoft.com/ubuntu/16.04/mssql-server xenial main
我们需要运行更新以将存储库获取到本地机器。
$ sudo apt-get update
存储库更新后,我们将运行以下命令来安装 MS-SQL 服务器。
$ sudo apt-get install -y mssql-server Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: gcc-5-base gdb gdbserver libbabeltrace-ctf1 libbabeltrace1 libc++1 libc6 libc6-dbg libcc1-0 libcurl3 libjemalloc1 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libstdc++6 libunwind8 python python-minimal python2.7 python2.7-minimal Suggested packages: .. .. Need to get 158 MB of archives. After this operation, 734 MB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1ubuntu0~16.04.1 [339 kB] Get:2 https://packages.microsoft.com/ubuntu/16.04/mssql-server xenial/main amd64 mssql-server amd64 14.0.1.246-6 [144 MB] .. .. .. 14 not fully installed or removed. Need to get 0 B/144 MB of archives. After this operation, 681 MB of additional disk space will be used. Preconfiguring packages ... (Reading database ... 60605 files and directories currently installed.) Preparing to unpack .../mssql-server_14.0.1.246-6_amd64.deb ... Unpacking mssql-server (14.0.1.246-6) ... Processing triggers for libc-bin (2.23-0ubuntu3) ... … .. Setting up libc++1:amd64 (3.7.0-1) ... Setting up libc6-dbg:amd64 (2.23-0ubuntu4) ... Setting up libjemalloc1 (3.6.0-9ubuntu1) ... Setting up mssql-server (14.0.1.246-6) ... +-------------------------------------------------------------------+ | Please run /opt/mssql/bin/sqlservr-setup to complete the setup of | | Microsoft(R) SQL Server(R). | +-------------------------------------------------------------------+ Processing triggers for libc-bin (2.23-0ubuntu3) ...
配置 MS SQL SA 帐户
安装完成后,我们将运行 sqlservr-setup 脚本以设置 SA 的密码;我们需要为 SA 帐户提供一个强密码(至少 8 个字符,包含大小写字母、1 到 10 之间的数字或任何非字母数字字符)。
# sudo /opt/mssql/bin/sqlservr-setup Microsoft(R) SQL Server(R) Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from http://go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator (SA) account: Please confirm the password for the system administrator (SA) account: The passwords specified do not match. Please try again. Please enter a password for the system administrator (SA) account: Please confirm the password for the system administrator (SA) account: Setting system administrator (SA) account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /lib/systemd/system/mssql-server-telemetry.service.
设置成功完成
配置完成后,我们将重新启动 MS SQL 服务,并使用以下命令查看服务的运行状态。
重启服务
# systemctl start mssql-server
查看服务的运行状态
# systemctl status mssql-server mssql-server.service - Microsoft(R) SQL Server(R) Database Engine Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2016-12-06 23:13:01 IST; 52s ago Main PID: 6173 (sqlservr) CGroup: /system.slice/mssql-server.service ├─6173 /opt/mssql/bin/sqlservr └─6190 /opt/mssql/bin/sqlservr Dec 06 23:13:28 ubuntu16 sqlservr[6173]: [45B blob data] .. .. Dec 06 23:13:28 ubuntu16 sqlservr[6173]: [124B blob data] Dec 06 23:13:36 ubuntu16 systemd[1]: Started Microsoft(R) SQL Server(R) Database Engine.
安装 MS SQL 客户端工具
安装并更新 GPG 密钥,然后我们将更新本地机器的存储库。
# curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 983 100 983 0 0 282 0 0:00:03 0:00:03 --:--:-- 282 OK
GPG 密钥更新后,我们将使用 Microsoft 存储库更新本地存储库。
$ sudo curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0 100 79 100 79 0 0 67 0 0:00:01 0:00:01 --:--:-- 67 deb [arch=amd64] https://packages.microsoft.com/ubuntu/16.04/prod xenial main$
存储库安装到本地机器后,我们可以使用以下命令安装 MS SQL 工具。
# sudo apt-get install mssql-tools Reading package lists... Done Building dependency tree Reading state information... Done .. .. Selecting previously unselected package mssql-tools. (Reading database ... 66292 files and directories currently installed.) Preparing to unpack .../mssql-tools_14.0.1.246-1_amd64.deb ... Unpacking mssql-tools (14.0.1.246-1) ... Setting up mssql-tools (14.0.1.246-1) ...
连接到 Linux 上的 SQL Server
工具安装到机器上后,我们将进行测试,以下是连接到 MS SQL 服务器的命令和通用语法。
语法
# sqlcmd –S IP-ADDRESS –U SA –P ‘<YOUR SYSTEM PASSWORD’ # sqlcmd sqlcmd -S localhost -U SA -P Test@1234 1>CREATE DATABASE TEST1; 2>GO; 3>quit
例如,我们将执行一些查询,这些查询返回所有数据库的名称。
SELECT Name from sys.Databases; GO
使用 SQL Server 默认设置创建数据库。
CREATE DATABASE inventdb; GO
在本文中,我们学习了如何在 Ubuntu 16.04 上安装 MS SQL Server、使用 MS SQL 命令行工具连接到服务器,以及如何更改或设置 MS SQL 的 SA 帐户密码、安装客户端工具和执行查询。