sqlcmd works in CMD and not in bash - sql-server

I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
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 0x2AFA.
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 have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?

I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port

Related

How to fix Microsoft ODBC Driver 17 for SQL Server : SSL Provider ssl_choose_client_version:unsupported protocol

I have installed Sql Server 2019 Developer Edition and mssql-tools on my Ubuntu 20.04 minimal.
I can connect to my localhost with no issue, but when I want to remote to another sql server:
sqlcmd -S <server> -U <username> -P <password>
I face this error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol].
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
I has this issue once in Debian 10, and also search the net for solution, so after that I change my openssl.conf manually (su permission needed):
nano /etc/ssl/openssl.cnf
and add these to my file:
ess_cert_id_alg = sha1 under the [tsa_config1] heading
openssl_conf = default_conf near the top
the following at the end:
[default_conf]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1.0
CipherString = DEFAULT#SECLEVEL=1
I know that MinProtocol and CipherString are normally set to TLSv1.2 and DEFAULT#SECLEVEL=2, but as I mentioned once in my Debian 10, I edited my openssl.conf and change TLSv1.2 to TLSv1.0 and DEFAULT#SECLEVEL=2 to DEFAULT#SECLEVEL=1 and my connection fixed, but in Ubuntu 20.04 minimal not only there wasn't these lines, but also when I insert these manually again I face the same error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol].
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
my opnessl version is:
OpenSSL 1.1.1f 31 Mar 2020
I also downgrade my openssl once to 1.0 but it didn't work either!
I couldn't find anything else so I came here to ask for help, appreciate your help.
1.1.1m save my life. Install it manually.
The manual installation looks like:
wget https://www.openssl.org/source/latest.tar.gz -O openssl-1.1.1h.tar.gz
tar -zxvf openssl-1.1.1m.tar.gz
cd openssl-1.1.1m
./config
make
sudo make install
openssl version
sudo ldconfig
Try to add -C option to trust the server certificate:
sqlcmd -S <server> -U <username> -P <password> -C
For more details, please check my answer here: https://stackoverflow.com/a/73443215/970830

Docker SQL Server 2017 on linux connection issue

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.

Docker: How to run a sql script in SQL Server Dockerfile

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"

Connect host machine's SQL Server 2017 from homestead vagrant

I'm trying to connect my local/host machine SQL Server from Laravel vagrant homestead. I've been googling all day trying to connect the host machine's database installing different PHP packages but so far have no lucks. I thought I would remove PHP at this point and try to establish the connection inside homestead using sqlcmd instead.
So, I have installed sqlcmd in homestead and tried to execute the following command to connect to the host machine's database.
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
However, I received the following error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection because an error was encountered during handshakes before login. Common causes include client attempting to connect to an unsupported version of SQL Server, server too busy to accept new connections or a resource limitation (memory or maximum allowed connections) on the server..
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection due to prelogin failure.
However, If I use sqlcmd on my host machine and execute the following command, I am able to access the database.
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In my Homestead.yaml, I have set up port forwarding from:
ports:
- send: 1433
to: 1433
You say this doesn't work:
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
Yet this does:
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In the former, you are using the IP, but no instance name.
Try:
sqlcmd -S 10.0.2.2\MSSQL2017 -U sa -P 'password' -Q "sp_databases"
Also:
Ensure TCP/IP is enabled in SQL Network Configuration.
Verify SQL Server Browser is running.
Allow remote connections in SQL Server properties.
Lock SQL Network configuration to port 1433 on all adapters, and disable dynamic ports (then restart services).

MS SQL Server on Linux: set password using script

I have installed MSSQL Server 2017 on Ubuntu 16.04.
Now, I must change password.
I know that I can change password using the following commands:
sudo systemctl stop mssql-server
sudo /opt/mssql/bin/mssql-conf set-sa-password
But! I need to change password via script.
Ok. To solve this problem, I had installed "expect" and had wrote script:
#!/bin/bash
/usr/bin/expect <<EOD
spawn sudo /opt/mssql/bin/mssql-conf set-sa-password
expect "password:"
send "Pa$$wo4d!\r"
expect "password:"
send "Pa$$wo4d!\r"
interact
EOD
Double password - password+confirmation.
I have run the script:
ubuntu:~# bash 1.sh
spawn sudo /opt/mssql/bin/mssql-conf set-sa-password
Enter the SQL Server system administrator password:
Confirm the SQL Server system administrator password:
But it does not work. When I try to connect to mssql server, I got an error:
ubuntu:~# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Pa$$wo4d!
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'SA'..
OK, let's try a Windows authorization:
ubuntu:~# /opt/mssql-tools/bin/sqlcmd -S localhost -E -C
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : SSPI Provider: No
Kerberos credentials available.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot generate SSPI context.
I can't connect to MSSQL Server until I change my password!
And!
I need to change password via script only.
Can somebody help me please?
More easiest way:
sudo MSSQL_SA_PASSWORD=${Password} /opt/mssql/bin/mssql-conf set-sa-password
Solve the problem via script:
#!/usr/bin/python
import pexpect
ssh_cmd = '/opt/mssql/bin/mssql-conf set-sa-password'
timeout=30
child = pexpect.spawn(ssh_cmd, timeout=timeout)
child.expect(['password:'])
child.sendline('Pa$$wo4d!')
child.expect(['password:'])
child.sendline('Pa$$wo4d!')
child.expect(pexpect.EOF)
child.close()
if 0 != child.exitstatus:
raise Exception(stdout)

Resources