azuresql-db-from-sql-server
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrate from the SQL Server image to the Azure SQL Database container
从 SQL Server 镜像迁移到 Azure SQL Database 容器
This skill converts an existing local SQL Server setup (the SQL Server image
) into the Azure SQL Database container so
local dev matches Azure SQL Database behavior. The two are not the same engine:
mcr.microsoft.com/mssql/server| SQL Server image | Azure SQL Database container | |
|---|---|---|
| Image | | |
| 2/3/4/8 | 5 |
| e.g. 'Developer Edition' | 'SQL Azure' |
| DB model | one instance, many DBs, | master for provisioning only; user DB for work; a user-database session returns |
If a project is using the SQL Server image but wants Azure-faithful local dev, stop
and switch to the container. This skill is self-contained;
for full container detail see the azuresql-db-container skill.
此 skill 将现有的本地 SQL Server 环境(SQL Server 镜像
)转换为 Azure SQL Database 容器,使本地开发的行为与 Azure SQL Database 保持一致。两者并非同一种引擎:
mcr.microsoft.com/mssql/server| SQL Server 镜像 | Azure SQL Database 容器 | |
|---|---|---|
| 镜像 | | |
| 2/3/4/8 | 5 |
| 例如 'Developer Edition' | 'SQL Azure' |
| 数据库模型 | 单个实例、多个数据库, | master 仅用于预置配置;用户数据库用于实际工作;在用户数据库会话中执行 |
如果项目正在使用 SQL Server 镜像,但希望本地开发忠实还原 Azure SQL Database 的行为,请停止使用该镜像并切换到容器。此 skill 自包含;有关容器的完整详情,请参阅 azuresql-db-container skill。
When to use
使用场景
- The project Dockerfile, compose file, or run script references
or
mcr.microsoft.com/mssql/server.mssql/server - There is an sqlcmd plus SA password setup, or someone says "SQL Server in Docker" / "a local mssql container".
- The user asks for "SQL Server locally" but actually targets Azure SQL Database.
- 项目的 Dockerfile、compose 文件或运行脚本引用了
或
mcr.microsoft.com/mssql/server。mssql/server - 存在 sqlcmd 和 SA password 配置,或者有人说“在 Docker 中运行 SQL Server”或“本地 mssql 容器”。
- 用户要求“在本地运行 SQL Server”,但实际目标是 Azure SQL Database。
Migration steps
迁移步骤
1. Detect SQL Server usage
1. 检测 SQL Server 的使用情况
Search the repo for the SQL Server image and the patterns that move with it:
bash
grep -rniE 'mcr\.microsoft\.com/mssql/server|mssql/server|ACCEPT_EULA|MSSQL_SA_PASSWORD|SA_PASSWORD|sqlcmd|Server=localhost,1433|Database=master' . 2>/dev/nullAnything that hits is a candidate for rewrite below.
搜索仓库中的 SQL Server 镜像及与其配套的模式:
bash
grep -rniE 'mcr\.microsoft\.com/mssql/server|mssql/server|ACCEPT_EULA|MSSQL_SA_PASSWORD|SA_PASSWORD|sqlcmd|Server=localhost,1433|Database=master' . 2>/dev/null所有命中项都是下面需要重写的候选项。
2. Sign in to the preview registry
2. 登录预览注册表
The image is in a private preview registry; sign in once with the shared
pull-only credentials provided when you sign up at https://aka.ms/sqldbcontainerpreview-signup (they may
be rotated during the preview):
bash
docker login sqldbpreview-dpgaeqhmgphzd4bk.azurecr.ioRegistry and tag are provisional during Private Preview.
该镜像位于私有预览注册表中;使用注册时提供的共享仅拉取凭据登录一次(预览期间这些凭据可能会轮换):
在 https://aka.ms/sqldbcontainerpreview-signup 注册时会获得共享的仅拉取凭据。
bash
docker login sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io在 Private Preview 期间,注册表和标签均为临时配置。
3. Rewrite the image and add --platform
3. 重写镜像并添加 --platform
Replace the SQL Server image with the Azure SQL Database image. The Azure image is x64
only, so on a non-x64 host add
(Docker) or (compose).
--platform linux/amd64platform: linux/amd64- Old:
mcr.microsoft.com/mssql/server:2022-latest - New:
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest
Keep , require a unique from the caller's
environment, keep the SA login, and keep port 1433. See
references/migrate-compose.md for a before/after compose.
ACCEPT_EULA=YMSSQL_SA_PASSWORD将 SQL Server 镜像替换为 Azure SQL Database 镜像。该 Azure 镜像仅支持 x64,因此在使用非 x64 主机时需添加 (Docker)或 (compose)。
--platform linux/amd64platform: linux/amd64- 旧镜像:
mcr.microsoft.com/mssql/server:2022-latest - 新镜像:
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest
保留 ,要求从调用方环境变量中获取唯一的 ,保留 SA 登录,并保留端口 1433。有关迁移前后的 compose 示例,请参阅 references/migrate-compose.md。
ACCEPT_EULA=YMSSQL_SA_PASSWORD4. Start the container and provision appdb first
4. 启动容器并优先预置 appdb
The engine does not auto-create databases on connect, and it is not
ready the instant returns. Use the canonical start recipe: it picks
a free host port, adds only on non-x64 hosts, waits for readiness
with a retry loop, and provisions inside that same loop. The
flags make a SQL error set the exit code so transient startup errors (e.g.
) are retried, not masked.
docker run--platformappdb-b -l 2Msg 913bash
: "${MSSQL_SA_PASSWORD:?Set MSSQL_SA_PASSWORD to a unique development secret}"
HOST_PORT=1433; while lsof -nP -iTCP:"$HOST_PORT" -sTCP:LISTEN >/dev/null 2>&1; do HOST_PORT=$((HOST_PORT+1)); done
PLATFORM=(); case "$(docker info -f '{{.Architecture}}' 2>/dev/null)" in x86_64|amd64) ;; *) PLATFORM=(--platform linux/amd64);; esac
docker rm -f sqldb 2>/dev/null
docker run -d --name sqldb "${PLATFORM[@]}" -e "ACCEPT_EULA=Y" -e MSSQL_SA_PASSWORD \
-p "127.0.0.1:$HOST_PORT:1433" sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest
until docker exec sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -b -l 2 -Q "IF DB_ID('"'"'appdb'"'"') IS NULL CREATE DATABASE appdb;"' \
>/dev/null 2>&1; do sleep 2; done
echo "ready on localhost,$HOST_PORT"A connection is for provisioning only; do real work on .
masterappdb该引擎在连接时不会自动创建数据库,而且 返回后也不会立即就绪。使用标准启动流程:它会选择空闲的主机端口,仅在非 x64 主机上添加 ,通过重试循环等待就绪,并在同一循环中预置 。 标志会让 SQL 错误设置退出码,从而对启动期间的瞬时错误(例如 )进行重试,而不是将其掩盖。
docker run--platformappdb-b -l 2Msg 913bash
: "${MSSQL_SA_PASSWORD:?Set MSSQL_SA_PASSWORD to a unique development secret}"
HOST_PORT=1433; while lsof -nP -iTCP:"$HOST_PORT" -sTCP:LISTEN >/dev/null 2>&1; do HOST_PORT=$((HOST_PORT+1)); done
PLATFORM=(); case "$(docker info -f '{{.Architecture}}' 2>/dev/null)" in x86_64|amd64) ;; *) PLATFORM=(--platform linux/amd64);; esac
docker rm -f sqldb 2>/dev/null
docker run -d --name sqldb "${PLATFORM[@]}" -e "ACCEPT_EULA=Y" -e MSSQL_SA_PASSWORD \
-p "127.0.0.1:$HOST_PORT:1433" sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest
until docker exec sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -b -l 2 -Q "IF DB_ID('"'"'appdb'"'"') IS NULL CREATE DATABASE appdb;"' \
>/dev/null 2>&1; do sleep 2; done
echo "ready on localhost,$HOST_PORT"masterappdb5. Re-point connection strings from master to the user DB
5. 将 connection string 从 master 重新指向用户数据库
SQL Server projects often connect straight to (or rely on a DB that SQL Server auto-creates). Azure SQL Database will not auto-create a database. Select the
target database in the connection string (, or for
sqlcmd). Avoid to switch databases. In a user-database session (the
Azure-faithful context where you develop), returns , exactly as
in Azure SQL Database in the cloud. A connection is a provisioning
session where the Azure statement filter is not enforced, so appears to work there, but is for provisioning
only, not application work. Standardize on one env var,
substituting the selected above for :
masterDatabase=appdb-d appdbUSEUSEMsg 40508masterUSEmasterSQL_CONNECTION_STRINGHOST_PORT<HOST_PORT>Server=localhost,<HOST_PORT>;Database=appdb;User Id=sa;Password=<MSSQL_SA_PASSWORD>;TrustServerCertificate=trueHouse style spells the keywords / / , which
keeps the examples consistent. and are documented SqlClient
synonyms and work too. For sqlcmd use
to trust the self-signed cert and to pick the database.
User Id=Password=Database=Uid=Pwd=-C-d appdbSQL Server 项目通常直接连接到 (或依赖由 SQL Server 自动创建的数据库)。Azure SQL Database 不会自动创建数据库。请在 connection string 中选择目标数据库(,或对 sqlcmd 使用 )。避免使用 切换数据库。在用户数据库会话(也就是开发时使用的忠实还原 Azure 行为的上下文)中, 会返回 ,与云端 Azure SQL Database 中的行为完全一致。 连接是预置配置会话,其中不强制执行 Azure 语句过滤器,因此 看起来可以正常工作,但 仅用于预置配置,不应用于应用工作。统一使用一个 环境变量,并将上面选出的 替换 :
masterDatabase=appdb-d appdbUSEUSEMsg 40508masterUSEmasterSQL_CONNECTION_STRINGHOST_PORT<HOST_PORT>Server=localhost,<HOST_PORT>;Database=appdb;User Id=sa;Password=<MSSQL_SA_PASSWORD>;TrustServerCertificate=true本 skill 的内部规范使用 / / 关键字拼写,以保持示例一致。 和 是 SqlClient 文档中列出的同义词,同样有效。对于 sqlcmd,使用 信任自签名证书,并使用 选择数据库。
User Id=Password=Database=Uid=Pwd=-C-d appdb6. Seed AFTER provisioning (no auto-init folder)
6. 在预置完成后进行种子数据初始化(无 auto-init 目录)
The image does not auto-run . That is a
Postgres/MySQL convention and is not honored here; do not rely on it. Seed by
running your script against the provisioned database:
/docker-entrypoint-initdb.d/*.sqlbash
docker exec -i sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -d appdb -i /dev/stdin' < seed.sql该镜像不会自动运行 。这是 Postgres/MySQL 的约定,此处不予支持;不要依赖它。应针对已预置的数据库运行脚本来完成种子数据初始化:
/docker-entrypoint-initdb.d/*.sqlbash
docker exec -i sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -d appdb -i /dev/stdin' < seed.sql7. Remove SQL Server-only features
7. 移除 SQL Server 专属功能
Some features exist only in the SQL Server image and must be removed or replaced.
Flag and fix every hit. Open references/sql-server-vs-azure-feature-matrix.md
when you meet a feature this list does not name.
- SQL Server Agent jobs: not available; use an external scheduler.
- FILESTREAM / FileTable: not supported; store blobs in columns or external storage.
- Full Service Broker cross-instance messaging: not supported.
- Cross-server distributed transactions (MS DTC, linked servers): not supported.
- Windows Auth / NTLM / Integrated Security: not supported; use SA / SQL auth.
- Instance-level tuning with : the procedure is absent, not blocked. Calling it returns
sp_configure("Could not find stored procedure 'sp_configure'"), which is a missing object rather than a refusal, so there is no permission to grant and no flag to unblock. Delete the inherited tuning step; it has no translation on this engine.Msg 2812 - /
BACKUP: refused in every session withRESTORE, exactly as in Azure SQL Database in the cloud. AMsg 40510is not a migration route into this engine; use SqlPackage with a bacpac or dacpac (see the azuresql-db-import skill)..bak - : refused with
ALTER DATABASE ... SET RECOVERY, which is the other refusal shape.Msg 40517itself is supported and only this option is refused, so a carried-over configuration script can be half accepted and leave the database in a state nobody intended. Drop the recovery-model step.ALTER DATABASE SET
某些功能仅存在于 SQL Server 镜像中,必须移除或替换。标记并修复每一处命中项。如果遇到此列表未涵盖的功能,请打开 references/sql-server-vs-azure-feature-matrix.md。
- SQL Server Agent jobs:不可用;请使用外部调度器。
- FILESTREAM / FileTable:不受支持;请将 blob 存储在列或外部存储中。
- 完整 Service Broker 跨实例消息传递:不受支持。
- 跨服务器分布式事务(MS DTC、linked servers):不受支持。
- Windows Auth / NTLM / Integrated Security:不受支持;请使用 SA / SQL auth。
- 使用 进行实例级调优:该过程不存在,而不是被阻止执行。调用它会返回
sp_configure("Could not find stored procedure 'sp_configure'"),这表示缺少对象而非请求被拒绝,因此既没有权限可授予,也没有可用于解除阻止的标志。删除继承的调优步骤;此引擎中不存在对应的转换方式。Msg 2812 - /
BACKUP:每个会话中都会被拒绝并返回RESTORE,与云端 Azure SQL Database 中的行为完全一致。Msg 40510文件不是迁移到此引擎的路径;请使用 SqlPackage 和 bacpac 或 dacpac(参阅 azuresql-db-import skill)。.bak - :会被拒绝并返回
ALTER DATABASE ... SET RECOVERY,这属于另一种拒绝形态。Msg 40517本身受支持,只有该选项被拒绝,因此沿用过来的配置脚本可能只执行一半,并使数据库处于无人预期的状态。删除恢复模式设置步骤。ALTER DATABASE SET
8. Verify identity
8. 验证身份
Confirm you are on the Azure SQL Database engine:
bash
docker exec -i sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -d appdb -Q "SELECT SERVERPROPERTY('"'"'EngineEdition'"'"') AS EngineEdition, SERVERPROPERTY('"'"'Edition'"'"') AS Edition;"'Expect and .
EngineEdition = 5Edition = SQL Azure确认当前使用的是 Azure SQL Database 引擎:
bash
docker exec -i sqldb /bin/sh -c 'SQLCMDPASSWORD="$MSSQL_SA_PASSWORD" exec /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -C -d appdb -Q "SELECT SERVERPROPERTY('"'"'EngineEdition'"'"') AS EngineEdition, SERVERPROPERTY('"'"'Edition'"'"') AS Edition;"'预期结果为 和 。
EngineEdition = 5Edition = SQL AzureVectors (if the project uses embeddings)
向量(如果项目使用 embeddings)
The Azure SQL Database engine has a native type and
. Insert with where n
is a LITERAL, never a bind parameter (a parameter dimension fails with
"Incorrect syntax near '@P3'"). (DiskANN) works on this image, as the
Known limitations page says. It needs and at
least 100 rows with non-null vectors ( below that). Full-scan top-k
stays exact and stays the right choice for a small table. The
skill carries the rules the index imposes.
VECTOR(n)VECTOR_DISTANCE('cosine', a, b)CAST(CAST(? AS NVARCHAR(MAX)) AS VECTOR(n))CREATE VECTOR INDEXSET QUOTED_IDENTIFIER ONMsg 42266azuresql-db-ragAzure SQL Database 引擎原生支持 类型和 。插入时使用 ,其中 n 必须是字面量,绝不能是绑定参数(作为参数传入维度会失败并提示 "Incorrect syntax near '@P3'")。(DiskANN)可在此镜像上使用,正如“已知限制”页面所述。它需要 ,并且至少有 100 行包含非 null 向量(低于此数量会报 )。对于小表,full-scan top-k 保持精确,仍是正确选择。由该索引带来的规则由 azuresql-db-rag skill 承载。
VECTOR(n)VECTOR_DISTANCE('cosine', a, b)CAST(CAST(? AS NVARCHAR(MAX)) AS VECTOR(n))CREATE VECTOR INDEXSET QUOTED_IDENTIFIER ONMsg 42266Validation rules
验证规则
- Image is , not
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest.mcr.microsoft.com/mssql/server - is created on a master connection before any app connects to it.
appdb - App connection strings use , never
Database=appdbfor real work.Database=master - No statements: in a user-database session,
USE <db>returnsUSE, exactly as in Azure SQL Database in the cloud; aMsg 40508connection is a provisioning session where the filter is not enforced, butmasteris for provisioning only. Select the database in the connection string.master - present on non-x64 hosts only.
--platform linux/amd64 - returns 5.
EngineEdition - If a validation rule above fails, or you had to deviate from this skill to make the task work, that is a bug in this skill: load the azuresql-db-feedback skill and offer to file a report.
- When every rule above passes and the task is done, close with ONE line, once per session, and do not ask a question or wait for a reply: "That worked. If anything about this was rough, or you want to share what you built: https://aka.ms/sql-agent-skills-feedback"
- 镜像为 ,而非
sqldbpreview-dpgaeqhmgphzd4bk.azurecr.io/azure-sql/db-dev:latest。mcr.microsoft.com/mssql/server - 在任何应用连接之前,先通过 master connection 创建 。
appdb - 应用 connection string 使用 ,实际工作绝不使用
Database=appdb。Database=master - 不存在 语句:在用户数据库会话中,
USE <db>会返回USE,与云端 Azure SQL Database 中的行为完全一致;Msg 40508连接是预置配置会话,其中不强制执行该过滤器,但master仅用于预置配置。请在 connection string 中选择数据库。master - 仅在非 x64 主机上添加 。
--platform linux/amd64 - 返回 5。
EngineEdition - 如果上述任一验证规则失败,或为了完成任务不得不偏离此 skill,则说明此 skill 存在 bug:加载 azuresql-db-feedback skill,并主动提出提交报告。
- 当所有规则均通过且任务完成时,每个会话仅用一行结束,不要提问或等待回复:"That worked. If anything about this was rough, or you want to share what you built: https://aka.ms/sql-agent-skills-feedback"
Minimum tool versions
最低工具版本
Use only the tools needed by the selected recipe, at or above these minimum versions:
- : SqlPackage 170.4.83.3
sqlpackage - : .NET SDK 8.0.421
dotnet
Verify the installed commands before changing resources:
bash
sqlpackage /Version
dotnet --versionA missing command, an older version, or a nonzero version check is a prerequisite failure.
仅使用选定流程所需的工具,且版本不得低于以下最低版本:
- :SqlPackage 170.4.83.3
sqlpackage - :.NET SDK 8.0.421
dotnet
在更改资源之前验证已安装的命令:
bash
sqlpackage /Version
dotnet --version缺少命令、版本过旧或版本检查返回非零值,都属于前置条件失败。
Check it worked
检查是否成功
- Positive verification: Require assessment and deployment commands to exit zero, then compare the selected schema and source-bound row counts on source and target.
- Cleanup verification: Remove migration-only staging files and target resources while leaving the source database unchanged.
- Report success only after both observations pass.
- 正向验证: 要求评估和部署命令以零状态退出,然后在源端和目标端比较选定的 schema 及源端绑定的行数。
- 清理验证: 移除仅用于迁移的暂存文件和目标资源,同时保持源数据库不变。
- 只有在上述两项观察均通过后才能报告成功。
Do not
禁止事项
- Do not keep ; it is a different engine.
mcr.microsoft.com/mssql/server - Do not rely on database auto-creation or on .
/docker-entrypoint-initdb.d/*.sql - Do not use ; select the database in the connection string.
USE appdb - Do not call a non-x64 host "supported"; just add on non-x64 hosts.
--platform linux/amd64 - Prefer /
User Id=in the .NET-style string. That is house style, not a requirement:Password=/Uid=are valid synonyms.Pwd= - Do not pass the vector dimension as a bind parameter.
- Do not keep SQL Agent, FILESTREAM, full Service Broker, cross-server distributed transactions, or Windows Auth.
- Do not go looking for a permission or a flag that makes run.
sp_configuresays the procedure is not there, so there is nothing to grant.Msg 2812 - Do not carry over a /
BACKUPstep (RESTORE) or anMsg 40510step (ALTER DATABASE ... SET RECOVERY).Msg 40517
- 不要保留 ;它是另一种引擎。
mcr.microsoft.com/mssql/server - 不要依赖数据库自动创建,也不要依赖 。
/docker-entrypoint-initdb.d/*.sql - 不要使用 ;请在 connection string 中选择数据库。
USE appdb - 不要将非 x64 主机称为“受支持”;只需在非 x64 主机上添加 。
--platform linux/amd64 - 优先在 .NET 风格的 connection string 中使用 /
User Id=。这是内部规范,而非硬性要求:Password=/Uid=也是有效的同义词。Pwd= - 不要将向量维度作为绑定参数传入。
- 不要保留 SQL Agent、FILESTREAM、完整 Service Broker、跨服务器分布式事务或 Windows Auth。
- 不要去寻找能让 运行所需的权限或标志。
sp_configure表示该过程不存在,因此没有任何权限可授予。Msg 2812 - 不要沿用 /
BACKUP步骤(RESTORE)或Msg 40510步骤(ALTER DATABASE ... SET RECOVERY)。Msg 40517
References
参考资料
- references/sql-server-vs-azure-feature-matrix.md: what carries over, what changes, what is gone. Read it when triaging SQL Server-only features found in step 7.
- references/migrate-compose.md: before/after docker-compose with a provision step. Read it when rewriting a compose file.
- references/sql-server-vs-azure-feature-matrix.md:哪些功能可以沿用、哪些会变化、哪些已不存在。在第 7 步排查 SQL Server 专属功能时阅读。
- references/migrate-compose.md:包含预置步骤的迁移前后 docker-compose 示例。在重写 compose 文件时阅读。
Staying current
保持最新
Authoritative, version-pinned references for the tools this skill uses (read the one you need):
- Docker Compose file reference: the Compose Specification for service, healthcheck, and depends_on syntax.
- SqlConnection connection string keywords: the full connection-string keyword table.
If the Microsoft Learn MCP server is configured, use or to fetch the current version of any of these on demand. It is optional; when it is unavailable, the references above are authoritative.
mcp__microsoft-learn__microsoft_docs_searchmcp__microsoft-learn__microsoft_docs_fetch本 skill 所用工具的权威、版本固定参考资料(按需阅读):
- Docker Compose 文件参考:有关 service、healthcheck 和 depends_on 语法的 Compose Specification。
- SqlConnection connection string 关键字:完整的 connection string 关键字表。
如果已配置 Microsoft Learn MCP server,请使用 或 按需获取这些内容的当前版本。这是可选操作;不可用时,上述参考资料即为权威依据。
mcp__microsoft-learn__microsoft_docs_searchmcp__microsoft-learn__microsoft_docs_fetch