SQL Server也能部署在Linux環(huán)境?SQL Server 2019在CentOS7部署詳解
概述
我們知道SQL Server是微軟公司推出的重要的數(shù)據(jù)庫產(chǎn)品,通常情況下只支持部署在Windows平臺上。不過令人感到興奮的是,從SQL Server 2017開始支持 Linux系統(tǒng)。此 SQL Server 版本與運行在 Microsoft 操作系統(tǒng)上的 SQL Server 數(shù)據(jù)庫引擎相同,具有許多相似的功能和服務(wù)。下面分享一下CentOS 7 上安裝 Microsoft SQL Server 2019 的步驟。
安裝過程
Step1:在 CentOS 7 上安裝 Microsoft SQL Server 2019
添加SQL Server 2019 鏡像倉庫
Microsoft SQL Server 2019 可供一般用途使用。通過在終端上運行以下命令,將存儲庫添加到 CentOS 7。
- sudo curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2019.repo
這會將 SQL Server 2019 存儲庫下載到 /etc/yum.repos.d/mssql-server.repo
更新你的系統(tǒng)緩存
- sudo yum makecache # CentOS 7
安裝SQL Server 2019
- sudo yum install -y mssql-server
要獲取有關(guān)已安裝軟件包的信息,請運行:
- [root@test ~]# rpm -qi mssql-server
- Name : mssql-server
- Version : 15.0.4178.1
- Release : 3
- Architecture: x86_64
- Install Date: Fri 29 Oct 2021 02:15:59 PM CST
- Group : Unspecified
- Size : 1213647503
- License : Commercial
- Signature : RSA/SHA256, Wed 29 Sep 2021 01:09:50 AM CST, Key ID eb3e94adbe1229cf
- Source RPM : mssql-server-15.0.4178.1-3.src.rpm
- Build Date : Tue 28 Sep 2021 01:50:37 PM CST
- Build Host : hls-build-pipeline-ub2-prod-build-cent73-02
- Relocations : (not relocatable)
- Summary : Microsoft SQL Server Relational Database Engine
- Description :
- The mssql-server package contains the Microsoft SQL Server Relational Database Engine.
Step 2:初始化 MS SQL 數(shù)據(jù)庫引擎
軟件包安裝完成后,運行 mssql-conf setup 并按照提示設(shè)置 SA 密碼并選擇您的版本。
- 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)
- 7) Enterprise Core (PAID)
- 8) I bought a license through a retail sales channel and have a product key to enter.
我會選擇 2 – Developer(免費)。
接受許可條款
- The license terms for this product can be found in
- /usr/share/doc/mssql-server or downloaded from:
- https://go.microsoft.com/fwlink/?LinkId=855862&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
設(shè)置 SQL Server 系統(tǒng)管理員密碼
- Enter the SQL Server system administrator password: <Password>
- Confirm the SQL Server system administrator password:<Confirm Password>
- Configuring SQL Server...
- sqlservr: This program requires a machine with at least 2000 megabytes of memory.
- /opt/mssql/bin/sqlservr: This program requires a machine with at least 2000 megabytes of memory.
- Initial setup of Microsoft SQL Server failed. Please consult the ERRORLOG
- in /var/opt/mssql/log for more information.
step3:安裝 SQL Server 命令行工具
然后使用 unixODBC 開發(fā)包安裝 mssql-tools。
- sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
- sudo yum -y install mssql-tools unixODBC-devel
step 4:啟動并啟用 mssql-server 服務(wù)
啟動 mssql-server 服務(wù)
- sudo systemctl start mssql-server
設(shè)置系統(tǒng)啟動時自動啟動
- sudo systemctl enable mssql-server
添加/opt/mssql/bin/ 到您的 $PATH 變量:
- echo 'export PATH=$PATH:/opt/mssql/bin:/opt/mssql-tools/bin' | sudo tee /etc/profile.d/mssql.sh
獲取文件以在當(dāng)前 shell 會話中開始使用 MS SQL 可執(zhí)行二進(jìn)制文件
- source /etc/profile.d/mssql.sh
如果您有活動的 Firewalld 服務(wù),請允許遠(yuǎn)程主機的 SQL Server 端口連接:
- sudo firewall-cmd --add-port=1433/tcp --permanent
- sudo firewall-cmd --reload
Step5:測試 SQL Server
連接到 SQL Server 并驗證它是否正常工作。
- $ sqlcmd -S localhost -U SA
使用步驟 2 中設(shè)置的密碼進(jìn)行身份驗證。
顯示數(shù)據(jù)庫用戶:
- 1> select name from sysusers;
- 2> go
創(chuàng)建測試數(shù)據(jù)庫:
- # Create new
- CREATE DATABASE mytestDB
- SELECT Name from sys.Databases
- GO
- USE mytestDB
- CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
- INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
- GO
- SELECT * FROM Inventory LIMIT 1;
顯示 SQL Server 上的數(shù)據(jù)庫。
- 1> select name,database_id from sys.databases;
- 2> go
刪除數(shù)據(jù)庫:
- 1> drop database testDB;
- 2> go
部署管理工具 Azure Data Studio
- [root@test ~]# cd /usr/local/src
- [root@test src]# wget https://azuredatastudiobuilds.blob.core.windows.net/releases/1.13.0/azuredatastudio-linux-1.13.0.tar.gz
- [root@test src]# tar -xvf ./azuredatastudio-linux-1.13.0.tar.gz -C /usr/local
- [root@test src]# cd ../
- [root@test local]# echo 'export PATH="$PATH:/usr/local/azuredatastudio-linux-x64"' >> ~/.bashrc
- [root@test local]# source ~/.bashrc
- # 啟動圖形化數(shù)據(jù)庫操作界面
- [root@test local]# azuredatastudio
- # 配置非root用戶使用
- [root@test local]# exit
- [gjp@test local]# echo 'export PATH="$PATH:/usr/local/azuredatastudio-linux-x64"' >> ~/.bashrc
- [test@test local]# source ~/.bashrc
- # 此處需要安裝 libXScrnSaver 依賴 否則會報找不到 libgtk-3.so.0
- [root@test local]# yum install libXScrnSaver
- # 注意 此處使用的是圖形化安裝的CentOS7
- [test@test local]# azuredatastudio
- # windows訪問時記得關(guān)閉防火墻
- [root@test ~]# systemctl stop firewalld
- [root@test ~]# systemctl disable firewalld
總結(jié)
如果你厭倦了在Windows上部署SQL Server,也許你可以嘗試在Linux平臺上部署,Linux平臺上SQL Server,能帶給你不一樣的體驗。