I have a SQL server which is triggered inside docker container from gitlab. Following yaml file does that :
services:
- name: mcr.microsoft.com/mssql/server:2019-latest
alias: mssql
variables:
MSSQL_HOST: microsoft-mssql-server-linux
ACCEPT_EULA: Y
MSSQL_COLLATION: Latin1_General_CS_AS
SA_PASSWORD: yourStrong(!)Password
Now when i am starting this pipeline its giving error in running the sql script saying that login failed for user SA . Error is as :
$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P
"yourStrong(!)Password" -i Scripts/DBScript.sql Sqlcmd: Error:
Microsoft ODBC Driver 17 for SQL Server : Login failed for user 'SA'..
Cleaning up file based variables ERROR: Job failed: command terminated
with exit code 1
This error gets resolved once i remove the collation "MSSQL_COLLATION:Latin1_General_CS_AS" from the yaml file.
This explains that i am not able to change MSSQL Serve collation.
Note: Container spawned by Gitlab is Linux Container which is also installing docker image of "mcr.microsoft.com/mssql/server:2019-latest".
Any idea how to change Collation level at MS SQL server level.
Related
I have a simple docker-compose file which builds a SQL Server database, restores a .bak file.
Now, if I run this independently - i.e. wait for the image to build and exclude the run.sh and run it with docker exec it sql-server-db /opt/mssql-tools/bin/sqlcmd it works no problem.
However, if I run this inside the build process I get
#0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
#0 8.435 Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
#0 8.435 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.
Where is my error?
docker-compose
version: '3.9'
services:
sql-server-db:
container_name: sql-svr-db
build:
context: .
dockerfile: docker/mssql.Dockerfile
ports:
- "5433:1433"
volumes:
- /drive:/var/opt/mssql
mssql.Dockerfile
FROM 'mcr.microsoft.com/mssql/server'
ENV SA_PASSWORD=Passw0rd!
ENV ACCEPT_EULA="Y"
USER root
WORKDIR /app/
RUN mkdir -p /var/opt/mssql/backup
COPY /drive/db.bak /var/opt/mssql/backup
ENTRYPOINT [ "/app/run.sh" ]
.run.sh
#!/bin/bash
echo "waiting for sql server to warm up ..."
sleep 45
echo "restoring database... from BAK file"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA \
-P 'Passw0rd!' \
-Q 'RESTORE DATABASE MYDB FROM DISK = "/var/opt/mssql/backup/db.bak" WITH MOVE "db_DATA" TO "/var/opt/mssql/data/db.mdf", MOVE "db_Log" TO "/var/opt/mssql/data/db.ldf"'
echo "database restored"
I'm using a python-mssql image to restore a .bak file on Bitbucket. This is the yaml file that I create:
image: diegonachon/mssql_python_sqlcmd
env:
- ACCEPT_EULA = Y
- SA_PASSWORD = 5tr0ngP455
pipelines:
custom:
run:
- step:
name: restauration
runs-on:
- 'self.hosted'
size: 8x
script:
- pip install pysftp
- pip install pyodbc
- pip install psycopg2
- mkdir /bak_db/
- python dowload_bak_file.py
- python restore_file.py
In download_bak_file.py I download a .bak file that I'll restore. Then, in restore_db.py, I restore it with this code:
import os
os.system(''' sqlcmd -S localhost -U SA -Q'''
''' "Restore DATABASE data_base FROM DISK = '/bak_db/data_base.bak' \
WITH MOVE 'data_base' TO '/bak_db/data_base.mdf', \
MOVE 'data_base' TO '/bak_db/data_base.ldf' " \
-P 5tr0ngP455 ''')
But I get these errors:
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.
What I've seen, is a port problem, so I assume that I should change "localhost" with something else. Is it or I'm missing something?
Never use localhost in docker configs. In dockers, localhost is inside the docker container itself. If you want to connect to a database, you should use its service name or machine IP.
My Docker linux SQL Server is not working today at my machine.
I am not sure if this is firewall (I have off them all), or Docker settings (as I just upgraded to the latest Docker version), or a Docker SQL Server issue (but this was working fine on the same machine earlier).
Could anyone help?
I have tried using bash,
/opt/mssql-tools/bin/sqlcmd -S localhost,8010 -U SA -P Test123!
Error:
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..
The docker compose file
version: '3'
services:
mssql:
network_mode: lsvc
image: microsoft/mssql-server-linux:2017-latest
container_name: mssql
hostname: mssql
volumes:
- ./.db:/var/opt/mssql/
- /var/opt/mssql/data
- ./sqlinit.sql:/scripts/sqlinit.sql
ports:
- 8010:1433
environment:
- ACCEPT_EULA=Y
- MSSQL_SA_PASSWORD=Test123!
command:
- /bin/bash
- -c
- |
# Launch MSSQL and send to background
/opt/mssql/bin/sqlservr &
# Wait for it to be available
echo "Waiting for MS SQL to be available"
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
while [ $$is_up -ne 0 ] ; do
echo -e $$(date)
/opt/mssql-tools/bin/sqlcmd -l 30 -S mssql -h-1 -V1 -U sa -P Test123! -Q "SET NOCOUNT ON SELECT \"YAY WE ARE UP\" , ##servername"
is_up=$$?
sleep 1
done
# Run every script in /scripts
# TODO set a flag so that this is only done once on creation,
# and not every time the container runs
#for foo in /scripts/*.sql
/opt/mssql-tools/bin/sqlcmd -S mssql -U sa -P Test123! -l 30 -e -i /scripts/sqlinit.sql
#done
# So that the container doesn't shut down, sleep this thread
sleep infinity
I would suspect that the mssql instance had failed to start.
Looking at your docker file when the server fail to start it runs into in infinite loop. I would advice you to limit the number of retries on the loop so you will have an indication that the server is failing on startup.
Or better consider the use of the HEALTHCHECK option and not the loop script
see https://blog.couchbase.com/docker-health-check-keeping-containers-healthy/
To troubleshooting the problem try
docker logs <mssql-container-id>
if this doesn't provide enough information try connecting to the container as this was a simple machine.
docker exec -it <mssql-container-id> bash
Look for errors in /var/opt/mssql/log/errorlog
See https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-troubleshooting-guide?view=sql-server-ver15
i found the root cause, it is not due to docker mssql linux
but the latest Docker Desktop Community 2.2.0.0
after uninstall it then downgrade to old version, Docker Desktop Community 2.1.0.5.
it is working now, connect to mssql success.
not sure what is new in latest docker desktop, anyway...i will use old version now.
also one important point to take note, the ".db" folder (windows path where the yml file reside), must deleted before build the docker image.
I am trying to build a SQL Server container and then run some SQL scripts to add a user and make the schema by using docker build . and later docker-compose build -> docker-compose up
FROM mcr.microsoft.com/mssql/server
COPY all.sql all.sql
COPY auto-setup-db.sql auto-setup-db.sql
RUN /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql
this line^ fails with:
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..
The command '/bin/sh -c /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword -i auto-setup-db.sql' returned a non-zero code: 1
And I'm not sure what I'm doing wrong, does anyone have an idea?
EDIT: this is the relevant part of my docker-compose file:
version: '3'
services:
mssql:
image: mssql
build: ./e2e/mssql
container_name: mssql
environment:
SA_PASSWORD: "myPassword"
ACCEPT_EULA: "Y"
EDIT 2:
Running docker-compose up gives a lot of output from SQL Server ex.
2019-08-29 17:05:13.02 spid8s Starting up database 'tempdb'.
but I need to find a way to run the schema script in there so I thought docker-build would be the easiest place. I'm very new to docker
Trying adding double quotes around the file path and use the full path. Please see the example.
.
.
.
EXPOSE 1433
RUN mkdir -p /var/opt/mssql/database
WORKDIR /var/opt/mssql/database
COPY auto-setup-db.sql .
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U SA -P myPassword -i "/var/opt/mssql/database/auto-setup-db.sql" -o "/var/opt/mssql/database/auto-setup-db.log"
I've created a docker container using the following command:
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
I'd like to use SSMS to connect to this database from my host machine.
Which parameters should I use in the SSMS login dialog?
My current entries:
Server type: Database Engine
Server name: [machine-name]
Authentication: SQL Server authentication
Login: sa
Password: MyStr#ngPassw0rd
But I'm getting this error:
TITLE: Connect to Server
Cannot connect to localhost:.
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
network path not found
Seems I'm not able to 'see' the database.
Is there something wrong with my connection string or is it not possible to connect this way?
If you are using Docker CE on Windows with Linux containers, specify double-quotes around the environment variables so that they are recognized:
docker run -e "ACCEPT_EULA=Y" -e 'SA_PASSWORD=MyStr#ngPassw0rd" -e "MSSQL_PID=Express" -p 1433:1433 -d microsoft/mssql-server-linux:latest
You will then be able to connect using SSMS. With the default 1433 port in this case, you can specify . or localhost as the server name. You can also connect to the container instance from a remote host specifying only your machine name (assuming firewall rules allow incoming port 1433 traffic).
With single-quotes, the values are not recognized and the container will immediately stop. You can use docker logs <container-name> to view messages from the SQL Server error log for troubleshooting. Below is an example using an explict container name (which I recommend to facilitate tasks like this) using your original problem command:
docker run --name sql1 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MyStr#ngPassw0rd' -e 'MSSQL_PID=Express' -p 1433:1433 -d microsoft/mssql-server-linux:latest
c3de002fc22106e53c9b165c7f444905b5ef1bb1596eddf1b097cdd6d60e6c75
docker logs sql1
The SQL Server End-User License Agreement (EULA) must be accepted
before SQL Server can start. The license terms for this product can be
downloaded from http://go.microsoft.com/fwlink/?LinkId=746388.
You can accept the EULA by specifying the --accept-eula command line
option, setting the ACCEPT_EULA environment variable, or using the
mssql-conf tool.
As you can see from the logged message, the SQL instance stopped (stopping the container) because the SQL Server did not recognize the EULA was accepted due to the single quotes.