-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using localhost
in database connection string results in timeout
#1125
Comments
What do you see when you
or
|
pyODBC just passes the connection string through, so this shouldn't be related to pyODBC at all... but
localhost? How are you running SQL Server on macOS? |
@v-chojas I use ssh port forwarding to a remote machine. BTW other tools (e. g. Azure Data Studio) have no issues connecting to |
Try testing with just the
|
@gordthompson I get the same results: no connection with localhost.
|
@gordthompson could you try strace'ing and see what IP it's actually trying to connect to? 0x2749 = 10057 (WSAENOTCONN) |
@v-chojas - Looks like it might have something to do with Ubuntu's trick of having separate entries in /etc/hosts for ip4
but that's just a guess. |
connect(3, {sa_family=AF_INET, sin_port=htons(1433), sin_addr=inet_addr("127.0.1.1")}, 16) If your service was listening on 127.0.0.1 then that would certainly not work. I don't know why the version of pyODBC could have anything to do with this. @svintuss could you get a strace with an older version of pyODBC (where you claim it works) and compare? |
re: "No such issue in version 4.0.34." FWIW, I get the same errors using 4.0.34:
|
@gordthompson, @v-chojas sorry, my initial statement that ver. 4.0.34 works fine is misleading. I can't reproduce a successful connection on it either. I might have messed something up while switching versions in the first place. @v-chojas sorry, strace requires Linux, I have macOS. Is there any other way to check where it is trying to connect to? |
It does look like unixODBC and the ssh tunnelling are not playing nicely together.
In any case this does not appear to be a pyodbc issue. |
That is extremely odd. unixODBC has literally zero networking-related code. @gordthompson in that case, does it try to connect to 127.0.0.1 or 127.0.1.1, and is the SQL Server listening on 0.0.0.0 (all IPs)? In the case of ssh tunnel, is it only listening on a specific destination IP like 127.0.0.1 and not 127.0.1.1 ? @svintuss netstat should show the attempts at connecting. Something like |
Not sure. I just did a vanilla install of SQL Server on Linux. (I've never used it before.) |
Yes, the default should be to listen on all IPs. |
Yes, it's listening on 0.0.0.0 |
Okay, so if I do
then trying to connect to localhost with
then trying to connect to localhost with |
@v-chojas after getting up an ssh tunnel netstat gives the following
My /etc/hosts on macOS has only
|
@svintuss - Something you might try: When I commented out the 127.0.1.1 line in my /etc/hosts file …
… I found that
then it started working. |
@svintuss - You might also try temporarily changing your
to
in case the two |
@v-chojas wrote:
It appears that this is not a unixODBC issue after all. The following test works with MariaDB ODBC on Ubuntu 22.04:
This is starting to look like an issue with the msodbcsql drivers. |
What does this short test program output?
(Written directly here; you may have to make changes to compile.) |
On the machine with the active ssh tunnel and the original /etc/hosts entries, i.e.,
gcc suggested that I change line 20 from ai = ai->next; to
so it would compile. When I run it I get
|
localhost
in database connection string results in timeout in pyodbc 4.0.35localhost
in database connection string results in timeout
The ODBC Driver for SQL Server uses the actual hostname of the current machine if you specify "localhost" because that is necessary for things like server certificate verification and Kerberos authentication. Thus if your machine is named "foo" and you specify "localhost", it will connect using "foo" instead of "localhost". If they resolve to the same IP, or different IPs of the same machine where the service is listening, there is no problem. This is the normal case. However, if localhost and the machine's hostname resolve to different IPs, and the service is listening only on one of them, then you will encounter this. |
Thanks for the explanation! |
@v-chojas thank you for this clarification. I have successfully connected using |
Off the top of my head, the only reason I can see to prefer However, I can think of two possible workarounds: (1) Get Python to do the lookup for you. Instead of hard-coding import socket
connection_string = (
"Driver=ODBC Driver 17 for SQL Server;"
f"Server={socket.gethostbyname('localhost')};"
)
print(connection_string)
# Driver=ODBC Driver 17 for SQL Server;Server=127.0.0.1; (2) Get the ssh tunnel to listen on 0.0.0.0, perhaps using something like one of the techniques described here |
Hi.
Version 4.0.35 of pyodbc can't connect to Microsoft SQL Server if
localhost
is specified in the connection string. Using 127.0.0.1 works fine. I haven't been able to check whether it won't accept any domain name or just localhost.No such issue in version 4.0.34.- that is misleading, I can't reproduce successful connection on ver. 4.0.34 either.Environment
Issue
The text was updated successfully, but these errors were encountered: