在Azure虚拟机上安装SQL server

时间:2022-09-25 17:18:32

Azure虽然向用户提供SQL paas服务,但是大多数用户还是习惯在用虚拟机自己搭建SQL server,这样的好处是便于后期最大化的扩展,所以鉴于这些情况,所以觉得有必要写这篇博客。

首先,我们要建立一台虚拟机,鉴SQL server对虚拟机内存的要求最低为3GB,所以笔者建议虚拟机选择A3或者A4,当然也可以选择A1的虚拟机,然后再升为A3或者A4,因为本次环境只是测试,这里我就直接建立A3虚拟机,OS为centos7.1

第一步,建立虚拟机,过程省略,有不会的读者自行百度,结果如下

在Azure虚拟机上安装SQL server

第二步,登陆虚拟机,切换到root用户

[yangyang@sqlserver1 ~]$ sudo su - root

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things: #) Respect the privacy of others.
#) Think before you type.
#) With great power comes great responsibility. [sudo] password for yangyang:
Sorry, try again.
[sudo] password for yangyang:
[root@sqlserver1 ~]#

第三步,获得yum源,yum的repo文件地址

下载到本地

[root@sqlserver1 ~]# wget https://packages.microsoft.com/config/rhel/7/mssql-server.repo
---- ::-- https://packages.microsoft.com/config/rhel/7/mssql-server.repo
Resolving packages.microsoft.com (packages.microsoft.com)... 13.75.127.55
Connecting to packages.microsoft.com (packages.microsoft.com)|13.75.127.55|:... connected.
HTTP request sent, awaiting response... OK
Length: [application/octet-stream]
Saving to: 鈓ssql-server.repo? %[======================================>] --.-K/s in 0s -- :: (13.7 MB/s) - 鈓ssql-server.repo?saved [/] [root@sqlserver1 ~]#

接下来把文件复制到/etc/yum.repos.d目录

[root@sqlserver1 ~]# cp mssql-server.repo /etc/yum.repos.d/

更新yum信息

[root@sqlserver1 ~]# yum makecache
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB :
extras | 3.4 kB :
openlogic | 1.3 kB :
packages-microsoft-com-mssql-server | 2.9 kB :
updates | 3.4 kB :
(/): base//x86_64/group_gz | kB :
(/): base//x86_64/primary_db | 5.6 MB :
(/): base//x86_64/other_db | 2.4 MB :
(/): base//x86_64/filelists_db | 6.6 MB :
(/): extras//x86_64/prestodelta | kB :
(/): extras//x86_64/filelists_db | kB :
(/): extras//x86_64/primary_db | kB :
(/): extras//x86_64/other_db | kB :
(/): openlogic//x86_64/filelists | 8.8 kB :
(/): openlogic//x86_64/primary | kB :
(/): openlogic//x86_64/other | 4.0 kB :
(/): packages-microsoft-com-mssql-server/primary_db | 3.2 kB :
(/): packages-microsoft-com-mssql-server/other_db | B :
(/): packages-microsoft-com-mssql-server/filelists_db | 1.8 kB :
(/): updates//x86_64/prestodelta | kB :
(/): updates//x86_64/filelists_db | kB :
(/): updates//x86_64/primary_db | 1.2 MB :
(/): updates//x86_64/other_db | MB :
Determining fastest mirrors
openlogic /
openlogic /
openlogic /
Metadata Cache Created

第四步,通过yum安装sql server

yum查找mssql的相关信息

[root@sqlserver1 ~]# yum search mssql
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
============================== N/S matched: mssql ==============================
mssql-server.x86_64 : Microsoft(R) SQL Server(R) Relational Database Engine
mssql-server-ha.x86_64 : Microsoft(R) SQL Server(R) Relational Database Engine Name and summary matches only, use "search all" for everything.

我们可以看到有两个版本,ha应该是做高可用性的版本,所以我们选择安装第一个

[root@sqlserver1 ~]# yum install mssql-server -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mssql-server.x86_64 :14.0.100.187- will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mssql-server x86_64 14.0.100.187- packages-microsoft-com-mssql-server M Transaction Summary
================================================================================
Install Package Total download size: M
Installed size: M
Downloading packages:
warning: /var/cache/yum/x86_64//packages-microsoft-com-mssql-server/packages/mssql-server-14.0.100.187-.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Public key for mssql-server-14.0.100.187-.x86_64.rpm is not installed
mssql-server-14.0.100.187-.x86_64.rpm | MB :
Retrieving key from https://packages.microsoft.com/keys/microsoft.asc
Importing GPG key 0xBE1229CF:
Userid : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
Fingerprint: bc52 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
From : https://packages.microsoft.com/keys/microsoft.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mssql-server-14.0.100.187-.x86_64 / +-------------------------------------------------------------------+
| Please run /opt/mssql/bin/sqlservr-setup to complete the setup of |
| Microsoft(R) SQL Server(R). |
+-------------------------------------------------------------------+ Verifying : mssql-server-14.0.100.187-.x86_64 / Installed:
mssql-server.x86_64 :14.0.100.187- Complete!

看到上面的结果表示安装完成

第五步,配置SQL server

[root@sqlserver1 ~]# cd /opt/mssql/bin
[root@sqlserver1 bin]# pwd
/opt/mssql/bin
[root@sqlserver1 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
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:
Please confirm the password for the system administrator (SA) account: 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
ln -s '/usr/lib/systemd/system/mssql-server.service' '/etc/systemd/system/multi-user.target.wants/mssql-server.service'
ln -s '/usr/lib/systemd/system/mssql-server-telemetry.service' '/etc/systemd/system/multi-user.target.wants/mssql-server-telemetry.service' Setup completed successfully.
[root@sqlserver1 bin]#

这样就配置成功了

第六步,检查状态

[root@sqlserver1 bin]# systemctl status mssql-server
mssql-server.service - Microsoft(R) SQL Server(R) Database Engine
Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled)
Active: active (running) since Thu -- :: UTC; 5min ago
Main PID: (sqlservr)
CGroup: /system.slice/mssql-server.service
忖1629 /opt/mssql/bin/sqlservr
忖1639 /opt/mssql/bin/sqlservr Jan :: sqlserver1 sqlservr[]: -- ::34.22 spid6s ....
Jan :: sqlserver1 sqlservr[]: -- ::34.86 spid6s ....
Jan :: sqlserver1 sqlservr[]: -- ::34.87 spid20s ....
Jan :: sqlserver1 sqlservr[]: -- ::34.87 spid20s ....
Jan :: sqlserver1 sqlservr[]: -- ::34.92 spid20s ....
Jan :: sqlserver1 sqlservr[]: -- ::35.01 spid5s ....
Jan :: sqlserver1 sqlservr[]: -- ::35.99 spid38s ...
Jan :: sqlserver1 systemd[]: [/usr/lib/systemd/system/mssql-serve...e'
Jan :: sqlserver1 systemd[]: [/usr/lib/systemd/system/mssql-serve...e'
Jan :: sqlserver1 systemd[]: [/usr/lib/systemd/system/mssql-serve...e'
Hint: Some lines were ellipsized, use -l to show in full.

可以从上图看出SQL server已经在运行,这一步说明SQL server已经完全安装成功了

但是接下来我们还要做一个工作,就是检查,我们需要使用SQL ToolTs,sqlcmd是mssql ODBC的一个工具,可以通过yum的repo直接yum安装

首先下载yum的repo

[root@sqlserver1 bin]# cd /root/
[root@sqlserver1 ~]# wget https://packages.microsoft.com/config/rhel/7/prod.repo
---- ::-- https://packages.microsoft.com/config/rhel/7/prod.repo
Resolving packages.microsoft.com (packages.microsoft.com)... 13.75.127.55
Connecting to packages.microsoft.com (packages.microsoft.com)|13.75.127.55|:... connected.
HTTP request sent, awaiting response... OK
Length: [application/octet-stream]
Saving to: 鈖rod.repo? %[======================================>] --.-K/s in 0s -- :: (31.7 MB/s) - 鈖rod.repo?saved [/] [root@sqlserver1 ~]# mv prod.repo /etc/yum.repos.d/
[root@sqlserver1 ~]# yum makecache
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB :
extras | 3.4 kB :
openlogic | 1.3 kB :
packages-microsoft-com-mssql-server | 2.9 kB :
packages-microsoft-com-prod | 2.9 kB :
updates | 3.4 kB :
(/): packages-microsoft-com-prod/filelists_db | 3.2 kB :
(/): packages-microsoft-com-prod/other_db | kB :
(/): packages-microsoft-com-prod/primary_db | 6.7 kB :
Loading mirror speeds from cached hostfile
Metadata Cache Created

接下来进行安装

[root@sqlserver1 ~]#  yum install mssql-tools
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mssql-tools.x86_64 :14.0.1.246- will be installed
--> Processing Dependency: msodbcsql for package: mssql-tools-14.0.1.246-.x86_64
--> Running transaction check
---> Package msodbcsql.x86_64 :13.1.1.0- will be installed
--> Processing Dependency: unixODBC-utf16 for package: msodbcsql-13.1.1.0-.x86_64
--> Processing Dependency: libodbcinst.so.()(64bit) for package: msodbcsql-13.1.1.0-.x86_64
--> Running transaction check
---> Package unixODBC-utf16.x86_64 :2.3.- will be installed
--> Processing Dependency: libltdl.so.()(64bit) for package: unixODBC-utf16-2.3.-.x86_64
--> Running transaction check
---> Package libtool-ltdl.x86_64 :2.4.-.el7_2 will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mssql-tools x86_64 14.0.1.246- packages-microsoft-com-prod k
Installing for dependencies:
libtool-ltdl x86_64 2.4.-.el7_2 base k
msodbcsql x86_64 13.1.1.0- packages-microsoft-com-prod 3.9 M
unixODBC-utf16 x86_64 2.3.- packages-microsoft-com-prod k Transaction Summary
================================================================================
Install Package (+ Dependent packages) Total download size: 4.5 M
Installed size: 4.5 M
Is this ok [y/d/N]: y
Downloading packages:
(/): libtool-ltdl-2.4.-.el7_2.x86_64.rpm | kB :
(/): mssql-tools-14.0.1.246-.x86_64.rpm | kB :
(/): unixODBC-utf16-2.3.-.x86_64.rpm | kB :
(/): msodbcsql-13.1.1.0-.x86_64.rpm | 3.9 MB :
--------------------------------------------------------------------------------
Total 3.0 MB/s | 4.5 MB :
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libtool-ltdl-2.4.-.el7_2.x86_64 /
Installing : unixODBC-utf16-2.3.-.x86_64 /
The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746838 and found in
/usr/share/doc/msodbcsql/LICENSE.TXT . By entering 'YES',
you indicate that you accept the license terms. Do you accept the license terms? (Enter YES or NO)
YES
Installing : msodbcsql-13.1.1.0-.x86_64 /
The license terms for this product can be downloaded from
http://go.microsoft.com/fwlink/?LinkId=746949 and found in
/usr/share/doc/mssql-tools/LICENSE.txt . By entering 'YES',
you indicate that you accept the license terms. Do you accept the license terms? (Enter YES or NO)
YES
Installing : mssql-tools-14.0.1.246-.x86_64 /
Verifying : libtool-ltdl-2.4.-.el7_2.x86_64 /
Verifying : msodbcsql-13.1.1.0-.x86_64 /
Verifying : mssql-tools-14.0.1.246-.x86_64 /
Verifying : unixODBC-utf16-2.3.-.x86_64 / Installed:
mssql-tools.x86_64 :14.0.1.246- Dependency Installed:
libtool-ltdl.x86_64 :2.4.-.el7_2 msodbcsql.x86_64 :13.1.1.0-
unixODBC-utf16.x86_64 :2.3.- Complete!

安装成功,接下来测试连接

登陆,输入刚刚的密码,并创建数据库test

[root@sqlserver1 ~]# sqlcmd -S localhost -U SA
Password:
> create database test;
> go
> use test;
> go
Changed database context to 'test'.

查看数据库

> select name from sys.databases;
> go
name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
test ( rows affected)

创建表yytable,并插入数据,最后进行查询

> create table yytable ( id int, name varchar(), gender varchar(), age int)
> go
> insert into yytable values (, 'yangyang','male',);
> go ( rows affected)
> select * from yytable;
> go
id name gender age
----------- -------------------- -------------------- -----------
yangyang male ( rows affected)
>

说明该步骤成功,且SQL server也可以使用了