Hướng dẫn cài đặt Microsoft SQL Server 2019 trên Ubuntu 20.04, 18.04, 16.04

SQL Server là một hệ quản trị cơ sở dữ liệu được Microsoft phát triển và được sử dụng phổ biến trên toàn thế giới. Trước đây SQL Server chỉ hỗ trợ hệ điều hành Windows. Năm 2016 Microsoft chính thức hỗ trợ SQL Server cho các hệ điều hành Linux, trong đó có Ubuntu.

Cấu hình tối thiểu yêu cầu của server Ubuntu để cài đặt SQL như sau:

  • CPU tốc độ tối thiểu 1.4GHz nhưng khuyến nghị từ 2GHz trở lên
  • RAM: Tối thiểu 02GB
  • Ổ cứng còn trống tối thiểu 10GB

Trong bài viết này, Zhost sẽ hướng dẫn bạn cài đặt SQL Server trên hệ điều hành Ubuntu. Các bước thực hiện như sau:

huong-dan-cai-dat-sql-server-ubuntu-01

Update hệ điều hành

Trước khi cài đặt SQL, hãy cập nhật OS và các gói hiện có trong server bằng lệnh sau:

Zhost Tutorial
sudo apt update && sudo apt -y upgrade

Sau khi cập nhật xong, khởi động lại để server khởi động vào Kernel mới nhất:

Zhost Tutorial
[ -f /var/run/reboot-required ] && sudo reboot -f

Khai báo public GPG keys và SQL Server 2019 repository

Khai báo public repository GPG keys bằng lệnh:

Zhost Tutorial
sudo wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

Sau đó khai báo các repository chứa các gói cài đặt SQL Server 2019. Tùy phiên bản OS bạn dùng mà sử dụng lệnh phù hợp trong các lệnh sau

Zhost Tutorial
#Ubuntu 16.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2019.list)"
Zhost Tutorial
#Ubuntu 18.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"
Zhost Tutorial
#Ubuntu 20.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"

Cài đặt SQL Server 2019

Chạy lệnh sau để thực hiện cài đặt SQL Server 2019:

Zhost Tutorial
sudo apt update -y && sudo apt install mssql-server -y

Chạy các thiết lập ban đầu cho SQL Server

Sau khi quá trình cài đặt hoàn tất, bạn chạy lệnh sau để thực hiện các cài đặt ban đầu cho SQL Server:

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

Thực hiện lựa chọn phiên bản cài đặt SQL Server phù hợp với nhu cầu của bạn (trong bài viết này, Zhost sẽ lựa chọn bản Developer)

Đồng ý các điều khoản của phần mềm, và đặt mật khẩu cho user sa

Zhost Tutorial
root@eq-alphahp-pentamic:~# sudo /opt/mssql/bin/mssql-conf setup
Choose an edition of SQL Server:
  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.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=2109348&clcid=0x409

Use of PAID editions of this software requires separate licensing through a
Microsoft Volume Licensing program.
By choosing a PAID edition, you are verifying that you have the appropriate
number of licenses in place to install and run this software.

Enter your edition(1-8): 2
The license terms for this product can be found in
/usr/share/doc/mssql-server or downloaded from:
https://go.microsoft.com/fwlink/?LinkId=2104294&clcid=0x409

The privacy statement can be viewed at:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x409

Do you accept the license terms? [Yes/No]:yes

Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
Configuring SQL Server...

ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink /etc/systemd/system/multi-user.target.wants/mssql-server.service → /lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.
root@eq-alphahp-pentamic:~#

Sau khi thiết lập thành công, bạn kiểm tra hoạt động của dịch vụ SQL Server bằng lệnh: systemctl status mssql-server.service

Zhost Tutorial
root@eq-alphahp-pentamic:~# systemctl status mssql-server.service
● mssql-server.service - Microsoft SQL Server Database Engine
   Loaded: loaded (/lib/systemd/system/mssql-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2024-05-28 10:41:58 UTC; 3min 3s ago
     Docs: https://docs.microsoft.com/en-us/sql/linux
 Main PID: 2969 (sqlservr)
    Tasks: 150
   CGroup: /system.slice/mssql-server.service
           ├─2969 /opt/mssql/bin/sqlservr
           └─3044 /opt/mssql/bin/sqlservr
May 28 10:42:03 eq-alphahp-pentamic sqlservr[2969]: [158B blob data]
May 28 10:42:03 eq-alphahp-pentamic sqlservr[2969]: [155B blob data]
May 28 10:42:03 eq-alphahp-pentamic sqlservr[2969]: [61B blob data]

Tiếp theo cài đặt MS SQL tools và unixODBC plugin bằng lệnh sau:

Zhost Tutorial
#Ubuntu 16.04:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt update 
sudo ACCEPT_EULA=Y apt install mssql-tools unixodbc-dev
Zhost Tutorial
#Ubuntu 18.04:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt update 
sudo ACCEPT_EULA=Y apt install mssql-tools unixodbc-dev
Zhost Tutorial
#Ubuntu 20.04:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt update 
sudo ACCEPT_EULA=Y apt install mssql-tools unixodbc-dev

Thêm các biến môi trường cần thiết để có thể thao tác với SQL Server qua Command line.

Zhost Tutorial
#For login sessions
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
source ~/.bash_profile
Zhost Tutorial
#For interactive/non-login sessions
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

Thử đăng nhập vào MS SQL Console và liệt kê danh sách Database:

Zhost Tutorial
root@eq-alphahp-pentamic:~# sqlcmd -S 127.0.0.1 -U SA
Password:
1> select name from sys.databases;
2> go
name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
(4 rows affected)

Như vậy là quá trình cài đặt SQL server 2019 lên Cloud Server Ubuntu đã hoàn tất.