Deploy SQL Server on Linux

Updated at:

SQL Server is a relational database management system (DBMS) from Microsoft. This guide shows you how to deploy SQL Server on an ECS instance running Linux.

Prerequisites

  • The instance must have at least 2 GiB of memory.

  • The instance must have a public IP address or an Elastic IP Address (EIP). If you are unsure how to enable internet access, see Enable internet access.

  • The instance's security group must have an inbound rule that allows traffic on port 22. For more information, see Add a security group rule .

Install SQL Server

Alibaba Cloud Linux 2/3, CentOS 7/8

  1. Add the Microsoft repository.

    Note

    In the following command, replace <package> with the Microsoft repository for your operating system. For more information, see Microsoft Repository.

    sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/<package>
    • For example, to add the mssql-server-2019 repository on Alibaba Cloud Linux 3/CentOS 8:

      sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/8/mssql-server-2019.repo
    • For example, to add the mssql-server-2019 repository on Alibaba Cloud Linux 2/CentOS 7:

      sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2019.repo
  2. Install SQL Server.

    sudo yum install -y mssql-server
  3. Select an edition and set the SA password.

    Note
    • The Evaluation, Developer, and Express editions are available for free.

    • The password must be at least 8 characters long and contain characters from three of the following four categories: uppercase letters, lowercase letters, digits, and special characters.

    sudo /opt/mssql/bin/mssql-conf setup
  4. Start SQL Server and enable it to start on boot.

    sudo systemctl start mssql-server
    sudo systemctl enable mssql-server
  5. Check the service status.

    systemctl status mssql-server

    The following output indicates that the service started successfully.

    [root@iZxxx opt]# systemctl status mssql-server
    ● mssql-server.service - Microsoft SQL Server Database Engine
       Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2024-12-23 15:46:37 CST; 2h 44min ago
         Docs: https://docs.microsoft.com/en-us/sql/linux
     Main PID: 2279 (sqlservr)
       CGroup: /system.slice/mssql-server.service
               ├─2279 /opt/mssql/bin/sqlservr
               └─2298 /opt/mssql/bin/sqlservr

Ubuntu

  1. Import the Microsoft GPG key.

    curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
  2. Add the Microsoft repository.

    Note

    In the following command, replace <package> with the SQL Server package for your operating system. For more information, see Microsoft Repository.

    sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/<package>)"

    For example, to register the Microsoft repository on Ubuntu 20.04:

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

    sudo apt-get update
    sudo apt-get install -y mssql-server
  4. Select an edition and set the SA password.

    Note
    • The Evaluation, Developer, and Express editions are available for free.

    • The password must be at least 8 characters long and contain characters from three of the following four categories: uppercase letters, lowercase letters, digits, and special characters.

    sudo /opt/mssql/bin/mssql-conf setup
  5. Start SQL Server and enable it to start on boot.

    sudo systemctl start mssql-server
    sudo systemctl enable mssql-server
  6. Check the service status.

    systemctl status mssql-server

    The following output indicates that the service started successfully.

    [root@iZxxx opt]# systemctl status mssql-server
    ● mssql-server.service - Microsoft SQL Server Database Engine
       Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
       Active: active (running) since Mon 2024-12-23 15:46:37 CST; 2h 44min ago
         Docs: https://docs.microsoft.com/en-us/sql/linux
     Main PID: 2279 (sqlservr)
       CGroup: /system.slice/mssql-server.service
               ├─2279 /opt/mssql/bin/sqlservr
               └─2298 /opt/mssql/bin/sqlservr

Install SQL Server CLI

Alibaba Cloud Linux 2/3, CentOS 7/8

  1. Download the Microsoft repository configuration file.

    Note

    In the following command, replace <package> with the Microsoft repository configuration file for your operating system. For more information, see Microsoft Repository.

    curl https://packages.microsoft.com/config/rhel/<package> | sudo tee /etc/yum.repos.d/mssql-release.repo
    • For example, to download the Microsoft repository configuration file on Alibaba Cloud Linux 3/CentOS 8:

      curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo
    • For example, to download the Microsoft repository configuration file on Alibaba Cloud Linux 2/CentOS 7:

      curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo
  2. (Optional) If you have a previous version of mssql-tools installed, remove the old unixODBC packages.

    sudo yum remove mssql-tools unixODBC-utf16 unixODBC-utf16-devel
  3. Install mssql-tools18 with the unixODBC developer package.

    sudo yum install -y mssql-tools18 unixODBC-devel
  4. (Optional) Update mssql-tools to the latest version.

    sudo yum check-update
    sudo yum update mssql-tools18
  5. Configure the environment variables.

    • Make the tools available for login sessions.

      echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bash_profile
      source ~/.bash_profile
    • Make the tools available for interactive/non-login sessions.

      echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
      source ~/.bashrc

Ubuntu

  1. Download the public repository GPG key.

    curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
  2. Register the Microsoft Ubuntu repository.

    In the following command, replace the <Version> parameter with your Ubuntu version.

    curl https://packages.microsoft.com/config/ubuntu/<Version>/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list

    For example, to register the Microsoft repository on Ubuntu 20.04:

    curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
  3. Install mssql-tools18 with the unixODBC developer package.

    sudo apt-get update
    sudo apt-get install mssql-tools18 unixodbc-dev
  4. (Optional) Update mssql-tools to the latest version.

    sudo apt-get update  
    sudo apt-get install mssql-tools18
  5. Configure the environment variables.

    • Make the tools available for login sessions.

      echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bash_profile
      source ~/.bash_profile
    • Make the tools available for interactive/non-login sessions.

      echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
      source ~/.bashrc

Connect to SQL Server and disable sa

For security, after your first connection with the sa account, create a new account and disable the sa account.

  1. Connect to SQL Server.

    Note

    To connect to SQL Server remotely:

    • Add an inbound rule to the security group to allow traffic on port 1433. For more information, see Add a security group rule.

    • Replace localhost with the name or IP address of your server.

    Use the sa account for the initial login. When you log in with your new account, replace sa with the new username.

    sqlcmd -No -S localhost -U sa

    Parameter descriptions:

    -S: The server name or IP address.

    -U: The username.

  2. Create a new account and disable the sa account.

    • Replace <YOUR_USER> with your desired username.

    • Replace <YOUR_PASSWORD> with your desired password.

    1. Create the login.

      CREATE LOGIN <YOUR_USER> WITH PASSWORD = '<YOUR_PASSWORD>';

      Run GO to apply the command.

      GO
    2. Assign the sysadmin role.

      ALTER SERVER ROLE sysadmin ADD MEMBER <YOUR_USER>;

      Run GO to make the command take effect.

      GO
    3. Disable the sa login.

      ALTER LOGIN sa DISABLE;

      Execute GO to make the command take effect.

      GO
    4. Verify that the changes have taken effect.

      SELECT name, is_disabled FROM sys.server_principals WHERE name IN ('sa', '<YOUR_USER>');

      Run GO to make the command take effect.

      GO

      In the output, is_disabled=1 indicates that the login is disabled, and is_disabled=0 indicates that the login is enabled.

      1> SELECT name, is_disabled FROM sys.server_principals WHERE name IN ('sa', 'admin');
      2> GO
      name                                                                                                                            is_disabled
      -------------------------------------------------------------------------------------------------------------------------------- -----------
      sa                                                                                                                                        1
      admin                                                                                                                                     0

Related documents

  • For a managed database service with high availability, reliability, security, and scalability, consider Alibaba Cloud ApsaraDB RDS. ApsaraDB RDS is a stable, reliable, and scalable relational database service that supports MySQL, SQL Server, PostgreSQL, and MariaDB engines. It provides comprehensive solutions for disaster recovery, backup, recovery, and migration.

  • You can use Data Transmission Service (DTS) to seamlessly migrate your self-managed databases to Alibaba Cloud databases. For more information, see Database migration solutions.