Microsoft SQL Server là một hệ quản trị cơ sở dữ liệu rất phổ biến trên thế giới, nó là nơi chứa dữ liệu cho nhiều phần mềm doanh nghiệp lớn, nhiều website cũng như hệ thống sản xuất.
Trước đây SQL Server chỉ có trên Windows [Server], giờ cũng đã có bản dành cho Linux bên cạnh nhiều phần mềm khác như PowerShell, Visual Studio Code hay bộ nguồn JavaScript của trình duyệt Edge…
Trong bài viết này, mình sẽ hướng dẫn các bạn cách cài đặt và sử dụng SQL Server trên các bản distro Linux phổ biến.
1. Cài đặt SQL Server trên Linux
SQL Server yêu cầu cấu hình tối thiểu 3250 MB Ram.
Bạn cần sử dụng quyền superuser
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
5 để cài đặt.
1.1. Cài đặt SQL Server trên CentOS/RHEL
a. Tải Microsoft SQL Server Red Hat repository
curl //packages.microsoft.com/config/rhel/7/mssql-server.repo > /etc/yum.repos.d/mssql-server.repo
b. Cài đặt SQL Server
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7
c. Cấu hình SQL Server
Sau khi hoàn tất cài đặt packages, bạn cần chạy script để cấu hình.
Lưu ý: YES để chấp nhận license và password đạt yêu cầu 8 kí tự trở lên bao gồm kí tự viết hoa, viết thường, số, kí tự đặc biệt.
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
Cài đặt xong, kiểm tra nếu SQL Server service đang chạy
systemctl status mssql-server -l
● mssql-server.service - Microsoft[R] SQL Server[R] Database Engine Loaded: loaded [/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled] Active: active [running] since Thu 2016-11-17 03:43:32 UTC; 7min ago
1.2. Cài đặt SQL Server trên Ubuntu
a. Nhập public repository GPG keys
curl //packages.microsoft.com/keys/microsoft.asc | apt-key add -
b. Đăng ký Microsoft SQL Server Ubuntu repository
curl //packages.microsoft.com/config/ubuntu/16.04/mssql-server.list > /etc/apt/sources.list.d/mssql-server.list
c. Cài đặt SQL Server
sudo apt-get update
sudo apt-get install -y mssql-server
d. Cấu hình SQL Server
Cấu hình giống như hướng dẫn đối với CentOS
sudo /opt/mssql/bin/sqlservr-setup
Cài đặt xong, kiểm tra nếu SQL Server service đang chạy
systemctl status mssql-server
Sau khi cài đặt thành công, các bạn có thể kết nối SQL Server vNext CTP1 trên Linux thông qua MsSql Tool, bao gồm tiện ích Sqlcmd, Transact-SQL queries để tạo, truy vấn database và bcp để import-export dữ liệu số lượng lớn.
Lưu ý:
- Trong phần lớn các trường hợp, yêu cầu và tiến trình kết nối không khác nhau giữa các bản phân phối Linux
- Sqlcmd chỉ là 1 tool để kết nối SQL server nhằm quản lý và truy vấn dữ liệu. Bên cạnh đó, bạn có thể sử dụng SQL Server Management và Visual Studio Code.
2.1. Cài đặt MsSql Tool
Để cài đặt, bạn cần sử dụng quyền superuser
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
5
a. Đối với CentOS/RHEL
Tải file cấu hình Microsoft Red Hat repository
curl //packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repo
Cài đặt MsSql Tool [YES để chấp nhận license term]
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 0
b. Đối với Ubuntu
Nhập GPG keys repository
curl //packages.microsoft.com/keys/microsoft.asc | apt-key add -
Đăng kí Microsoft Ubuntu repository
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 2
Cập nhật hệ thống và cài đặt MsSql Tool
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 3
2.2. Kết nối và truy cập dữ liệu SQL Server
a. Kết nối cơ sở dữ liệu SQL Server
Để kết nối SQL Server trên Linux, bạn cần sử dụng SQL Authentication. Với việc kết nối từ xa, bạn cần mở port SQL Server [mặc định là port 1433 qua tcp] trên Firewall.
Nếu bạn thiết lập tường lửa bằng Iptables:
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 4
Nếu bạn thiết lập tường lửa bằng FirewallD:
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 5
Kết nối SQL Server qua
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
7 với các thông số về SQL Server name [-S], username [-U] và password [-P]
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 6
Để kết nối từ xa, chỉ định tên máy hoặc IP cho thông số -S, ví dụ
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 6
b. Truy vấn cơ sở dữ liệu SQL Server
Sau khi kết nối thành công SQL Server, bạn có thể chạy các truy vấn đề trả về thông tin hoặc tạo các cơ sở dữ liệu. Sau đây là một số ứng dụng cơ bản
Truy vấn danh sách cơ sở dữ liệu
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 8
Kết quả trả về
sudo yum install -y mssql-server
Installed: mssql-server.x86_64 0:14.0.1.246-6 Dependency Installed: bzip2.x86_64 0:1.0.6-13.el7 gdb.x86_64 0:7.6.1-80.el7 9
Tạo và sử dụng cơ sở dữ liệu mới
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
0
Sử dụng CSDL testdb vừa tạo
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
1
Tạo và chèn dữ liệu vào 1 bảng trong cơ sở dữ liệu mới
Tạo 1 bảng gồm 3 trường:
- Trường số thứ tự ID [key], loại dữ liệu INT
- Trường tên Name, loại dữ liệu NVARCHAR
- Trường số lượng Quantity loại dữ liệu INT
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
2
Nhập dữ liệu, mỗi dữ liệu 1 dòng, các trường ngăn cách nhau bởi dấu
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
8và bảo vệ dữ liệu bằng text qualifier
sudo /opt/mssql/bin/sqlservr-setup
Microsoft[R] SQL Server[R] Setup You can abort setup at anytime by pressing Ctrl-C. Start this program with the --help option for information about running it in unattended mode. The license terms for this product can be downloaded from //go.microsoft.com/fwlink/?LinkId=746388 and found in /usr/share/doc/mssql-server/LICENSE.TXT. Do you accept the license terms? If so, please type "YES": YES Please enter a password for the system administrator [SA] account:password Please confirm the password for the system administrator [SA] account:password Setting system administrator [SA] account password... Do you wish to start the SQL Server service now? [y/n]: y Do you wish to enable SQL Server to start on boot? [y/n]: y Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service. Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service to /usr/lib/systemd/system/mssql-server-telemetry.service. Setup completed successfully.
9