使用的 SQL Server 容器镜像是 mcr.microsoft.com/mssql/server:2017-latest-ubuntu
,跑 CI 时出现下的错误,请问如何解决?
Microsoft.Data.SqlClient.SqlException : An unexpected error occurred while checking the sector size for file '/var/opt/mssql/data/BlogTestDb6_log.ldf'. Move the file to a local NTFS volume, where the sector size can be retrieved. Check the SQL Server error log for more information.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
持续集成是用 gitlab-ci runner 跑的
排查过程:
1)进入 mssql 容器检查 /var/opt/mssql/data/
路径是否存在。
# docker run -it mcr.microsoft.com/mssql/server:2017-latest-ubuntu ls /var/opt/mssql/data
SQL Server 2019 will run as non-root by default.
This container is running as user root.
To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216.
ls: cannot access '/var/opt/mssql/data': No such file or directory
默认竟然不存在这个路径。
2)进一步查看发现连 /var/opt/mssql
这个路径也不存在,根据微软的帮助文档,默认 data 路径就是 /var/opt/mssql/data
。
The filelocation.defaultdatadir and filelocation.defaultlogdir settings change the location where the new database and log files are created. By default, this location is /var/opt/mssql/data
3)为什么不在容器内为默认路径创建文件夹?微软意图何在?是失误还是“别有用心”?。。。想明白了,应该是微软的用心良苦,不给人犯错的机会——将数据文件放在容器内,必需通过 mount volume 映射到本机路径到 /var/opt/mssql/data
。
4)对于这里的持续集成场景,测试数据用完就作废,保存在容器内完全没问题,既然默认没有 /var/opt/mssql/data
目录,那就在容器启动时创建这个目录吧。
5)解决方法:在 .gitlab-ci.yml 中修改 mssql 容器启动命令,在启动时创建 /var/opt/mssql/data 目录。
- name: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
alias: dbserver-test
command: ["sh", "-c", "mkdir -p /var/opt/mssql/data && /opt/mssql/bin/sqlservr"]