MacOS下MSSQL的快速部署与数据库恢复
Posted alitrack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MacOS下MSSQL的快速部署与数据库恢复相关的知识,希望对你有一定的参考价值。
DBeaver连接测试
因为工作需要,有捡起了搁置已久的MSSQL Server,今天就尝试在Docker中实现快速部署和数据库恢复,闲话不多说,直接进入正题。
0x开始前注意事项,
Docker 引擎版本要求 1.8+
最少2GB内存 (2017-CU2以前版本要求至少3.25 GB)
必须的环境参数:
ACCEPT_EULA=Y
SA_PASSWORD=
MSSQL_PID= (default: Developer)
强SA密码: 至少8位字符,并要求包括大写字母、小写字母,数字和非数字、字母的字符
1x准备工作,
如果没有安装Docker Desktop,请先行安装
为了便于记录,本文仍然使用Notebook来做。
设置别名(为了简化工作)
alias sqlcmd docker exec -it mssql2017 \
/opt/mssql-tools/bin/sqlcmd -S localhost \
-U sa -P 'yourStrong(!)Password' $*
store sqlcmd
2x安装MSSQL2017
!docker run -e 'ACCEPT_EULA=Y' \
-e 'SA_PASSWORD=yourStrong(!)Password' \
--name mssql2017 \
-v /Users/steven/data/mssql:/root/mssql \
-p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest
将会得到一个名叫mssql2017的容器,
外部映射端口 1433
卷/root/mssql映射到host的/Users/steven/data/mssql
用户名:sa
密码:yourStrong(!)Password
3x测试
%sqlcmd -Y 200 -Q "SELECT @@VERSION AS 'SQL Server Version'; "
#返回
SQL Server Version
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2017 (RTM-CU20) (KB4541283) - 14.0.3294.2 (X64)
Mar 13 2020 14:53:45
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Linux (Ubuntu 16.04.6 LTS)
(1 rows affected)
4x恢复数据库
sql="""
USE [master]
GO
CREATE DATABASE [northwnd] ON
( FILENAME = N'/root/mssql/northwnd.MDF' )
FOR ATTACH
GO
"""
%sqlcmd -Q "{sql}"
#返回
Changed database context to 'master'.
Msg 5120, Level 16, State 101, Server e239d2f42fa4, Line 1
Unable to open the physical file "/root/mssql/northwnd.mdf".
Operating system error 87: "87(The parameter is incorrect.)".
Msg 1802, Level 16, State 7, Server e239d2f42fa4, Line 1
CREATE DATABASE failed. Some file names listed could not
be created. Check related errors.
原因是网络路径不支持,需要把数据库放到Docker容器的路径下
!docker exec -it mssql2017 mkdir /var/opt/mssql/backup
!docker exec -it mssql2017 cp /root/mssql/northwnd.mdf \
/var/opt/mssql/backup/
再次执行恢复命令,
sql="""
USE [master]
GO
CREATE DATABASE [northwnd] ON
( FILENAME = N'/var/opt/mssql/backup/northwnd.MDF' )
FOR ATTACH
GO
"""
%sqlcmd -Q "{sql}"
运气好的话,返回信息,
Changed database context to 'master'.
File activation failure. The physical file name
"/var/opt/mssql/backup/Northwind_log.ldf" may be incorrect.
New log file '/var/opt/mssql/backup/Northwind_log.ldf'
was created.
再执行下数据库检查就OK了,
sql="""
DBCC CHECKDB ('Northwind')
GO
"""
%sqlcmd -Q "{sql}"
上面是测试数据库,是SQL Server 2012版本(它可以自动逐步升级到最新版本),不过我今天运气不是很好,
Changed database context to 'master'.
Msg 1813, Level 16, State 2, Server e239d2f42fa4, Line 1
Could not open new database 'CRM'. CREATE DATABASE is aborted.
Msg 950, Level 20, State 1, Server e239d2f42fa4, Line 1
Database 'CRM' cannot be upgraded because its
non-release version (539) is not supported by this version
of SQL Server. You cannot open a database that is
incompatible with this version of sqlservr.exe.
You must re-create the database.
原来是我的mdf版本太低(SQL Server 2000),还得在Windows虚拟机里安装SQL Server 2008 Express,进行导入升级到SQL Server 2017可以识别的最低版本。
下图是服务器版本,内部数据库版本,数据库兼容级别以及数据库支持的兼容级别对照表,
最后再附一个简单检测数据库内部版本号的Python代码,
#针对MDF的,offset是0x12064
#针对bak, offset是3756,
def mssql_internal_version(file_path):
if file_path.lower().endswith(".mdf"):
offset=0x12064
elif file_path.lower().endswith(".bak"):
offset=3756
else:
return 0
with open(file_path,'rb') as f:
f.seek(offset)
y=f.read(2)
return int.from_bytes(y, "little") # 256
参考
https://hub.docker.com/_/microsoft-mssql-server
https://www.microsoft.com/zh-CN/download/details.aspx?id=1695
https://sqlserverbuilds.blogspot.com/2014/01/sql-server-internal-database-versions.html
如果有什么建议和意见,也欢迎留言,或者加我个人微信,
谢谢点亮[在看]!
以上是关于MacOS下MSSQL的快速部署与数据库恢复的主要内容,如果未能解决你的问题,请参考以下文章
MSSQL Recover Dropped Table Without Restore DB
如何使用Disk Drill 3为macOS恢复丢失的数据?