-2

I'm new to SQL databases and to Docker containers. I'm trying to follow up a simple tutorial and I have started by downloading Docker and the MSSQL database (and its command tools). After it, I have tried to run the command sqlcmd to check if everything was fine until now and this error appears in the terminal:

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 0x2AF9.
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..
Victors-MacBook-Pro:~ vict0rsan$

I followed exactly the following steps in order to reach this point:

Database configuration

If the project configuration is not altered, then hybris will run over an HSQL database. However, the project will use an SQLServer in production, so it's advisable to have a local database instance with SQLServer, just in case some database-specific issue should be tracked. To run hybris locally with SQLServer:

Download latest docker image with MS SQLServer (Project page: https://hub.docker.com/_/microsoft-mssql-server)
docker pull mcr.microsoft.com/mssql/server:2019-CU8-ubuntu-16.04
Give Docker at least 2GB of memory.

The docker image requires at least 2GB of image memory, so go to Docker desktop > Preferences > Resources and set the memory value to at least 2GB.

Run the instance
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=[[strong password]]' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-CU8-ubuntu-16.04
Install mssql tools:
brew untap microsoft mssql-preview
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install mssql-tools

(If the first command raises an error, just ignore it. Then accept all EULA prompts.)

Execute the script to create the database, the user, the login and the schema:

Save the following snippet to a text file, such as init.sql, and replace "[[TheAldoLoginPassword]]" with the actual password you want to give the database user:

/* Create login */
USE [master]
GO
CREATE LOGIN [AldoLogin] WITH PASSWORD=N'[[TheAldoLoginPassword]]', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
GRANT CONNECT SQL to AldoLogin WITH GRANT OPTION;
GO
/* Create database */
CREATE DATABASE [aldo];
GO
/* Set mappings, create schemas and set schema ownership and default schemas */ 
USE [aldo]
CREATE USER [AldoUser] FOR LOGIN [AldoLogin]
GO
ALTER ROLE [db_ddladmin] ADD MEMBER [AldoUser]
GO
CREATE SCHEMA AldoSchema AUTHORIZATION AldoUser;
GO 
ALTER USER [AldoUser] WITH DEFAULT_SCHEMA=[AldoSchema]
GO
ALTER DATABASE aldo SET READ_COMMITTED_SNAPSHOT ON
GO
ALTER DATABASE aldo SET ALLOW_SNAPSHOT_ISOLATION ON
GO

Execute the script with the following command:

sqlcmd -S localhost -U SA -i /path/to/init.sql 

(When prompted about the password, enter the one created for the docker image, not the one for the database user.)

Do you know what can be causing this? I appreciate your help, thanks in advance.

victorsan
  • 1
  • 2
  • 2
    Is the instance running? Did you put in the correct connection details...? – Larnu Jul 01 '21 at 15:20
  • 1
    Please check [Why am I getting “Cannot Connect to Server - A network-related or instance-specific error”?](https://stackoverflow.com/q/18060667/205233) for an exhaustive list of possible causes and how to verify and fix them. – Filburt Jul 01 '21 at 15:22
  • Where did you install that database in relation to the container? – Hans Kesting Jul 01 '21 at 15:39
  • Does this answer your question? [Why did a network-related or instance-specific error occur while establishing a connection to SQL Server?](https://stackoverflow.com/questions/1391503/why-did-a-network-related-or-instance-specific-error-occur-while-establishing-a) – Max Jul 01 '21 at 17:38
  • Please [Edit](https://stackoverflow.com/posts/68212836/edit) your question to include the `sqlcmd` you're using. A common issue is `-S localhost` not working as expected when you have a broken IPv6 stack. – AlwaysLearning Jul 01 '21 at 21:35
  • Have you tried using `sqlcmd -S 127.0.0.1 -U sa -i /path/to/init.sql` instead? – AlwaysLearning Jul 01 '21 at 21:39
  • @Larnu please check the updated post and let me know if I missed something. Thank you for your time. – victorsan Jul 01 '21 at 21:40
  • @AlwaysLearning why would you suggest that? Is that IP the default one? Sry for too many questions I am very new to sql servers and databases – victorsan Jul 01 '21 at 21:41
  • `127.0.0.1` and `::1` are addresses in different protocols. If `localhost` resolves to `::1` and Docker only exposes ports on `127.0.0.1` then `-S localhost` won't connect. – AlwaysLearning Jul 01 '21 at 21:44
  • @AlwaysLearning you were right, that solved my problem – victorsan Jul 02 '21 at 09:17

1 Answers1

0

I just changed localhostfor 127.0.0.1and it worked. Thanks to @AlwaysLearning for his useful answer.

victorsan
  • 1
  • 2