dbcli / mssql-cli

A command-line client for SQL Server with auto-completion and syntax highlighting
BSD 3-Clause "New" or "Revised" License
1.35k stars 192 forks source link

Problems connecting to SQL Server 2014 #526

Open TreasureJim opened 2 years ago

TreasureJim commented 2 years ago

System Information Connecting OS: Ubuntu 20.04 Server OS: Microsoft Windows Server 2012 SQL Server: Microsoft SQL Server 2014

Problem Can't connect to my SQL Server after installing mssql-cli through pip. There doesn't seem to be an apt repository for Ubuntu 20.04 so couldn't install via that method like adviced in documentation. Running mssql-cli -S 192.168.100.201 -U <user> -P <user> gives an error of: Error message: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught) . Running it with --enable-sqltoolsservice-logging gives an output of:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.SqlTools.Utility.Logger.Close() in D:\a\1\s\src\Microsoft.SqlTools.Hosting\Utility\Logger.cs:line 79
   at Microsoft.SqlTools.ServiceLayer.Program.Main(String[] args) in D:\a\1\s\src\Microsoft.SqlTools.ServiceLayer\Program.cs:line 27
Traceback (most recent call last):
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/contracts/request.py", line 51, in get_response
    response = self.json_rpc_client.get_response(self.request_id, self.owner_uri)
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/jsonrpcclient.py", line 84, in get_response
    raise self.exception_queue.get()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/jsonrpcclient.py", line 124, in _listen_for_response
    response = self.reader.read_response()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/jsonrpcclient.py", line 272, in read_response
    while (not self.needs_more_data or self.read_next_chunk()):
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/jsonrpcclient.py", line 326, in read_next_chunk
    raise EOFError(u'End of stream reached, no output.')
EOFError: End of stream reached, no output.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/main.py", line 122, in <module>
    main()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/main.py", line 115, in main
    run_cli_with(mssqlcli_options)
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/main.py", line 52, in run_cli_with
    mssqlcli.connect_to_database()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/mssql_cli.py", line 278, in connect_to_database
    owner_uri, error_messages = self.mssqlcliclient_main.connect_to_database()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/mssqlcliclient.py", line 91, in connect_to_database
    owner_uri, error_messages = self._execute_connection_request_with(connection_params)
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/mssqlcliclient.py", line 180, in _execute_connection_request_with
    response = connection_request.get_response()
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/contracts/request.py", line 67, in get_response
    return self.response_error(error)
  File "/home/judasmoses/.local/lib/python3.8/site-packages/mssqlcli/jsonrpc/contracts/connectionservice.py", line 22, in response_error
    u'ownerUri': cls.owner_uri,
AttributeError: type object 'ConnectionRequest' has no attribute 'owner_uri'
chrisg123 commented 1 year ago

I faced this same issue. I beleive this is caused by OpenSSL 3.0 having CipherString = DEFAULT:@SECLEVEL=2 in /etc/ssl/openssl.cnf

What solved it for me is to create an alternate config /etc/ssl/openssl_tls1.cnf:

openssl_conf = default_conf

[ default_conf ]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
MinProtocol = TLSv1
CipherString = DEFAULT:@SECLEVEL=1

And then when I make the connection:

OPENSSL_CONF=/etc/ssl/openssl_tls1.cnf mssql-cli -S <server> -U <user> -P <password> -d <database>

Works on Ubuntu 22.04.1 LTS, OpenSSL 3.0.2 connecting to Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64)

Tip I found it helpful to first troubleshoot getting sqlcmd to work to rule out other connection problem. I was connecting from WSL2 and there were definitely other issues to overcome with that setup.

xinwei89 commented 1 year ago

https://stackoverflow.com/a/72137153/19946316 Maybe this helps?