温馨提示×

Linux上如何安装SQL Server

小樊
34
2025-11-08 08:25:31
栏目: 云计算

Installing SQL Server on Linux: A Step-by-Step Guide
SQL Server can be installed on several Linux distributions, including Ubuntu, Red Hat Enterprise Linux (RHEL), and SUSE Linux Enterprise Server (SLES). Below is a distribution-specific guide to help you set up SQL Server, including prerequisites, repository configuration, installation, and post-installation steps.


1. Prerequisites

Before starting, ensure your system meets these requirements:

  • Supported Distributions: Ubuntu 16.04+/22.04+, RHEL 7/8/9, SLES 12 SP5+/15 SP6+.
  • Hardware: Minimum 2GB RAM, 6GB disk space, 2+ CPU cores.
  • Dependencies: Install curl, gnupg (Ubuntu/Debian) or zypper (SLES), and wget (if needed). For RHEL 8+, install python2 and compat-openssl10 (see Microsoft Learn).
  • Firewall: Open TCP port 1433 (SQL Server’s default port) to allow remote connections.

2. Ubuntu: Installation Steps

a. Add Microsoft’s GPG Key & Repository

Import Microsoft’s public key and register the SQL Server repository for your Ubuntu version (replace 20.04 with your Ubuntu version if different):

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

# Register repository (adjust version in URL as needed)
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2022.list)"

For newer Ubuntu versions (e.g., 22.04), use the appropriate repository URL from Microsoft’s documentation.

b. Install SQL Server

Update package lists and install mssql-server:

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

The installer will prompt you to set the SA password (must be at least 8 characters with 3 of: uppercase, lowercase, number, symbol).

c. Configure & Start SQL Server

Run the configuration tool to accept the license and set the SA password:

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

Verify the service is running:

sudo systemctl status mssql-server

Enable auto-start on boot:

sudo systemctl enable mssql-server

3. RHEL: Installation Steps

a. Add Microsoft’s GPG Key & Repository

Download the RHEL repository file and install it:

# For RHEL 9
sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/9/mssql-server-2022.repo

# For RHEL 8
sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo

Import the GPG key:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

b. Install SQL Server

Install the mssql-server package:

sudo yum install -y mssql-server  # RHEL 7/8
sudo dnf install -y mssql-server  # RHEL 9

Run the configuration tool to set the SA password:

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

Check the service status:

sudo systemctl status mssql-server

Enable auto-start:

sudo systemctl enable mssql-server

c. Open Firewall (if using FirewallD)

sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
sudo firewall-cmd --reload

4. SLES: Installation Steps

a. Add Microsoft’s GPG Key & Repository

Register the SQL Server repository for your SLES version (replace 15 with your SLES version if different):

# For SLES 15
sudo zypper addrepo -fc https://packages.microsoft.com/config/sles/15/mssql-server-2022.repo

Import the GPG key:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

b. Install SQL Server

Refresh the repository and install mssql-server:

sudo zypper refresh
sudo zypper install -y mssql-server

Configure the SA password:

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

Verify the service:

sudo systemctl status mssql-server

Enable auto-start:

sudo systemctl enable mssql-server

5. Post-Installation: Install Command-Line Tools

To manage SQL Server (e.g., run queries), install sqlcmd and bcp (command-line utilities):

Ubuntu/Debian

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

# Install tools
sudo apt-get update
sudo apt-get install -y mssql-tools unixodbc-dev

# Add to PATH (optional but recommended)
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

RHEL/SLES

# Add tools repository
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo  # Adjust for RHEL/SLES version

# Install tools
sudo yum install -y mssql-tools unixODBC-devel  # RHEL
sudo zypper install -y mssql-tools unixODBC-devel  # SLES

# Add to PATH
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

Verify the tools work by connecting to SQL Server:

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

Enter the SA password when prompted. You should see the SQL Server command prompt (1>).


6. Security Best Practices

  • Change SA Password: Use a strong, complex password (mix of uppercase, lowercase, numbers, symbols).
  • Disable SA (Optional): Create a new sysadmin user and disable SA to reduce attack surface:
    -- Run in sqlcmd
    CREATE LOGIN [newadmin] WITH PASSWORD = 'StrongPassword123!';
    ALTER SERVER ROLE sysadmin ADD MEMBER [newadmin];
    ALTER LOGIN SA DISABLE;
    
  • Use Least Privilege: Avoid running SQL Server as root. The default installation runs as the mssql user.

By following these steps, you’ll have a fully functional SQL Server instance running on Linux. For version-specific details (e.g., SQL Server 2025 preview), refer to Microsoft’s official documentation.

0