无法在 Docker 映像、Gitlab CI 中使用 sqlcmd 连接到 SQL Server

问题描述 投票:0回答:1

我尝试构建一个 CI 管道,其中也测试了数据库更改,但 SQL Server 存在问题。我为此定义了以下工作:

variables:
  MSSQL_SA_PASSWORD: <YourStrong@Passw0rd> 
  ACCEPT_EULA: Y

build_database:
  stage: build
  image: mcr.microsoft.com/mssql/server:2017-latest
  script:
    - cd './Database/Up'
    - /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -Q "SELECT @@VERSION"
    - for file in *.sql; do echo "sqlcmd -i $file"; /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -i $file; done;

我的 SQL 脚本位于

./Database/Up
文件夹中,但我从未真正执行它们,作业在
SELECT @@VERSION
任务中失败。

我总是得到以下输出:

$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "$MSSQL_SA_PASSWORD" -Q "SELECT @@VERSION"
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

有什么建议我做错了什么吗?根据 docs,我应该能够使用

/opt/mssql-tools18/bin/sqlcmd -S localhost -U SA -P '<YourPassword>'
连接到服务器,其中密码被指定为环境变量(就像我在 gitlab CI 中将其定义为变量一样)。

sql-server docker continuous-integration
1个回答
0
投票

实际上,我可以通过让服务器在单独的服务中运行来使其工作,如下所示:

services:
  - name: mcr.microsoft.com/mssql/server:2017-latest
    alias: mssql

variables:
  MSSQL_HOST: mssql
  MSSQL_SA_PASSWORD: <YourStrong@Passw0rd> 
  ACCEPT_EULA: Y

build_database:
  stage: build
  image: mcr.microsoft.com/mssql/server:2017-latest
  script:
    - cd './Database/Up'
    - for file in *.sql; do echo "sqlcmd -i $file"; /opt/mssql-tools/bin/sqlcmd -S mssql -U SA -P "$MSSQL_SA_PASSWORD" -i $file; done;

我的错误可能是我想连接到 localhost/127.0.0.1,但无法弄清楚正确的主机名是什么。

© www.soinside.com 2019 - 2024. All rights reserved.