首页 > 解决方案 > 仅在 Azure Pipelines 上用户“sa”登录失败

问题描述

我正在尝试在我的 azure 管道中运行 e2e 测试。但是服务无法正确启动,因为它们无法连接到 sql 并出现以下错误:

 Error: 18456, Severity: 14, State: 7
 Login failed for user 'sa'. Reason: An error occurred while evaluating the password. [CLIENT: 172.18.0.9]

令人困惑的部分是,它只在天蓝色管道上失败。在我的本地机器上它可以正常工作。我已经尝试过不同的密码和连接字符串。我在 azure 管道和我的机器上运行一个 linux 容器。

码头工人-compose.yml:

version: '3.4'

services:

  sqldata:
    image: mcr.microsoft.com/mssql/server:2017-latest

  identity-api:
    image: ${REGISTRY:-PROJECTNAME}/identity.api:${PLATFORM:-linux}-${TAG:-latest}
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - sqldata

docker-compose.override.yml

version: '3.4'

services:

  sqldata:
    environment:
      - SA_PASSWORD=Pass@word
      - MSSQL_SA_PASSWORD=Pass@word
      - ACCEPT_EULA=Y
    ports:
      - "5433:1433"

  identity-api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://0.0.0.0:80
      - ConnectionString=Server=sqldata;Database=IdentityDb;User Id=sa;Password=Pass@word

标签: sql-serverdockerdocker-composeazure-devopsazure-pipelines

解决方案


问题是当服务尝试连接并运行迁移时数据库仍在启动。我添加了一个检查以等待主数据库在服务迁移之前准备好。

while (IsServerConnected(connectionString) == false)
{
    logger.LogWarning("Database server not ready ({ConnectionString})...", connectionString);
    Thread.Sleep(5000);
}
...
private static bool IsServerConnected(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (SqlException)
        {
            return false;
        }
    }
}

推荐阅读