Hướng dẫn cài sql server Informational
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. Show 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 LinuxSQL 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-setupMicrosoft(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 http://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/RHELa. Tải Microsoft SQL Server Red Hat repositorycurl https://packages.microsoft.com/config/rhel/7/mssql-server.repo > /etc/yum.repos.d/mssql-server.repob. Cài đặt SQL Serversudo yum install -y mssql-serverInstalled: 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 ServerSau 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-setupMicrosoft(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 http://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 Ubuntua. Nhập public repository GPG keyscurl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -b. Đăng ký Microsoft SQL Server Ubuntu repositorycurl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server.list > /etc/apt/sources.list.d/mssql-server.listc. Cài đặt SQL Serversudo apt-get updatesudo apt-get install -y mssql-serverd. Cấu hình SQL ServerCấ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 ý:
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-setupMicrosoft(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 http://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/RHELTải file cấu hình Microsoft Red Hat repository curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/msprod.repoCài đặt MsSql Tool (YES để chấp nhận license term) sudo yum install -y mssql-serverInstalled: 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 UbuntuNhập GPG keys repository curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -Đăng kí Microsoft Ubuntu repository sudo yum install -y mssql-serverInstalled: 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-serverInstalled: 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 Servera. 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-serverInstalled: 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-serverInstalled: 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-setupMicrosoft(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 http://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-serverInstalled: 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-serverInstalled: 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 ServerSau 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-serverInstalled: 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-serverInstalled: 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-setupMicrosoft(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 http://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-setupMicrosoft(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 http://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:
sudo /opt/mssql/bin/sqlservr-setupMicrosoft(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 http://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-setupMicrosoft(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 http://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-setupMicrosoft(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 http://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 |