0
点赞
收藏
分享

微信扫一扫

@Linux系统安装部署Sql Server(MSSQL)

醉东枫 2022-10-09 阅读 90


文章目录

  • ​​1.Sql server安装概述​​
  • ​​2.基础环境​​
  • ​​3.安装 Sql Server​​
  • ​​4.配置Sql Server​​
  • ​​5.测试连接MMSQL​​
  • ​​1)安装配置mmsql连接工具​​
  • ​​2)测试连接mssql​​
  • ​​6.MSSQL基础操作命令​​


@Linux系统安装部署Sql Server(MSSQL)_db

1.Sql server安装概述

安装sql server数据库,有最简单的两种方式;
第一种,使用yum、dnf等安装包管理工具安装
第二种,使用rpm直接安装即可

2.基础环境

性能要求一般:1c 2G
性能大于以上要求,其他也没有什么特殊要求

​​【Microsoft Red Hat存储库】​​

#关闭selinux
getenforce
setenforce 0 #临时关闭,永久关闭就更改配置文件



#查看并关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl enable firewalld.service



#生产环境,不关闭防火墙的话,就配置开通端口,防止连接阻断
firewall-cmd --state
firewall-cmd --zone=public --add-port=1433/tcp --permanent
firewall-cmd --reload




#安装配置Microsoft Red Hat存储库
curl https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo >

3.安装 Sql Server

#使用yum 或者 dnf,按照系统默认的安装包管理器来,不同系统可能不同
yum install

4.配置Sql Server

安装完成后,可运行配置脚本并按照提示一步一步向下执行操作

#运行以下可执行程序
[root@server ~]# 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.

Details about editions can be found at
https://go.microsoft.com/fwlink/?LinkId=852748&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 # 输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]:y #接受许可条款

Enter the SQL Server system administrator password: #设置sa的账号密码,sa为默认超级管理账户
Confirm the SQL Server system administrator password: #确认SQL Server系统管理员密码:
Configuring SQL Server...

ForceFlush is enabled for this instance.
ForceFlush feature is enabled for log durability.
Created symlink from /etc/systemd/system/multi-user.target.wants/mssql-server.service to /usr/lib/systemd/system/mssql-server.service.
Setup has completed successfully. SQL Server is now starting.






-------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
sudo /opt/mssql/bin/mssql-conf setup
SQL Server :
1) Evaluation (180 )
2) Developer ()
3) Express ()
4) Web ()
5) Standard ()
6) Enterprise ()
7) Enterprise Core ()
8)

:
https://go.microsoft.com/fwlink/?LinkId=852748&clcid=0x804

使
Microsoft



(1-8): 2
:
/usr/share/doc/mssql-server :
https://go.microsoft.com/fwlink/?LinkId=855862&clcid=0x804

:
https://go.microsoft.com/fwlink/?LinkId=853010&clcid=0x804

? [Yes/No]:yes


SQL Server :
(1) English
(2) Deutsch
(3) Español
(4) Français
(5) Italiano
(6)
(7)
(8) Português
(9) Русский
(10)
(11)
1-11:10
SQL Server :
SQL Server :
SQL Server......







#查看文件mmsql位置
[root@server ~]# find / -name mssql
/var/opt/mssql
/usr/share/grafana/public/app/plugins/datasource/mssql
/opt/mssql




#安装程序成功完成,启动SQL Server,并加入开机自启
systemctl daemon-reload
systemctl start mssql-server.service
systemctl enable mssql-server.service




#查看运行状态
systemctl status mssql-server



#查看当前安装版本
[root@server ~]# rpm -qa | grep mssql
mssql-server-14.0.3451.2-1.x86_64




#查看服务端口监听状态
[root@server ~]# netstat -lntp |grep sqlser
tcp 0 0 0.0.0.0:1433 0.0.0.0:* LISTEN 5743/sqlservr
tcp 0 0 127.0.0.1:1434 0.0.0.0:* LISTEN 5743/sqlservr
tcp6 0 0 :::1433 :::* LISTEN 5743/sqlservr
tcp6 0 0 ::1:1434 :::*

5.测试连接MMSQL

1)安装配置mmsql连接工具

​​【Sql Server tools】​​

#安装Microsoft Red Hat存储库
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo



#使用 unixODBC 开发人员包安装 mssql-tools,安装mssql-tools工具
sudo yum install -y mssql-tools unixODBC-devel




#环境变量配置
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> /etc/profile
source /etc/profile

2)测试连接mssql

#查看命令详情
shell> sqlcmd -?
Microsoft (R) SQL Server Command Line Tool
Version 17.10.0001.1 Linux
Copyright (C) 2017 Microsoft Corporation. All rights reserved.

usage: sqlcmd [-U login id] [-P password]
[-S server or Dsn if -D is provided]
[-H hostname] [-E trusted connection]
[-N Encrypt Connection][-C Trust Server Certificate]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w screen width]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-c cmdend]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-m errorlevel] [-V severitylevel] [-W remove trailing spaces]
[-u unicode output] [-r[0|1] msgs to stderr]
[-i inputfile] [-o outputfile]
[-k[1|2] remove[replace] control characters]
[-y variable length type display width]
[-Y fixed length type display width]
[-p[1] print statistics[colon format]]
[-R use client regional setting]
[-K application intent]
[-M multisubnet failover]
[-b On error batch abort]
[-D Dsn flag, indicate -S is Dsn]
[-X[1] disable commands, startup script, environment variables [and exit]]
[-x disable variable substitution]
[-g enable column encryption]
[-G use Azure Active Directory for authentication]
[-? show syntax summary]




#使用sqlcmd本地测试连接:sqlcmd -S localhost -U sa -P <password>
sqlcmd -S 127.0.0.1 -U sa -P hzl@20144
sqlcmd -S localhost -U sa -P hzl@20144


shell> sqlcmd -S localhost -U sa -P hzl@20144
1> CREATE DATABASE TESTDB; #创建数据库
2> SELECT Name from sys.databases; #查询所有数据库
3> go #输入Go执行输入的命令
Name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
TESTDB

(5 rows affected)

#数据插入存储
USE TESTDB;
CREATE TABLE dbo.Inventory (
id INT, name NVARCHAR(50),
quantity INT
);
INSERT INTO dbo.Inventory VALUES (1, 'banana', 150);
INSERT INTO dbo.Inventory VALUES (2, 'orange', 154);
GO





++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

《案例》:
shell> sqlcmd -S localhost -U sa -P hzl@20144
1> CREATE DATABASE MSSQLDB; #创建数据库
2> SELECT Name from sys.databases; #查询所有数据库
3> go #输入Go执行输入的命令
Name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
MSSQLDB

(5 rows affected)
1> USE MSSQLDB;
2> go
已将数据库上下文更改为 "MSSQLDB"
1> CREATE TABLE dbo.Inventory (id INT, name NVARCHAR(50),quantity INT);
2> go
1> INSERT INTO dbo.Inventory VALUES (1, 'zeny', 150);
2> go

(1 rows affected)
1> INSERT INTO dbo.Inventory VALUES (2, 'hzl', 154);
2> go

(1 rows affected)
1>
1> SELECT * FROM dbo.Inventory WHERE quantity > 152; #查询数据
2> go
id name quantity
----------- -------------------------------------------------- -----------
2 hzl 154

(1 rows affected)
1>

6.MSSQL基础操作命令

创建与删除登录用户

创建
exec sp_addlogin '登陆账号', '登陆密码'
例:
use test
GO
exec sp_addlogin 'jamysong', 'Sjm123456*'


删除
exec sp_droplogin '登陆账号'
例:
use test
GO
exec sp_droplogin 'jamysong'

创建与删除数据库用户

创建
exec sp_grantdbaccess '登录用户名','数据库用户名'
例:
use test
GO
exec sp_grantdbaccess 'jamysong','jamy'


删除
exec sp_dropuser '数据库用户名'
例:
use test
GO
exec sp_dropuser 'jamy'

给数据库用户添加角色

public
–public 角色是一个特殊的数据库角色,每个数据库用户都属于它。public 角色:
–捕获数据库中用户的所有默认权限。
–无法将用户、组或角色指派给它,因为默认情况下它们即属于该角色。
–含在每个数据库中,包括 master、msdb、tempdb、model 和所有用户数据库。
–无法除去。

db_owner
–进行所有数据库角色的活动,以及数据库中的其它维护和配置活动。
–该角色的权限跨越所有其它固定数据库角色。

db_accessadmin
–在数据库中添加或删除 Windows NT 4.0 或 Windows 2000 组和用户以及 SQL Server 用户。

db_datareader
–查看来自数据库中所有用户表的全部数据。

db_datawriter
–添加、更改或删除来自数据库中所有用户表的数据

db_ddladmin
–添加、修改或除去数据库中的对象(运行所有 DDL)

db_securityadmin
–管理 SQL Server 2000 数据库角色的角色和成员,并管理数据库中的语句和对象权限

db_backupoperator
–有备份数据库的权限


给数据库用户jamy赋予db_owner角色,该角色的所拥有的权限全部都赋予给了数据库用户jamy
exec sp_addrolemember 'db_owner','jamy'


举报

相关推荐

0 条评论