最近利用python写了一个备份sql server数据库的程序,但是是在windows测试运行的,打算用shell再写个脚本用来备份公司的linux服务器,刚开始编写脚本,又不能直接在生产坏境里面搞,只能自己先搭建一个虚拟环境了。
这个是一个新装的centos7系统镜像,没有升级和安装任何服务或工具
系统类型 | 系统版本 | ip地址 | 内存 |
---|---|---|---|
linux | centos7.5 | 192.168.100.132 | >2G |
内存一定要大于2GB,要不然就会出现小编一样的报错,具体报错继续向下看就知道了
建议提前关闭selinux,因为关闭selinux需要重启计算机,不关闭也没事的,按照方案一步一步往下走会有关闭selinux这一步的
链接:https://pan.baidu.com/s/18LzhuRDF_xIqC50LqLOUiw
提取码:1lqj
复制这段内容后打开百度网盘手机App,操作更方便哦
下面所用到的所有包都在这个里面,如果需要安装更高版本的sql server,请自行下载
地址:https://packages.microsoft.com/rhel/7/mssql-server-2017/mssql-server-14.0.3370.1-18.x86_64.rpm
下载速度贼慢,建议喝个茶,刷会抖音或者找小编谈会对象
地址:
https://packages.microsoft.com/rhel/7.3/prod/msodbcsql-13.1.6.0-1.x86_64.rpm
https://packages.microsoft.com/rhel/7.3/prod/mssql-tools-14.0.5.0-1.x86_64.rpm
一、如果linux上面可以连接公网,建议直接在Linux上面执行下面的命令,直接将这个包下载到Linux服务器上面,免得来回折腾
[root@localhost ~]# wget https://packages.microsoft.com/rhel/7/mssql-server-2017/mssql-server-14.0.3370.1-18.x86_64.rpm
-bash: wget: 未找到命令 ##没想到自己还安装了中文版本的系统。这个报错说没有wget命令,因为咱的系统是新的,当然什么都没有了很正常的
[root@localhost ~]# yum -y install wget #yum安装下wget这个命令
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 224 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:02
(4/4): updates/7/x86_64/primary_db | 5.6 MB 00:00:03
正在解决依赖关系
--> 正在检查事务
---> 软件包 wget.x86_64.0.1.14-18.el7_6.1 将被 安装
--> 解决依赖关系完成
依赖关系解决
=================================================================================================================================================
Package 架构 版本 源 大小
=================================================================================================================================================
正在安装:
wget x86_64 1.14-18.el7_6.1 base 547 k
事务概要
=================================================================================================================================================
安装 1 软件包
总下载量:547 k
安装大小:2.0 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/base/packages/wget-1.14-18.el7_6.1.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY0 B --:--:-- ETA
wget-1.14-18.el7_6.1.x86_64.rpm 的公钥尚未安装
wget-1.14-18.el7_6.1.x86_64.rpm | 547 kB 00:00:00
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 检索密钥
导入 GPG key 0xF4A80EB5:
用户ID : "CentOS-7 Key (CentOS 7 Official Signing Key) "
指纹 : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
软件包 : centos-release-7-5.1804.el7.centos.x86_64 (@anaconda)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : wget-1.14-18.el7_6.1.x86_64 1/1
验证中 : wget-1.14-18.el7_6.1.x86_64 1/1
已安装:
wget.x86_64 0:1.14-18.el7_6.1
完毕! #安装成功
2、根据上面的地址,可以直接在windows的浏览器里面输入地址,回车即下载
如果需要安装其他的最新版本的,可以在地址的上一级目录里面找下需要的版本。目前最新的就他了,进行安装
国外网站哈,速度贼慢
经过漫长的时间,终于下载完毕。自己百度一下怎么上传到linux上吧,这个非常简单的。
由于网盘里面已经将所有的包都下载下来了,所以上面的下载rpm包可以不看了,如果说你要安装更高版本的话,建议你看一下。
如果服务器连接公网的话,可以用yum localinstall mssql-server-2017/mssql-server-14.0.3370.1-18.x86_64.rpm,会自动解决依赖问题,但是如果没有公网呢。建议大家看这篇:centos7.5记录一次yum包的过程
网址:https://blog.csdn.net/guijianchouxyz/article/details/114009204
root@localhost ~]# rpm -ivh mssql-server-14.0.3370.1-18.x86_64.rpm gdb-7.6.1-120.el7.x86_64.rpm bzip2-1.0.6-13.el7.x86_64.rpm libsss_nss_idmap-1.16.5-10.el7_9.7.x86_64.rpm cyrus-sasl-2.1.26-23.el7.x86_64.rpm cyrus-sasl-gssapi-2.1.26-23.el7.x86_64.rpm
警告:mssql-server-14.0.3370.1-18.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID be1229cf: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:cyrus-sasl-gssapi-2.1.26-23.el7 ################################# [ 17%]
2:cyrus-sasl-2.1.26-23.el7 ################################# [ 33%]
3:libsss_nss_idmap-1.16.5-10.el7_9.################################# [ 50%]
4:bzip2-1.0.6-13.el7 ################################# [ 67%]
5:gdb-7.6.1-120.el7 ################################# [ 83%]
6:mssql-server-14.0.3370.1-18 ################################# [100%]
+--------------------------------------------------------------+
请运行 "sudo /opt/mssql/bin/mssql-conf setup"
完成 Microsoft SQL Server 的设置
+--------------------------------------------------------------+
需重启 SQL Server 才能应用此设置。请运行
"systemctl restart mssql-server.service"。
[root@localhost ~]# systemctl restart mssql-server.service #按照上面的提示操作
[root@localhost ~]# sudo /opt/mssql/bin/mssql-conf setup #进行sql server的配置,如下图
这个报错如果你的内存在2G以上就不会出现,sql_sql 对内存有要求
Connecting to 192.168.100.132:22... #通过内网连接,不在物理机上面操作了,看的不得劲
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Wed Feb 24 10:37:55 2021
[root@localhost ~]# 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
选择 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...
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.
安装程序已成功完成。SQL Server 正在启动。
[root@localhost ~]# systemctl status mssql-server
● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2021-02-24 10:41:32 CST; 4min 34s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 10578 (sqlservr)
CGroup: /system.slice/mssql-server.service
├─10578 /opt/mssql/bin/sqlservr
└─10598 /opt/mssql/bin/sqlservr
2月 24 10:41:38 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:38.84 spid14s 2021-02-24 10:41:38.84 spid14s 0 transact...uired.
2月 24 10:41:38 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:38.85 spid14s 2021-02-24 10:41:38.85 spid14s Recovery i...uired.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.05 spid14s 2021-02-24 10:41:39.05 spid14s Polybase f...abled.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.05 spid14s 2021-02-24 10:41:39.06 spid14s Clearing t...abase.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.65 spid14s 2021-02-24 10:41:39.65 spid14s Starting u...mpdb'.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.88 spid14s tempdb 2021-02-24 10:41:39.88 spid14s The...le(s).
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.89 spid22s Service Broker 2021-02-24 10:41:39.89 spid22s...state.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.90 spid22s Database Mirroring 2021-02-24 10:41:39.90 spi...state.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.91 spid22s 2021-02-24 10:41:39.91 spid22s Service Br...arted.
2月 24 10:41:39 localhost.localdomain sqlservr[10578]: 2021-02-24 10:41:39.92 spid7s 2021-02-24 10:41:39.92 spid7s Recovery i...uired.
Hint: Some lines were ellipsized, use -l to show in full.
root@localhost ~]# rpm -ivh mssql-tools-14.0.5.0-1.x86_64.rpm msodbcsql-13.1.6.0-1.x86_64.rpm unixODBC-2.3.1-14.el7.x86_64.rpm
警告:mssql-tools-14.0.5.0-1.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID be1229cf: NOKEY
准备中… ################################# [100%]
正在升级/安装…
1:unixODBC-2.3.1-14.el7 ################################# [ 33%]
The license terms for this product can be downloaded from
https://aka.ms/odbc131eula 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
2:msodbcsql-13.1.6.0-1 ################################# [ 67%]
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
3:mssql-tools-14.0.5.0-1 ################################# [100%]
[root@localhost ~]# rpm -ivh mssql-server-agent-14.0.3006.16-3.x86_64.rpm
警告:mssql-server-agent-14.0.3006.16-3.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID be1229cf: NOKEY
错误:依赖检测失败:
mssql-server-agent 被 (已安?) mssql-server-14.0.3370.1-18.x86_64 取代
[root@localhost ~]# echo ‘export PATH=“KaTeX parse error: Expected 'EOF', got '#' at position 66: …ot@localhost ~]#̲ source ~/.bash…PATH:/opt/mssql-tools/bin”’ >> ~/.bashrc
[root@localhost ~]# source ~/.bashrc
[root@localhost ~]# sqlcmd -S localhost -U sa #本地登录到数据库里面
Password: #输入密码
1> select name from sys.databases; #查询当前得库名
2> go
name
--------------------------------------------------------------------------------------------------------------------------------
master
tempdb
model
msdb
(4 rows affected)
1> create database ceshi; #创建一个叫ceshi得库
2> go
1> use ceshi; #进入到ceshi这个库里面
2> go
已将数据库上下文更改为 "ceshi"。
1> create table t1 (id int, cname nvarchar(50)) #在ceshi这个库里面创建一个叫t1得表
2> go
1> insert into t1 (id, cname) values (1,'abc'); #为t1表增加一条记录
2> go
(1 rows affected)
1> select * from t1; #查看t1表记录
2> go
id cname
----------- --------------------------------------------------
1 abc
(1 rows affected)
1> quit #退出
[root@localhost ~]# rpm -ivh mssql-server-agent-14.0.3006.16-3.x86_64.rpm
警告:mssql-server-agent-14.0.3006.16-3.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID be1229cf: NOKEY
错误:依赖检测失败:
mssql-server-agent 被 (已安裝) mssql-server-14.0.3370.1-18.x86_64 取代
[root@localhost ~]# sudo firewall-cmd --zone=public --add-port=1433/tcp --permanent
success
[root@localhost ~]# sudo firewall-cmd --reload
success
# firewall-cmd未启用不需要做此设置
[root@localhost ~]# vi /etc/selinux/config #更改selinux的配置文件
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled #将他修改为disables,意思大家懂得
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
-- INSERT --
[root@localhost ~]# init 6 #重启服务器,顺便也验证一下,自己安装得sql server服务在重启服务器后是否稳定
[root@localhost ~]# systemctl start mssql-server #开启数据库服务,当然,大家在这里也可以设置为开机自启,具体方法百度
[root@localhost ~]# systemctl status mssql-server
● mssql-server.service - Microsoft SQL Server Database Engine
Loaded: loaded (/usr/lib/systemd/system/mssql-server.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2021-02-24 23:31:07 CST; 40s ago
Docs: https://docs.microsoft.com/en-us/sql/linux
Main PID: 999 (sqlservr)
CGroup: /system.slice/mssql-server.service
├─ 999 /opt/mssql/bin/sqlservr
└─1451 /opt/mssql/bin/sqlservr
2月 24 23:31:25 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:25.01 spid14s 2021-02-24 23:31:25.01 spid14s Starting up...model'.
2月 24 23:31:25 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:25.29 spid6s 2021-02-24 23:31:25.29 spid6s Parallel re...ze [1].
2月 24 23:31:25 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:25.48 spid14s 2021-02-24 23:31:25.48 spid14s Polybase fe...sabled.
2月 24 23:31:25 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:25.48 spid14s 2021-02-24 23:31:25.48 spid14s Clearing te...tabase.
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.06 spid14s 2021-02-24 23:31:26.06 spid14s Starting up...empdb'.
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.26 spid14s tempdb 2021-02-24 23:31:26.26 spid14s The ...ile(s).
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.26 spid22s Service Broker 2021-02-24 23:31:26.27 spid22s ... state.
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.27 spid22s Database Mirroring 2021-02-24 23:31:26.27 spid... state.
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.38 spid22s 2021-02-24 23:31:26.38 spid22s Service Bro...tarted.
2月 24 23:31:26 localhost.localdomain sqlservr[999]: 2021-02-24 23:31:26.39 spid6s 2021-02-24 23:31:26.39 spid6s Recovery is...quired.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#
到此,SQL Server已在CentOS7上正常运行