Docker: launch a mssql script outside of the docker image - sql-server

I've been trying to run an MSSQL script on my docker and it always fails... is my syntax wrong or something... I've looked this post but it doesn't work
Executing SQL scripts on docker container
docker exec mssql '/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql'
this returned an error
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ************** -q </CRE.sql\": stat /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P AucMa1633485 -q </CRE.sql: no such file or directory"
I also tried something like this...
sudo docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'gitgood12345' -q </CRE.sql
The command returns me this error, which I'm not sure why....
Sqlcmd: '-q': Missing argument. Enter '-?' for help.
Please help me... the second method used to work, I think I forget a parameter or something...

The sqlcmd -q option requires an argument of the command you want to run. As your example is piping commands in on standard input, try without the -q.
docker exec mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 < /CRE.sql
Quoting everything results in the container trying to execute a binary named "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P gitgood12345 -q </CRE.sql" which doesn't exist.

Related

Docker desktop for windows - Getting Sqlcmd: 'DATABASE': Unknown Option

While running the below query on windows cmd
docker exec -it %containerName% /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P %dbPassword% -Q 'ALTER DATABASE MyTest SET READ_COMMITTED_SNAPSHOT ON;GO'
I am getting error sqlcmd: 'DATABASE': Unknown Option. Enter '-?' for help.
While when I am running /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P mypass -Q 'ALTER DATABASE MyTest SET READ_COMMITTED_SNAPSHOT ON;GO' on Docker cli it works properly.
What could be the reason that running the same query from windows cmd throws DATABASE unknown error?

How to I run a single sql command from the sqlcmd prompt?

In SQL Server, if I want to run a script from the command line, I can do this
/opt/mssql-tools/bin/sqlcmd -S $DB_HOST -U $DB_USER -P $DB_PASS -d $DB_NAME -i myscript.sql
Is it possible to run just a single command without a script and get the results? For instance, if I just wanted to run
SELECT 1
How would I do that from the command line?
I think you want the -q switch:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"
Documentation is here
Given that you want to:
run just a single command without a script and get the results
Another possible answer would be to run:
sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -Q "SELECT 1"
That is with a capital -Q.
This will run the command and then exit, allowing you to read the error code result.
The answer above uses lowercase -q, which will run the command but leave the sqlcmd prompt open and running.
Depending on what you want, the case of the -Q/-q argument matters.

SQL cmd login failed

I am trying to execute a large .sql file using sqlcmd. I can easily access the sa account through SSMS with the correct password. However, I am getting a
Login failed for user 'sa'
error when using sqlcmd.
Commands tried:
sqlcmd -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
osql -S servername\SQLEXPRESS -U sa -P REDACTED -d dbname -i C:\sample.sql
sqlcmd -S "servername\SQLEXPRESS" -U "sa" -P "REDACTED" -d "dbname" -i "C:\sample.sql"
What happens if you just login without the -d parameter and then type use dbname? I'm suspicious that the database you're trying to use can't be used by the "sa" for some reason. Is it offline?

Failed to connect to docker hosted MSSQL

I have a problem followng [this tutorial](https://hub.docker.com/r/microsoft/mssql-server-linux/
) where I try to connect to my docker hosted MSSQL via sqlcmd.
I executed the following in PowerShell from windows:
docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it \
-d microsoft/mssql-server-linux:latest /bin/bash
Note: "-it" and "/bin/bash" is added due to docker will be stopped automatically if there is no any activity detected.
I ran docker container ls -a to verify it is running:
docker container Is -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92cfc504ab70 microsoft/mssql-server-linux:latest "/bin/bash" 27 minutes ago Up 27 minutes 0.0.0.0:1433->1433/tcp mssql
I ran telnet local-ip:1433 on my host, it is working fine.
Problem lies when I do the following:
docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa \
-P yourStrong(!)Password
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..
I also tried to connect in using powershell via my host
Link:https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker
Command:
sqlcmd -S 192.168.0.110,1433 -U SA -P yourStrong(!)Password
Note: 192.168.0.110(got this from running ipconfig in host machine.)
Any help ?
I found out the problems after some trials and errors, and re-reading the documents. I should use double quotes for the arguments when I executed my command in PowerShell.
I was looking into the wrong direction. Initially I executed the command:
docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d \
microsoft/mssql-server-linux:latest
Container stopped automatically by itself every time it starts.
Then, I did some googling and found:
docker run -e 'ACCEPT_EULA=Y' --name mssql -e \
'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d \
microsoft/mssql-server-linux:latest /bin/bash
It seemed fine on the surface. It got executed successfully in PowerShell. It didn't stop automatically anymore.If I dig deeper using
docker container logs mssql
to see the log for mssql. No error given, just that I don't see a lots of info given, which led me to think that there were no problems in my command.
But the right way to run these commands is using double quotes.
Link: https://hub.docker.com/r/microsoft/mssql-server-linux/
IMPORTANT NOTE: If you are using PowerShell on Windows to run these commands use double quotes instead of single quotes.
E.g.
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=YourStrong!Passw0rd" -p 1401:1433 --name sql1 -d microsoft/mssql-server-linux:2017-latest
I am also able to login using SSMS with:
Server name: Hostip,1401
Username: sa
Password:yourpassword
Try 127.0.0.1 or 0.0.0.0 instead of localhost
For example :
docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P 'yourStrong(!)Password'
docker run command syntax is the following:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
When you execute the command:
docker run -e 'ACCEPT_EULA=Y' --name mssql -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d microsoft/mssql-server-linux:latest /bin/bash
/bin/bash in the end overrides CMD layer defined in the Dockerfile of microsoft/mssql-server-linux image.
So, just start a container without any additional command in the end:
$ docker run -e 'ACCEPT_EULA=Y' --name mssql -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -it -d microsoft/mssql-server-linux:latest
And now you are able to access a MSSQL:
$ docker exec -it mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'yourStrong(!)Password'
1>
I'm new to Docker and I also I had the same issue when I try to connect to the SQL Server container from my application(or sqlcmd app container from Microsoft) which is also running in another Docker container. It looks like each container gets its own subnet IP address, so 'localhost' would never work if you're trying to connect to the SQL from another container.
The command below will give you the full list of IP addresses in the bridge network. You can specify the IP directly in the connection string.
docker network inspect bridge
From your message, it looks the server is not configured to access remotely. Can you follow the way mentioned below to enable it?
Using SSMS (SQL Server Management studio):
In Object Explorer, right-click a server and select Properties.
Click the Connections node.
Under Remote server connections, select the Allow remote connections to this server check box.
Thanks,
Ananda Kumar J.

Can anyone tell me what the mistake is in the following osql syntax?

osql -U sa -d master -i ConnectInternal.sql -U sa -P ""
I need to login with sa account with blank password. But it returns an error:
The syntax of the command is incorrect.
According to the SQLCMD documentation on MSDN, if you have a "blank" password, just specify -P at the end of your command and nothing after that - so try:
sqlcmd -d master -i ConnectInternal.sql -U sa -P
Also: you're not specifying any server here...... you probably need to add -S yourserver to the command line, too!

Resources