Deploy SQL Server on Linux
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
-
Add the Microsoft repository.
NoteIn 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-2019repository onAlibaba 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-2019repository onAlibaba 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
-
-
Install SQL Server.
sudo yum install -y mssql-server -
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 -
-
Start SQL Server and enable it to start on boot.
sudo systemctl start mssql-server sudo systemctl enable mssql-server -
Check the service status.
systemctl status mssql-serverThe 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
-
Import the Microsoft GPG key.
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc -
Add the Microsoft repository.
NoteIn 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)" -
Install SQL Server.
sudo apt-get update sudo apt-get install -y mssql-server -
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 -
-
Start SQL Server and enable it to start on boot.
sudo systemctl start mssql-server sudo systemctl enable mssql-server -
Check the service status.
systemctl status mssql-serverThe 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
-
Download the Microsoft repository configuration file.
NoteIn 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
-
-
(Optional) If you have a previous version of
mssql-toolsinstalled, remove the old unixODBC packages.sudo yum remove mssql-tools unixODBC-utf16 unixODBC-utf16-devel -
Install
mssql-tools18with the unixODBC developer package.sudo yum install -y mssql-tools18 unixODBC-devel -
(Optional) Update
mssql-toolsto the latest version.sudo yum check-update sudo yum update mssql-tools18 -
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
-
Download the public repository GPG key.
curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc -
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.listFor 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 -
Install
mssql-tools18with the unixODBC developer package.sudo apt-get update sudo apt-get install mssql-tools18 unixodbc-dev -
(Optional) Update
mssql-toolsto the latest version.sudo apt-get update sudo apt-get install mssql-tools18 -
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.
-
Connect to SQL Server.
NoteTo 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
localhostwith the name or IP address of your server.
Use the
saaccount for the initial login. When you log in with your new account, replacesawith the new username.sqlcmd -No -S localhost -U saParameter descriptions:
-S: The server name or IP address.
-U: The username.
-
-
Create a new account and disable the
saaccount.-
Replace
<YOUR_USER>with your desired username. -
Replace
<YOUR_PASSWORD>with your desired password.
-
Create the login.
CREATE LOGIN <YOUR_USER> WITH PASSWORD = '<YOUR_PASSWORD>';Run GO to apply the command.
GO -
Assign the
sysadminrole.ALTER SERVER ROLE sysadmin ADD MEMBER <YOUR_USER>;Run GO to make the command take effect.
GO -
Disable the
salogin.ALTER LOGIN sa DISABLE;Execute GO to make the command take effect.
GO -
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.
GOIn the output,
is_disabled=1indicates that the login is disabled, andis_disabled=0indicates 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.