美文网首页
在Ubuntu 20.04上部署SQLServer单实例

在Ubuntu 20.04上部署SQLServer单实例

作者: 这货不是王马勺 | 来源:发表于2024-01-09 11:12 被阅读0次

安装

先决条件:
至少需要 2GB 内存才能运行 Linux 上的 SQL Server。
文件系统必须是 XFS 或 EXT4 。 其他文件系统(如 BTRFS)均不受支持 。

修改主机名

hostnamectl set-hostname test1

重启后生效

要在 Ubuntu 上配置 SQL Server,请在终端中运行以下命令以安装 mssql-server 包 。
导入公共存储库 GPG 密钥:

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

注册 SQL Server Ubuntu 存储库:

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

运行以下命令以安装 SQL Server:

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

包安装完成后,运行 mssql-conf setup,按照提示设置 SA 密码并选择版本。

sudo /opt/mssql/bin/mssql-conf setup

请记住为 SA 帐户指定强密码。 需要最小长度为 8 个字符,包括大写和小写字母、十进制数字和/或非字母数字符号。
完成配置后,验证服务是否正在运行:

systemctl status mssql-server --no-pager

如果计划远程连接,可能还需要在防火墙上打开 SQL Server TCP 端口(默认值为 1433)。

若要创建数据库,则需要使用可在 SQL Server 上运行 Transact-SQL 语句的工具进行连接。 按照以下步骤安装 SQL Server 命令行工具:sqlcmd 实用工具bcp 实用工具

参考:

https://learn.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-ubuntu?view=sql-server-ver15&tabs=ubuntu2004

自动安装脚本

此示例 bash 脚本可在 Ubuntu 上安装 SQL Server,且无需交互式输入。 它提供数据库引擎、SQL Server 命令行工具和 SQL Server 代理的安装示例,并执行安装后步骤。 可以选择安装全文搜索并创建管理用户。

此示例在 Ubuntu Server 20.04 上安装 SQL Server 2019 (15.x)。 如果要安装不同版本的 SQL Server 或 Ubuntu Server,请相应地更改 Microsoft 存储库路径。

将示例脚本保存到文件,然后对其进行自定义。 需要替换脚本中的变量值。 还可以将任何脚本变量设置为环境变量,前提是从脚本文件中删除它们。

如果 SQL Server 启动缓慢,则脚本可能会失败。 这是因为脚本将以非零状态退出。 删除第一行上的 -e 开关可以解决此问题。

注:SA_PASSWORD 环境变量已弃用。 请改用 MSSQL_SA_PASSWORD。

#!/bin/bash -e

# Use the following variables to control your install:

# Password for the SA user (required)
MSSQL_SA_PASSWORD='<YourStrong!Passw0rd>'

# Product ID of the version of SQL Server you're installing
# Must be evaluation, developer, express, web, standard, enterprise, or your 25 digit product key
# Defaults to developer
MSSQL_PID='evaluation'

# Enable SQL Server Agent (recommended)
SQL_ENABLE_AGENT='y'

# Install SQL Server Full Text Search (optional)
# SQL_INSTALL_FULLTEXT='y'

# Create an additional user with sysadmin privileges (optional)
# SQL_INSTALL_USER='<Username>'
# SQL_INSTALL_USER_PASSWORD='<YourStrong!Passw0rd>'

if [ -z $MSSQL_SA_PASSWORD ]
then
  echo Environment variable MSSQL_SA_PASSWORD must be set for unattended install
  exit 1
fi

echo Adding Microsoft repositories...
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
repoargs="$(curl https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"
sudo add-apt-repository "${repoargs}"
repoargs="$(curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list)"
sudo add-apt-repository "${repoargs}"

echo Running apt-get update -y...
sudo apt-get update -y

echo Installing SQL Server...
sudo apt-get install -y mssql-server

echo Running mssql-conf setup...
sudo MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD \
     MSSQL_PID=$MSSQL_PID \
     /opt/mssql/bin/mssql-conf -n setup accept-eula

echo Installing mssql-tools and unixODBC developer...
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools unixodbc-dev

# Add SQL Server tools to the path by default:
echo Adding SQL Server tools to your path...
echo PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

# Optional Enable SQL Server Agent:
if [ ! -z $SQL_ENABLE_AGENT ]
then
  echo Enabling SQL Server Agent...
  sudo /opt/mssql/bin/mssql-conf set sqlagent.enabled true
fi

# Optional SQL Server Full Text Search installation:
if [ ! -z $SQL_INSTALL_FULLTEXT ]
then
    echo Installing SQL Server Full-Text Search...
    sudo apt-get install -y mssql-server-fts
fi

# Configure firewall to allow TCP port 1433:
echo Configuring UFW to allow traffic on port 1433...
sudo ufw allow 1433/tcp
sudo ufw reload

# Optional example of post-installation configuration.
# Trace flags 1204 and 1222 are for deadlock tracing.
# echo Setting trace flags...
# sudo /opt/mssql/bin/mssql-conf traceflag 1204 1222 on

# Restart SQL Server after installing:
echo Restarting SQL Server...
sudo systemctl restart mssql-server

# Connect to server and get the version:
counter=1
errstatus=1
while [ $counter -le 5 ] && [ $errstatus = 1 ]
do
  echo Waiting for SQL Server to start...
  sleep 3s
  /opt/mssql-tools/bin/sqlcmd \
    -S localhost \
    -U SA \
    -P $MSSQL_SA_PASSWORD \
    -Q "SELECT @@VERSION" 2>/dev/null
  errstatus=$?
  ((counter++))
done

# Display error if connection failed:
if [ $errstatus = 1 ]
then
  echo Cannot connect to SQL Server, installation aborted
  exit $errstatus
fi

# Optional new user creation:
if [ ! -z $SQL_INSTALL_USER ] && [ ! -z $SQL_INSTALL_USER_PASSWORD ]
then
  echo Creating user $SQL_INSTALL_USER
  /opt/mssql-tools/bin/sqlcmd \
    -S localhost \
    -U SA \
    -P $MSSQL_SA_PASSWORD \
    -Q "CREATE LOGIN [$SQL_INSTALL_USER] WITH PASSWORD=N'$SQL_INSTALL_USER_PASSWORD', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON; ALTER SERVER ROLE [sysadmin] ADD MEMBER [$SQL_INSTALL_USER]"
fi

echo Done!

若要运行该脚本,请执行以下操作:
1.将示例粘贴到你最喜欢的文本编辑器中,并使用便于记忆的名称保存它,例如 install_sql.sh。
2.自定义 MSSQL_SA_PASSWORD、MSSQL_PID 和要更改的任何其他变量。
3.将脚本标记为可执行文件

chmod +x install_sql.sh

4.运行脚本

./install_sql.sh

参考:

https://learn.microsoft.com/zh-cn/sql/linux/sample-unattended-install-ubuntu?view=sql-server-ver15

脱机安装

如果 Linux 计算机无法访问快速入门中使用的联机存储库,则可以直接下载包文件。 这些包位于 Microsoft 存储库中,网址:

https://packages.microsoft.com
  • 下载适用于平台的数据库引擎包。 在发行说明的包详细信息部分找到包下载链接。
  • 将下载的包移动到 Linux 计算机。 如果使用了不同的计算机下载包,则可以通过“scp”命令将包移至你的 Linux 计算机。
    -安装数据库引擎包。 根据你的平台使用以下命令之一。 将此示例中的包文件名替换为下载的确切名称。
sudo dpkg -i mssql-server_versionnumber_amd64.deb
  • 解决缺少依赖项的问题:此时可能会出现缺少依赖项的情况。 如果没有,可以跳过此步骤。 在 Ubuntu 上,如果能够访问包含这些依赖项的已批准的存储库,最简单的解决办法是使用 apt-get -f install 命令。 此命令还会完成 SQL Server 的安装。 若要手动检查依赖项,请使用以下命令:
dpkg -I mssql-server_versionnumber_amd64.deb

解决缺少的依赖项后,可以尝试再次安装 mssql-server 包。
我这里安装的是2019,依赖如下:
libatomic1, libunwind8, libnuma1, libc6, adduser, libc++1, gdb, debconf, hostname, openssl (>= 1.0.1g), python3, libgssapi-krb5-2, libsss-nss-idmap0, gawk, sed, libpam0g, libldap-2.4-2, libsasl2-2, libsasl2-modules-gssapi-mit, tzdata, lsof, procps

使用apt-get install安装:

sudo apt-get install libatomic1 libunwind8 libnuma1  libc6 adduser libc++1 gdb debconf hostname  openssl   python3  libgssapi-krb5-2  libsss-nss-idmap0  gawk  sed  libpam0g  libldap-2.4-2  libsasl2-2  libsasl2-modules-gssapi-mit  tzdata  lsof  procps

安装完依赖后,再执行安装,提示如下:

root@admin123:/app/database# sudo dpkg -i mssql-server_15.0.4345.5-2_amd64.deb
Selecting previously unselected package mssql-server.
(Reading database ... 113557 files and directories currently installed.)
Preparing to unpack mssql-server_15.0.4345.5-2_amd64.deb ...
Unpacking mssql-server (15.0.4345.5-2) ...
Setting up mssql-server (15.0.4345.5-2) ...

+--------------------------------------------------------------+
Please run 'sudo /opt/mssql/bin/mssql-conf setup'
to complete the setup of Microsoft SQL Server
+--------------------------------------------------------------+

Processing triggers for libc-bin (2.31-0ubuntu9.14) ...
Processing triggers for man-db (2.9.1-1) ...
  • 完成 SQL Server 安装。 使用“mssql-conf”完成 SQL Server 安装:
sudo /opt/mssql/bin/mssql-conf setup

步骤如下:

root@admin123:/app/database# 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) - 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=2109348&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): 6
The 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=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...

The licensing PID was successfully processed. The new edition is [Enterprise 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.

查看运行状态:

systemctl status mssql-server.service

参考:

https://learn.microsoft.com/zh-cn/sql/linux/sql-server-linux-setup?view=sql-server-ver15#offline

下载地址:

https://learn.microsoft.com/zh-cn/sql/linux/sql-server-linux-release-notes-2019?view=sql-server-ver15

SQLServer on Linux
参考:

https://learn.microsoft.com/zh-cn/sql/linux/sql-server-linux-overview?view=sql-server-ver15

相关文章

网友评论

      本文标题:在Ubuntu 20.04上部署SQLServer单实例

      本文链接:https://www.haomeiwen.com/subject/qlmpndtx.html