Mục lục

Giới thiệu
Microsoft SQL Server là hệ quản trị cơ sở dữ liệu do Microsoft phát triển, được sử dụng rộng rãi trong các hệ thống doanh nghiệp trên toàn thế giới. Trước đây, SQL Server chỉ hỗ trợ hệ điều hành Windows, tuy nhiên từ năm 2016, Microsoft đã chính thức hỗ trợ SQL Server trên nền tảng Linux, trong đó có Ubuntu.
Tài liệu này do Zhost biên soạn nhằm hướng dẫn cài đặt Microsoft SQL Server 2019 trên Ubuntu 20.04, 18.04 và 16.04. Nội dung phù hợp cho môi trường Cloud Server, VPS và máy chủ vật lý, giúp người dùng triển khai SQL Server nhanh chóng và ổn định trên hệ điều hành Ubuntu.
Hướng dẫn thực hiện
Bước 1: Cập nhật 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:
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:
[ -f /var/run/reboot-required ] && sudo reboot -f
Bước 2: Khai báo public GPG keys và SQL Server 2019 repository.
- Khai báo public repository
GPG keysbằng lệnh:
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:
- Ubuntu 16.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/16.04/mssql-server-2019.list)"
-
- Ubuntu 18.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/18.04/mssql-server-2019.list)"
-
- Ubuntu 20.04:
sudo add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/20.04/mssql-server-2019.list)"
Bước 3: Cài đặt SQL Server 2019.
Chạy lệnh sau để thực hiện cài đặt SQL Server 2019:
sudo apt update -y && sudo apt install mssql-server -y
Bước 4: 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:
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.
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
Nếu như hiển thị trạng thái là active thì service đã hoạt động.
- Tiếp theo cài đặt
MS SQL toolsvàunixODBCplugin bằng lệnh sau:- 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
-
- 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
-
- 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.
- Dành cho phiên đăng nhập:
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile source ~/.bash_profile
-
- Dành cho không cần đăng nhập:
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc source ~/.bashrc
- Thử đăng nhập vào
MS SQL Consolevà liệt kê danh sách Database:
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)
Kết luận
Qua các bước trên, bạn đã hoàn tất việc cài đặt và cấu hình Microsoft SQL Server 2019 trên Ubuntu một cách đầy đủ và sẵn sàng sử dụng trong môi trường thực tế. SQL Server hoạt động ổn định trên Linux giúp hệ thống vừa tận dụng được sức mạnh của hệ sinh thái Microsoft, vừa khai thác được tính linh hoạt và hiệu năng của Ubuntu.
Zhost khuyến nghị sau khi cài đặt, bạn nên tiếp tục cấu hình firewall, backup dữ liệu định kỳ và bảo mật tài khoản SQL Server để đảm bảo hệ thống vận hành an toàn và ổn định lâu dài. Chúc các bạn thành công!
