示例:SUSE Linux Enterprise Server 的无人参与 SQL Server 安装脚本Sample: Unattended SQL Server installation script for SUSE Linux Enterprise Server

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) - Linux适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) - Linux

此示例 Bash 脚本在没有交互式输入的 SUSE Linux Enterprise Server(SLES) v12 SP2上安装 SQL Server 2017。This sample Bash script installs SQL Server 2017 on SUSE Linux Enterprise Server (SLES) v12 SP2 without interactive input. 它提供数据库引擎、SQL Server 命令行工具和 SQL Server 代理的安装示例,并执行安装后步骤。It provides examples of installing the database engine, the SQL Server command-line tools, SQL Server Agent, and performs post-install steps. 可以选择安装全文搜索并创建管理用户。You can optionally install full-text search and create an administrative user.

提示

如果不需要无人参与的安装脚本,则安装 SQL Server 最快的方法是遵循 SLES 的快速入门If you do not need an unattended installation script, the fastest way to install SQL Server is to follow the quickstart for SLES. 有关其他设置信息,请参阅 Linux 上的 SQL Server 的安装指南For other setup information, see Installation guidance for SQL Server on Linux.

先决条件Prerequisites

  • 至少需要 2GB 内存才能运行 Linux 上的 SQL Server。You need at least 2 GB of memory to run SQL Server on Linux.
  • 文件系统必须是 XFS 或 EXT4 。The file system must be XFS or EXT4. 其他文件系统(如 BTRFS)均不受支持 。Other file systems, such as BTRFS, are unsupported.
  • 有关其他系统要求,请参阅 Linux 上的 SQL Server 的系统要求For other system requirements, see System requirements for SQL Server on Linux.

重要

SQL Server 2017 需要 libsss_nss_idmap0,默认的 SLES 存储库未提供此资源。SQL Server 2017 requires libsss_nss_idmap0, which is not provided by the default SLES repositories. 可以从 SLES v12 SP2 SDK 安装它。You can install it from the SLES v12 SP2 SDK.

示例脚本Sample script

#!/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'

# Install SQL Server Agent (recommended)
SQL_INSTALL_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...
sudo zypper addrepo -fc https://packages.microsoft.com/config/sles/12/mssql-server-2017.repo
sudo zypper addrepo -fc https://packages.microsoft.com/config/sles/12/prod.repo 
sudo zypper --gpg-auto-import-keys refresh

#Add the SLES v12 SP2 SDK to obtain libsss_nss_idmap0
sudo SUSEConnect -p sle-sdk/12.2/x86_64

echo Installing SQL Server...
sudo zypper 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 zypper install -y mssql-tools unixODBC-devel

# 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 SQL Server Agent installation:
if [ ! -z $SQL_INSTALL_AGENT ]
then
  echo Installing SQL Server Agent...
  sudo zypper install -y mssql-server-agent
fi

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

# Configure firewall to allow TCP port 1433:
echo Configuring SuSEfirewall2 to allow traffic on port 1433...
sudo SuSEfirewall2 open INT TCP 1433
sudo SuSEfirewall2 stop
sudo SuSEfirewall2 start

# Example of setting post-installation configuration options
# Set trace flags 1204 and 1222 for deadlock tracing:
# echo Setting trace flags...
# sudo /opt/mssql/bin/mssql-conf traceflag 1204 1222 on

# Restart SQL Server after making configuration changes:
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 5s
  /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!

运行脚本Running the script

运行该脚本To run the script

  1. 将示例粘贴到你最喜欢的文本编辑器中,并使用便于记忆的名称保存它,例如 install_sql.shPaste the sample into your favorite text editor and save it with a memorable name, like install_sql.sh.

  2. 自定义 MSSQL_SA_PASSWORDMSSQL_PID 和要更改的任何其他变量。Customize MSSQL_SA_PASSWORD, MSSQL_PID, and any of the other variables you'd like to change.

  3. 将脚本标记为可执行文件Mark the script as executable

    chmod +x install_sql.sh
    
  4. 运行脚本Run the script

    ./install_sql.sh
    

了解脚本Understanding the script

Bash 脚本执行的第一件事是设置几个变量。The first thing the Bash script does is set a few variables. 它们可以是脚本变量(例如本示例),也可以是环境变量。These can be either scripting variables, like the sample, or environment variables. MSSQL_SA_PASSWORD 变量是安装 SQL Server 所必需的变量,其余变量是为脚本创建的自定义变量 。The variable MSSQL_SA_PASSWORD is required by SQL Server installation, the others are custom variables created for the script. 示例脚本执行以下步骤:The sample script performs the following steps:

  1. 导入公共 Microsoft GPG 密钥。Import the public Microsoft GPG keys.

  2. 注册用于 SQL Server 的 Microsoft 存储库和命令行工具。Register the Microsoft repositories for SQL Server and the command-line tools.

  3. 更新本地存储库Update the local repositories

  4. 安装 SQL ServerInstall SQL Server

  5. 将 SQL Server 配置为 MSSQL_SA_PASSWORD 并自动接受最终用户许可协议。Configure SQL Server with the MSSQL_SA_PASSWORD and automatically accept the End-User License Agreement.

  6. 自动接受 SQL Server 命令行工具的最终用户许可协议,安装这些工具,然后安装 unixodbc-dev 包。Automatically accept the End-User License Agreement for the SQL Server command-line tools, install them, and install the unixodbc-dev package.

  7. 为便于使用,请将 SQL Server 命令行工具添加到路径。Add the SQL Server command-line tools to the path for ease of use.

  8. 如果已设置 SQL_INSTALL_AGENT 变量的脚本,则将默认安装 SQL Server 代理。Install the SQL Server Agent if the scripting variable SQL_INSTALL_AGENT is set, on by default.

  9. 如果已设置 SQL_INSTALL_FULLTEXT 变量,则可以选择安装 SQL Server 全文搜索。Optionally install SQL Server Full-Text search, if the variable SQL_INSTALL_FULLTEXT is set.

  10. 在系统防火墙上取消阻止 TCP 端口 1433,这是从另一个系统连接到 SQL Server 所必需的。Unblock port 1433 for TCP on the system firewall, necessary to connect to SQL Server from another system.

  11. (可选)设置跟踪标志以进行死锁跟踪。Optionally set trace flags for deadlock tracing. (需要取消注释行)(requires uncommenting the lines)

  12. SQL Server 现已安装,若要使其可操作,请重启该过程。SQL Server is now installed, to make it operational, restart the process.

  13. 验证是否已正确安装 SQL Server,并隐藏所有错误消息。Verify that SQL Server is installed correctly, while hiding any error messages.

  14. 如果同时设置 SQL_INSTALL_USERSQL_INSTALL_USER_PASSWORD,则将创建新的服务器管理用户。Create a new server administrator user if SQL_INSTALL_USER and SQL_INSTALL_USER_PASSWORD are both set.

后续步骤Next steps

简化多个无人参与安装,并创建独立的 Bash 脚本来设置适当的环境变量。Simplify multiple unattended installs and create a stand-alone Bash script that sets the proper environment variables. 可删除该示例脚本使用的任何变量,并将其放入自己的 Bash 脚本。You can remove any of the variables the sample script uses and put them in their own Bash script.

#!/bin/bash
export MSSQL_SA_PASSWORD='<YourStrong!Passw0rd>'
export MSSQL_PID='evaluation'
export SQL_INSTALL_AGENT='y'
export SQL_INSTALL_USER='<Username>'
export SQL_INSTALL_USER_PASSWORD='<YourStrong!Passw0rd>'
export SQL_INSTALL_AGENT='y'

然后运行 Bash 脚本,如下所示:Then run the Bash script as follows:

. ./my_script_name.sh

有关 Linux 上的 SQL Server 的详细信息,请参阅 Linux 上的 SQL Server 概述For more information about SQL Server on Linux, see SQL Server on Linux overview.