question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'Login failed for user' when connecting to a docker hosted SQLServer instance

See original GitHub issue

Driver version or jar name

mssql-jdbc-7.0.0.jre8.jar

SQL Server version

(Using docker) Microsoft SQL Server 2017 (RTM-CU9) (KB4341265) - 14.0.3030.27 (X64) Jun 29 2018 18:02:47 Copyright © 2017 Microsoft Corporation Express Edition (64-bit) on Linux (Ubuntu 16.04.4 LTS)

Client operating system

Microsoft Windows Version 1803 (17134.112)

Java/JVM version

java version “1.8.0_181” Java™ SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)

Table schema

None

Problem description

I’m using liquibase, a database change migration tool, to update my databases. I tried to execute the migrations using a mssql docker image. When trying to connect the following exception occurs:

liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:5cbf9253-ffe6-4b3f-a9b7-fb7f2eaa04e6
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:132)
        at liquibase.integration.commandline.Main.doMigration(Main.java:953)
        at liquibase.integration.commandline.Main.run(Main.java:191)
        at liquibase.integration.commandline.Main.main(Main.java:129)
Caused by: liquibase.exception.DatabaseException: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:5cbf9253-ffe6-4b3f-a9b7-fb7f2eaa04e6
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:254)
        at liquibase.database.DatabaseFactory.openDatabase(DatabaseFactory.java:149)
        at liquibase.integration.commandline.CommandLineUtils.createDatabaseObject(CommandLineUtils.java:97)
        ... 3 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:5cbf9253-ffe6-4b3f-a9b7-fb7f2eaa04e6
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254)
        at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:258)
        at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:104)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4772)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3581)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:81)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3541)
        at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2395)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
        at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
        at liquibase.database.DatabaseFactory.openConnection(DatabaseFactory.java:246)
        ... 5 common frames omitted

Logging in using Sql Server Management Studio works fine as well as creating a database in the docker container using sqlcmd.exe with the same credentials.

If I execute the same batch file against a locally installed SQL Server (2016 - I don’t have a newer version installed) it is working fine. But I can’t get it to run against the SQL Server in the docker container.

I can’t figure out what’s the problem here.

Expected behavior and actual behavior

Logging in using the same credentials as with SSMS or SQLCMD should work via jdbc.

Repro code

I created a small sample that leads to the exception here: https://github.com/papauorg/mssql-jdbc-login-sample/tree/master

1.) Start the docker container (latest mssql-image) using docker-compose up 2.) Execute the liquibase batch file (that’s using the jdbc driver) RunLiquibaseAgainstDocker.bat

Could you please have a look at this? Would be great if you could tell me if i’m doing something wrong, here?

Thanks, Philipp

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
cheenamalhotracommented, Aug 8, 2018

Hi @papauorg

I tested with your repro, and turns out, JDBC Driver is able to connect successfully with 0.0.0.0 as servername while ODBC driver cannot identify 0.0.0.0 then. This is due to the way docker maps ports to host machines. It uses 0.0.0.0 to map the port unless hostname is specified.

You can make a small change in docker-compose.yml by specifying ports as below:

ports:
     - "127.0.0.1:1434:1433"

Also, specify same host in RunLiquidbaseAgainstDocker.bat file: ServerName=127.0.0.1 so that ODBC Driver can identify it. The JDBC Driver can resolve host 127.0.0.1 to localhost for connecting to this server but ODBC Driver cannot do so. Explicitly specifying host IP address makes it work for both drivers.

1reaction
cheenamalhotracommented, Aug 9, 2018

The JDBC Driver can connect to 0.0.0.0 and not localhost. The ServerName you provided in Run… script was ‘localhost’ 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Login failed for user 'sa' · Issue #301 · microsoft/mssql-docker
I use the following statement to run mssql in my container: docker run -d --name sql_server_thib -e 'ACCEPT_EULA=Y' -e MSSQL_SA_PASSWOR...
Read more >
Login fails for SA sql server linux docker - Stack Overflow
Finally I got it to work: docker run --name sqlserver --hostname sqlserver -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=1StrongPwd!!
Read more >
Unable to connect to host's sql server from docker container
Unable to connect to host's sql server from docker container. I have packaged a ASP.NET MVC 4.7.2 app to a container image.
Read more >
Microsoft SQL Server - Ubuntu based images - Docker Hub
Tags Architecture Dockerfile OsVersion CreatedTime LastUpdatedTime 2022‑latest amd64 No Dockerfile Ubuntu 20.04 05/31/2022 11/16/2022 latest amd64 No Dockerfile Ubuntu 20.04 09/21/2018 11/16/2022 2019‑latest amd64 No Dockerfile...
Read more >
YAML pipeline random connection failures to DB in mssql ...
The sqlcmd statement is running on sql server 2017 in a docker container that is ... Microsoft ODBC Driver 17 for SQL Server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found