Chapter 5

Install and configure SQL Server on Linux

Since SQL Server 2017, you can install SQL Server on multiple platforms: Windows, Linux, and in containers. Chapter 4, “Install and configure SQL Server instances and features,” discussed installing SQL Server on containers and in a Kubernetes environment, as well as performing setup and configuration on Windows Server.

This chapter covers Linux distributions, the basic differences between Windows and Linux, how to install SQL Server on the supported distributions of Linux, and the main differences in SQL Server on Windows and Linux. It is a shorter chapter because the differences are minimal.

What is Linux?

Linux, sometimes referred to as GNU/Linux, is an operating system (OS) including a kernel, system libraries, and a package manager. A Linux distribution (or distro for short) is a software collection composed of the Linux kernel, system libraries and tools, and numerous software packages (the equivalent of installer files on Windows), maintained by commercial and noncommercial organizations from all over the world.

There are several hundred distributions in active development worldwide, with many of the same software packages available in each one, so it can be confusing trying to pick a distribution. Microsoft supports SQL Server on three distributions, which makes your decision easier.

Like Windows Server Core, Linux distributions may offer a server-only configuration with a smaller package footprint and a command-line user interface. You can also install a desktop user interface through a window manager package, which is similar to the Windows desktop. To install SQL Server on Linux, you can use whichever server or desktop configuration you prefer. However, the desktop environment uses more system resources.

Differences between Windows and Linux

Windows is a proprietary OS created and maintained by Microsoft. Linux is an open-source OS created and maintained by a loose collection of volunteers, as well as various commercial and noncommercial organizations.

The main difference between Windows and Linux is philosophical. Because Linux is open source, if there is a feature you want to change or add, the end-user license lets you modify the source code yourself and recompile it. With a proprietary OS, the license does not permit modification, so you must submit a feature request and hope that enough people think it is a worthwhile addition.

On a technical level, Linux has a different directory structure, file system, and user interface from Windows. Applications written for one OS do not run natively on the other without some form of code modification or recompilation. For example, you cannot simply copy the sqlservr.exe file from Windows to Linux and run it. We cover this in more detail in the section “Caveats of SQL Server on Linux” later in this chapter.

Note

Code written in certain languages with their own runtime engines (for example .NET, Python, and Java) may run on both Windows and Linux if they access resources that are common to each OS.

Active Directory Domain Services authentication

Unlike with Windows, Active Directory Domain Services (AD DS) is not built into Linux and must be installed separately. SQL Server can then use Active Directory on both operating systems, which is recommended for centralized security and management. This allows you to extend your current Active Directory (AD) to access SQL Server instances on Linux as an extension of your Windows network.

Note

SQL Server 2022 introduces Azure Active Directory (Azure AD) authentication powered by Azure Arc, which is not the same as Active Directory Domain Services (AD DS). To use Azure Arc with SQL Server on Linux, you need the Azure extension for SQL Server on Linux, just as you would on Windows.

File systems and directory structures

Windows Server supports NTFS and ReFS. Several file systems are supported on Linux and might differ between distribution and edition, but will usually be ext4. SQL Server is supported on both ext4 and XFS, because these two file systems provide similar features to NTFS, including journaling, large partitions, and fine-grained access control.

Note

A journaling file system keeps track of changes that are not yet committed. It helps prevent corruption in the case of a disaster—for example, after a power failure or system crash.

On Windows, individual volumes are addressed by a letter followed by a colon. For example, the default volume for a Windows OS is C: and all files beneath the root directory (C:) are addressable through that drive letter. Files are stored logically in folders, and these folders and subfolders (also called directories and subdirectories) are located beneath the root directory on each drive and separated by a backslash ().

On Linux, the directory structure is based off a root directory, called / (a single forward slash). While files are also stored in directories and subdirectories, everything, including individual drives, are addressed as subdirectories beneath the root directory separated by a slash (/).

Note

On Linux, the forward slash character (/) is just referred to as a slash. This differentiates it from a backslash ().

Package managers

Every Linux distribution comes with a package manager to install, manage, update, and delete packages using online repositories. For practical purposes, this is the major difference between distributions when installing software.

When a package manager installs an application (the package), it connects to a central repository controlled by the distribution maintainer. You can also register a third-party repository to install packages outside of that distribution, provided the repository provides packages in the appropriate format.

Package managers use information from repositories to ensure that packages are compatible with one another, which makes it much easier to keep your system stable and up to date. This built-in dependency resolution means that you install only what you need. If a package is dependent on one or more other packages that are not installed, the package manager will automatically install those dependencies.

Note

PowerShell runs on both Windows and Linux. You should familiarize yourself with PowerShell to give yourself an advantage managing SQL Server on both operating systems. You can learn more about this in Chapter 9, “Automate SQL Server administration.”

Run commands with elevated privileges

Many commands for installing, configuring, and administering Linux require administrative privileges, just like on Windows. The commands are preceded by the sudo keyword, which stands for superuser do. This is less risky than logging in as the superuser account (root). You will be prompted for the root password when you run sudo for the first time after a fixed period (usually 15 minutes).

Note

Some Linux distributions do not allow you to log in directly as the root account for added security. We recommend using the sudo command as a best practice.

Linux distributions supported by SQL Server

As noted, several hundred choices make it difficult to decide on which Linux distribution you should install. For SQL Server, however, the choice is a lot easier because it is supported only on the following three commercial distributions:

  • Red Hat Enterprise Linux (RHEL). Maintained by Red Hat, Inc., RHEL uses the RPM package manager through the command yum (which stands for Yellowdog Updater, Modified).

  • SUSE Linux Enterprise Server (SLES). Maintained by SUSE Group, SLES uses the ZYpp package manager through the command zypper (which stands for Zen/YaST Packages Patches Patterns Products).

  • Ubuntu Server (Ubuntu). Maintained by Canonical Ltd., Ubuntu uses the APT package manager through the command apt-get or aptitude (which stands for Advanced Package engine).

Note

Although Docker is included in the list of supported Linux distributions, this is not entirely accurate. Docker is just one of several OS-level virtualization engines on which you can deploy SQL Server containers. However, SQL Server containers are only supported for a production environment on Linux and Kubernetes. You can read more about Linux containers in Chapter 2, and about Kubernetes deployments in Chapter 4.

Having a vendor and support agreement behind an open-source implementation can be comforting and beneficial to organizations that have limited experience with a new technology stack. This support is especially valuable for SQL Server administrators starting out with Linux who are more familiar with Windows Server.

Considerations for installing SQL Server on Linux

As discussed in previous chapters, SQL Server has several considerations for CPU, RAM, and storage. In the vast majority of cases, you will apply the same principles as you would on Windows, with some minor caveats.

  • Image We cover CPU and RAM configuration in Chapters 2, 3, and 4. The next section adds Linux-specific configuration using mssql-conf.

Configure OS settings

Microsoft recommends the following OS settings for a dedicated SQL Server instance to run optimally on Linux.

Caution

Some configuration settings do not persist between reboots, so you will have to create an init.d script, which runs at boot time. Refer to your distribution’s documentation for details on how to configure the options that follow.

Configure high performance

Aside from the computer’s BIOS, where high performance should be enabled (in other words, power saving should be disabled), you can also modify CPU settings at the Linux kernel level:

  • Set the CPU frequency governor to 100% using the cpupower command.

  • Use the performance option with the x86_energy_perf_policy command.

  • Set the min_perf_pct setting to 100%.

  • Set C-States to C1 only.

These CPU settings are functionally equivalent to enabling High Performance on Windows.

Configure NUMA nodes

For computers with more than one NUMA node, use the sysctl command to disable auto-NUMA balancing, setting kernel.numa_balancing to 0. You do this because SQL Server handles NUMA internally.

Configure virtual address space

Chapter 2 discussed the working set, or memory provided by the OS for use by a process—in this case, SQL Server. The working set resides in a virtual address space, which in turn is mapped to physical memory by internal OS structures. The default setting for the number of memory map areas in virtual memory might not be sufficient for SQL Server on Linux, so you should use the sysctl command to change vm.max_map_count to the upper limit of 262144 (262,144).

Configure Transparent Hugepages

Certain Linux distributions, including Red Hat, provide improved performance on systems by increasing the size of memory blocks transparently to the underlying process. This is beneficial for applications with contiguous memory access patterns and works to SQL Server’s advantage. Transparent Hugepages (THP) is already enabled on Linux by default, so you should leave this on for a dedicated SQL Server instance.

Set up the file system

As mentioned in Chapter 3, “Design and implement an on-premises database infrastructure,” SQL Server on Linux requires either the ext4 or XFS file system. If the file system supports it, use a 64-KB block size to match the size of an extent. Otherwise, use the largest size that it supports. (For example, ext4 may only support a block size of 4 KB.) With newer storage subsystems and SANs, this block size is less important than it used to be.

By default, SQL Server places the data and log files in /var/opt/mssql/data. Notice that this path starts with a single forward slash, which is the root directory on a Linux system. This path then includes var, which is a default directory created for the OS to write data during normal operation. (var stands for variable.) Then, opt (which stands for optional) is for optional packages that are not included in a default installation of Linux. The next directory is mssql, which stands for Microsoft SQL.

Note

You can use symbolic or hard links to redirect the default data path to a different physical location (for instance, another drive), but we recommend using the mssql-conf tool to change the location instead, like you would on Windows.

A good practice is to keep transaction log files and tempdb on your fastest available storage. Additionally, you should mount volumes using the noatime attribute, which prevents tracking the last accessed time for that volume, thereby improving performance. This is managed in the file system table configuration file known as fstab. Refer to the fstab documentation for more information.

Recommended disk settings

For optimal settings at the physical disk level, you can set the disk read-ahead to 4096 bytes using the blockdev command. Additionally, there are several settings you can configure using the sysctl command:

  • Set kernel.sched_min_granularity_ns to 10000000 (10,000,000).

  • Set kernel.sched_wakeup_granularity_ns to 15000000 (15,000,000).

  • Set vm.dirty_ratio to 40.

  • Set vm.dirty_background_ratio to 10.

  • Set vm.swappiness to 10.

Caution

Some sysctl settings may be overridden by modules that load later in the boot process. You can read more about this at https://linux.die.net/man/8/sysctl.

Install SQL Server on Linux

To integrate better with Linux, SQL Server leverages the package manager concept, meaning you only install the components you need, starting with the Database Engine package. This can drastically reduce the amount of time it takes to install SQL Server compared to Windows. For instance, on an Internet-connected machine with a high-speed connection, you can download, install, and configure SQL Server 2022 in under 5 minutes.

Table 5-1 shows which packages are available for SQL Server on Linux. The command-line tools include sqlcmd and bcp, which work the same as their Windows counterparts.

Table 5-1 SQL Server packages for Linux

Component

Package name

SQL Server Database Engine

mssql-server

Full-Text Search

mssql-server-fts

SQL Server Integration Services

mssql-server-is

PolyBase

mssql-server-polybase

SQL Server Agent (included in Database Engine)

mssql-server-agent

SQL Server command-line tools

mssql-tools

SQL Server 2022 Language Extensions

mssql-server-extensibility

Note

SQL Server Agent is installed with the Database Engine but is not enabled by default. To enable SQL Server Agent, use mssql-conf and set sqlagent.enabled to true. You can read more about mssql-conf in the section “Use mssql-conf to set up and configure SQL Server” later in this chapter.

Installation requirements

The minimum system requirements for SQL Server on Linux are as follows:

  • CPU. 2 GHz (x64-compatible), with two physical cores

  • Memory. 3.25 GB RAM

  • Storage. 6 GB (formatted with either ext4 or XFS)

Note

While SQL Server Express edition is artificially limited to 1,410 MB for the buffer pool, it can use additional memory for columnstore and memory-optimized objects. You can read more about edition scale limits at https://learn.microsoft.com/sql/linux/sql-server-linux-editions-and-components-2022.

Download and install packages

You can install SQL Server on a computer that is connected to the Internet or on an offline computer. In the latter case you must download the packages you need and copy them to the offline machine.

Download the third-party repository configuration file

You must download SQL Server packages directly from Microsoft because they are not available from the official distribution repositories. You must add the Microsoft package repository to the list of approved repositories on your computer, depending on which version of the Linux distribution you have installed.

Note

When you add a third-party repository, you must first import that repository’s public keys to ensure that any files you are downloading are verified. Read more about public key encryption in Chapter 13, “Protect data through classification, encryption, and auditing.”

The following sample bash command loads the package repository configuration for SQL Server 2022 for RHEL 8.x into the repository on your machine. In this example, the trailing space and backslash at the end of the first line are a bash convention to indicate that the command spans more than one line:

sudo curl -o /etc/yum.repos.d/mssql-server.repo 
https://packages.microsoft.com/config/rhel/8/mssql-server-2022.repo
Download the package

Once the package manager is configured to accept packages from Microsoft and the list of available packages is refreshed, you can install SQL Server immediately or download the packages for offline installation on another computer. For RHEL, the following command will download the package locally (as well as any dependencies):

sudo yum localinstall mssql-server_2022.x86_64.rpm
Install the package

Depending on the distribution and package manager, you will install the SQL Server Database Engine package using one of the following methods:

  • RHEL. sudo yum install -y mssql-server

  • SLES. sudo zypper install -y mssql-server

  • Ubuntu. sudo apt-get install -y mssql-server

Each install command has a -y switch, which forces any prompts to agree to the question in the affirmative. This is useful for agreeing to install dependencies without prompting for each one—for example, in unattended installs (see the next section). If you want to confirm every prompt manually, you can remove the -y switch.

Perform an unattended installation

You can also install SQL Server on Linux using a shell script, which is recommended for production deployments and deployments across multiple servers, to ensure a consistent experience. The script should include all the steps you need to register the Microsoft package repository, download the requisite packages, and perform the post-installation configuration.

See the “Configure SQL Server on Linux” section later in this chapter for more information about post-installation steps. You can also get a sample bash script from Microsoft Docs, depending on the distribution you have chosen, from the following locations:

Update packages

With the Microsoft SQL Server package repository as an approved third-party option, the distribution’s package manager will update SQL Server components (including cumulative updates) at the same time as other OS updates. This is functionally equivalent to the Windows Update feature titled “Receive updates for other Microsoft products.”

To update the packages on your Linux OS, including any updates of SQL Server components you have installed, use the following commands:

  • RHEL. sudo yum update

  • SLES. sudo zypper update

  • Ubuntu. sudo apt-get update

Note

On Red Hat Enterprise Linux, you must use subscription-manager to register your computer with the Red Hat Network (RHN) before updates will work.

Configure SQL Server on Linux

After you install the SQL Server Database Engine package, you need to configure SQL Server for the first time. This differs from the way SQL Server on Windows works (unless you perform an unattended install), because you can follow SQL Server Setup and configure the settings as you go, whereas on Linux, you configure SQL Server after it is installed.

  • Image You can read more about TCP ports in Chapter 13.

Use mssql-conf to set up and configure SQL Server

Windows uses the SQL Server Configuration Manager to configure SQL Server at the OS level. Linux does not have its own registry, so SQL Server’s configuration is stored in a plain text file and accessed when the SQL Server service starts up.

You will initially interact with SQL Server through the CLI—most likely the bash shell. To configure SQL Server after installing the Database Engine package, you must run mssql-conf from the command line.

Note

The mssql-conf tool is written in Python, as are several other command-line tools for working with SQL Server. When you install SQL Server on Linux, you may notice that one of the dependencies installed is the Python package.

Configuration settings

The executable package for mssql-conf is in the /opt/mssql/bin path. You can see that /opt is a directory off the root directory where optional packages are stored, and mssql stands for Microsoft SQL. The bin directory stands for binaries, which are functionally the same as executable files on Windows.

Note

To execute a binary file on Linux and other Unix-like operating systems, you must either provide the full path to the binary or, if you have navigated to the directory already, you must prefix the binary with ./ (a period and a slash).

mssql-conf uses a configuration file (called mssql.conf), which is a plain text file located at /var/opt/mssql/mssql.conf. Remember that /var is where files are written to, which is a convenient way to remember the difference between /opt and /var/opt.

The mssql-conf tool has two phases: first-run (initial) setup, and configuration, both of which we cover next.

Note

Once your SQL Server instance has been set up the first time, you can connect to it from any computer running SQL Server Management Studio, Azure Data Studio, sqlcmd, or mssql-conf—indeed, any tool that can connect to SQL Server.

First-run setup

From bash, run the following command to enter the configuration tool to configure SQL Server 2022 on Linux:

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

You are presented with a numbered list. If you make an error, you can quit the tool and run it again.

Choose the correct edition

SQL Server on Linux might not ask you to enter a license key. When you set up SQL Server for the first time, you are prompted for the edition you will be using. The following editions are available (this list is copied from the mssql-conf setup prompt):

  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.

Make your selection based on the edition you want to install and move on to the next prompt. You are shown the path to the license agreement, and then prompted to accept that agreement.

The Evaluation, Developer, and Express editions are free and do not require a paid license, but you will still be prompted to agree to the license terms. A good guideline for choosing a license is that if you plan to process production data under any circumstances (including if you want to test your database backups), you cannot use the Evaluation or Developer edition, and must purchase a paid license. Alternatively, you can use the artificially limited Express edition.

Choose the language

Now you are prompted to choose a default language for the SQL Server instance. You can choose from among 11 different options: English, German, Spanish, French, Italian, Japanese, Korean, Portuguese, Russian, Simplified Chinese, or Traditional Chinese.

Choose a SQL Server system administrator password

Your system administrator (SA) password should be a strong password. Microsoft’s guidance is to choose an alphanumeric password with a minimum length of eight uppercase and lowercase characters, including letters, digits, and symbols. If you plan to use AD authentication, you can disable this account later.

Note

Our password recommendation is similar, but you should increase the minimum length to 15 characters. You can generate and save the password using a password manager, so there is no need to pick a memorable password. You should not use the sa account directly unless it is an emergency or you are setting up the instance.

After you choose a password, you will be prompted to confirm it. Once that’s done, the SQL Server service will restart, taking these settings into account.

Configure the SQL Server instance

The configuration settings for SQL Server are managed using the same mssql-conf tool, replacing the SQL Server Configuration Manager on Windows.

There is a wide range of settings that you can use to customize your SQL Server instance. This is a brief overview of what is available (taken from https://learn.microsoft.com/sql/linux/sql-server-linux-configure-mssql-conf):

  • Agent. Enables SQL Server Agent. Although SQL Server Agent is installed along with the Database Engine, you still need to enable it.

  • Collation. Sets a new collation for SQL Server on Linux.

  • Customer Feedback. Specifies whether SQL Server sends feedback to Microsoft. This option cannot be disabled on free editions.

  • Database Mail Profile. Sets the default database mail profile for SQL Server on Linux.

  • Default Data Directory. Sets the default directory for new SQL Server database data files (.mdf). As noted in Chapter 3, we recommend moving this to a dedicated volume.

  • Default Log Directory. Changes the default directory for new SQL Server database log (.ldf) files. As noted in Chapter 3, we recommend moving this to a dedicated volume.

  • Default Master Database Directory. Changes the default directory for the master database and log files. As noted in Chapter 3, we recommend moving this to a dedicated volume.

  • Default Master Database File Name. Changes the name of master database files. We do not recommend changing this in the normal course of business, but it is useful in a disaster recovery scenario when restoring a master database.

  • Default Dump Directory. Changes the default directory for new memory dumps and other troubleshooting files. You can set the dump file type using the Dump Type option (see below).

  • Default Error Log Directory. Changes the default directory for new SQL Server ERRORLOG, Default Profiler Trace, System Health Session XE, and Hekaton (Memory-Optimized) Session XE files.

  • Default Backup Directory. Changes the default directory for new backup files. We recommend moving this to a dedicated volume separate from the data and log files to ensure business continuity should a drive failure occur.

  • Dump Type. Sets the type of memory dump file to collect in the event of a crash or exception in a SQL Server process. Each size setting (Mini, Miniplus, Filtered, and Full) provides a different level of detail in a memory dump for troubleshooting purposes. You can set the path for the dump directory using the Default Dump Directory option (see above).

  • High Availability. Enables availability groups.

  • Local Audit Directory. Sets a directory to add local audit files.

  • Locale. Sets the locale for SQL Server to use, in the form of a language code identifier (LCID).

  • Memory Limit. Sets the memory limit for SQL Server. Avoid going over the default of 80 percent of the maximum physical memory available on the server; Linux will terminate the SQL Server instance without warning if it detects high resource utilization.

  • TCP Port. Changes the port on which SQL Server listens for connections. Do so only if you have a specific business case. We do not recommend changing it for security reasons because a network sniffing tool will detect the new port almost instantly.

  • TLS. Configures Transport Layer Security (TLS). This is used to enforce encryption and to configure the path to the certificate and private key. You can also set TLS versions here, but Microsoft recommends only using TLS 1.2 (and higher when available). We cover TLS in more depth in Chapter 13. If you use Kerberos authentication to connect your SQL Server instance to AD, the Kerberos keytab file is also configured here.

  • Trace Flags. Sets the trace flags that the service is going to use. We recommend enabling at least Trace Flag 3226, which disables messages in the error log for successful database backups.

Caveats of SQL Server on Linux

SQL Server on Linux is implemented using a thin translation layer called the SQL Platform Abstraction Layer (SQLPAL), which maps Windows system calls to Linux system calls. This allows the exact same code for the Database Engine to be used on both operating systems. So, the Linux version does not have internal awareness that it is running on a different platform, and as far as SQL Server is concerned, it is running inside Windows.

This platform abstraction is both hugely powerful and limiting. It is powerful because now SQL Server runs on Windows, Linux, in containers, and on ARM64 devices with Azure SQL Edge. But it is limiting because only enough Windows system calls have been translated to the underlying OS to get these to work. Certain features are not available due to this limitation, including access to the Windows Registry. Only a very small stub of the Registry is included to support the required Windows system calls (including the Windows Data Protection API).

We covered the lack of a Registry to explain why mssql-conf replaces SQL Server Configuration Manager in the previous section, but several other features of SQL Server, which are dependent on Windows features, have not been implemented.

Missing SQL Server features on Linux

The list of features not available for SQL Server 2022 on Linux has shrunk since SQL Server 2017 was released. Microsoft has stated that new features will be enabled over time. If you need any of the following features, you must use SQL Server on Windows.

  • SQL Server Analysis Services (SSAS)

  • SQL Server Reporting Services (SSRS) (however, the SSRS databases can be hosted on a Linux instance)

  • Master Data Services (MDS)

  • Data Quality Services (DQS)

  • FILESTREAM and FileTable (requires NTFS or ReFS)

  • Extended stored procedures

  • Volume Shadow Copy Service (VSS) snapshots

  • Buffer Pool Extension

  • SQL Server Agent Alerts

A few other features are available, but with limitations:

  • Database Mirroring. This feature is in maintenance mode. We recommend that you use another high availability solution such as log shipping or availability groups. We cover availability groups on Linux and Windows in Chapter 11.

  • SQL Server Integration Services (SSIS). There is a component that lets you run SSIS packages, but the feature is quite limited otherwise. Another side effect is that the designer for Maintenance Plans in SQL Server Management Studio does not work.

  • SQL Server Browser Service. Linux does not allow more than one instance, so this is not a significant gap.

    Note

    Stretch Database was never supported on Linux, and with SQL Server 2022 it has been deprecated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.76.44