PDO_ODBC and unixODBC cannot connect MSSQL with non-default port - sql-server

I have to connect two MSSQL servers on php/linux environment. So I decide to use unixODBC and PDO_ODBC.
mssql on server1: 10.10.10.5:1433
mssql on server2: 10.10.10.8:14233 (non-default port number)
I think there is a port problem of PDO_ODBC or unixODBC. I tried some code below.
This code works fine. connected successfully.
$db = new PDO('odbc:Driver=SQL Server Native Client 11.0; Server=10.10.10.5; Port=1433; Database=dbname; UID=uid; PWD=pwd');
This code didn't work. connect failed.
$db = new PDO('odbc:Driver=SQL Server Native Client 11.0; Server=10.10.10.8; Port=14233; Database=dbname; UID=uid; PWD=pwd');
Strangely, This code also works fine with wrong value. :(
$db = new PDO('odbc:Driver=SQL Server Native Client 11.0; Server=10.10.10.5; Port=14233; Database=dbname; UID=uid; PWD=pwd');
I concluded that someone ignore port setting on dsn of PDO.
I also tried to other setting using /etc/odbc.ini
[odbc-erp]
Driver=SQL Server Native Client 11.0
Description=MSSQL
Trace=Yes
Server=10.10.10.8
Port=14233
Database=dbname
And, this code didn't make connection.
$dbh = new PDO('odbc:odbc-erp', $uid, $pwd);
Error message:
DB Error: SQLSTATE[HYT00] SQLConnect: 0 [unixODBC][Microsoft][SQL Server Native Client 11.0]Login timeout expired in /home/user/public_html/test/testodbc.php on line 40
AND, this code works fine.
[odbc-mes]
Driver=SQL Server Native Client 11.0
Description=MSSQL
Trace=Yes
Server=10.10.10.5
Port=14233 // Wrong value (10.10.10.5 server port is 1433.)
Database=dbname
And, this code didn't make connection.
$dbh = new PDO('odbc:odbc-mes', $uid, $pwd);
The ports is all opened. tcpdump show the connection use always use ms-sql-s (1433).
sqlcmd is works fine.
# sqlcmd -U uid -P pwd -S 10.10.10.8,14233 -d OES
1> select ##version
2> go
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2008 R2 (SP2) - 10.50.4033.0 (X64)
Jul 9 2014 16:04:25
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(1 rows affected)
1>
I wonder what I'm missing. Or is there the other way to connect with mssql?
CentOS 6.6
PHP 5.5.19
sqlncli-11.0.1790.0
-
# rpm -qa
php-5.5.19-2.el6.remi.x86_64
php-mssql-5.5.19-2.el6.remi.x86_64
php-odbc-5.5.19-2.el6.remi.x86_64
php-pdo-5.5.19-2.el6.remi.x86_64
# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 11.0.1790.0 Linux
Copyright (c) Microsoft Corporation. All rights reserved.
# cat /etc/sysconfig/selinux
SELINUX=disabled
SELINUXTYPE=targeted

The driver doesn't support a Port keyword. Per the documentation, you need to append the port after the server name in the DSN:
Server=10.10.10.5,14233
The documentation also lists the keywords that are supported.

Related

Zabbix Agent on Windows Server Error with ODBC connection to SQL Server

Zabbix Server Version: 6.2.2 -> OS Ubunut 22 LTS
Zabbix Agent for Windows: 6.2.1.2400 -> Windows Server 2019
Template: https://www.zabbix.com/de/integrations/mssql
SQL Server: 2019 Standard (v15.0.4178.1)
My problem is that I get this error
Cannot connect to ODBC DSN: [SQL_ERROR]:[IM002][0][[unixODBC][Driver Manager]Data source name not found and no default driver specified]
I have configured following macros:
{$MSSQL.DSN} -> "ELAdminSQL"
{$MSSQL.INSTANCE} -> "ELVERWALTUNG"
{$MSSQL.PASSWORD} -> "MyPassword"
{$MSSQL.USER} -> "zbx_monitor"
I used the "ODBC Driver 18 for SQL Server x64" v2018.181.01.01

Issue connecting pyodbc to MS SQL database on Windows 10 Pro from WSL2 -> Ubuntu 20.04

I have pc with Windows 10. I installed Microsoft SQL Server 2019. Next action, on Windows 10 I installed WSL2 (Ubuntu 20.04). I try to connect from WSL2 (Ubuntu 20.04) to MS SQL on Windows 10.
I have tried everything I know and what I have found. Below are two articles on the subject that I have tried with no success.
I opened the 1433 port in Windows Firewall, and tried use him and not in all options with server and driver. I tried use five options with server and two options with driver, you can see this options in code bellow. I tried edit file /etc/hosts, where I add IP-address 172.29.176.1 (it action describe in second link)
Connect to SQL Server on Windows from WSL2 with pyodbc - Connection Timeout
Connect to SQL Server running on Windows host from a WSL 2/Ubuntu sqlcmd
If connect to the MS SQL from Windows everything works.
What could be the problem, what am I doing wrong, I will be grateful for the help?!
import pyodbc
import pandas as pd
import socket
#server1 = f'{socket.gethostname()}.local'
#server2 = 'DESKTOP-2TUPNJK.local'
#server3 = '172.29.176.1' # this is IP-address WSL2 and this IP-address specified in WSL2 (Ubuntu 20.04) -> /etc/resolv.conf
#server4 = '192.168.1.4' # this is IP-address my PC on Windows 10
server5 = '127.0.0.1' # this is IP-address from /etc/hosts -> localhost
#driver1 = '{ODBC Driver 17 for SQL Server}'
driver2 = '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1'
cnxn = pyodbc.connect('DRIVER=' + driver2 + ';SERVER='+server5+';PORT=1433;DATABASE=Test;UID=user;PWD=Password')
df = pd.read_sql_query('SELECT name FROM sys.Tables', cnxn)
print(df)
I get next error:
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
I managed to solve this issue:
First -> the server name must be the IP address of your PC (ipconfig in Windows cmd)
Second -> next step follow this instruction (I didn't set up -> ApexSQL tools)
And this is my code:
import pyodbc
import pandas as pd
cnxn = pyodbc.connect('DRIVER=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.9.so.1.1;SERVER=192.168.0.2,1433;DATABASE=Test;UID=sa;PWD=Test')
df = pd.read_sql_query('SELECT * FROM sys.Tables', cnxn)
print(df)

Connect to SQL Server database from RHEL with Windows Authentication

I have been working for two weeks in the installation of Superset (from Airbnb) for data visualization on a virtual RHEL machine and the connection with a SQL Server database. But I still cannot connect to this database because of a problem of driver I guess. I tried many things and I would like to know if you have a solution, about a driver I need, about modifications in my configuration etc...
Someone told me about jTDS driver. Maybe I need something like this but for python. If you have any idea, here is what I already did.
1) I tried to connect to the database from Superset :
SQL Alchemy URI : mssql://user:password#fr0-iacls-190.eu.company.corp:10001/dbname
ERROR : {"error": "Connection failed!\n\n
The error message returned was:\n(pyodbc.Error) ('IM002', '[IM002] [unixODBC][DriverManager]
Data source name not found, and no default driver specidied (0) (SQLDriverConnect)')"}
2) I tried almost the same with mssql+pymssql :
SQL Alchemy URI : mssql+pymssql://user:password#fr0-iacls-190.eu.company.corp:10001/dbname
ERROR:{"error":"Connection failed!\n\n
The error message returned was:\n(pymssql.OperationalError) (18456, 'DB-Lib error message 20018,
severity 14:\\nGeneral SQL Server error: Check messages from the SQL Server\\n
DB-Lib error message 20002, severity 9:\\n Adaptive Server connection failed (fr0-iacls-190.eu.company.corp:10001)\\n')"}
3) I tried to connect to the database from my terminal on virtual RHEL machine :
# tsql -S fr0-iacls-190.eu.company.corp -U user
Password:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
20^C
Here I have a timer that increase the number every second. I stopped the example after 20 seconds.
4) Finally I tried a python script like this one :
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=fr0-iacls-190.eu.company.corp;DATABASE=dbname;UID=;PWD=password')
The empty UID is a tip used in another StackOverflow post.
# python connect.py
Traceback (most recent call last):
File "connect.py", line 2, in <module>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=fr0-iacls-190.eu.company.corp;DATABASE=dbname;UID=;PWD=password')
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
To finish, I read about two files, odbc.ini and odbcinst.ini but I don't know if they are in the good directory (/etc). I am not working on root (~) but in in the parent directory of root (cd ~/..)...
Here are the two files if necessary :
odbc.ini
;
; odbc.ini
;
[ODBC Data Sources]
JDBC = Sybase JDBC Server
[JDBC]
Driver = /usr/local/lib/libtdsodbc.so
Description = Sybase JDBC Server
Trace = No
Servername = JDBC
Database = pubs2
UID = guest
[Default]
Driver = /usr/local/lib/libtdsodbc.so
odbcinst.ini
[PostgreSQL]
Description=ODBC for PostgreSQL
Driver=/usr/lib/psqlodbcw.so
Setup=/usr/lib/libodbcpsqlS.so
Driver64=/usr/lib64/psqlodbcw.so
Setup64=/usr/lib64/libodbcpsqlS.so
FileUsage=1
[MySQL]
Description=ODBC for MySQL
Driver=/usr/lib/libmyodbc5.so
Setup=/usr/lib/libodbcmyS.so
Driver64=/usr/lib64/libmyodbc5.so
Setup64=/usr/lib64/libodbcmyS.so
FileUsage=1
[MSSQLTest]
Driver = ODBC Driver 13 for SQL Server
Server = [http:]fr0-iacls-190.eu.company.corp[,10001]
#
# Note:
# Port is not a valid keyword in the ~/.odbc.ini file
# for the Microsoft ODBC driver on Linux
#
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.4.0
UsageCount=1
Many thanks for your time and your help.
A few things to try:
Correct the python connection string:
You have the aliased the MS SQL driver in /etc/odbcinst.ini to [ODBC Driver 13 for SQL Server] therefore in your python code you should be using that, rather than SQL Server:
import pyodbc
cnxn = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=fr0-iacls-190.eu.company.corp;Port=10001;Database=dbname;UID=user;PWD=password')
replacing user and password with correct credentials.
Use isql to test a connection using odbc.ini:
Generally you use the odbcinst.ini to setup driver configuration, and then odbc.ini for database instances (referencing the drivers), thus a valid entry to your odbc.ini could be:
[friendly_database_name]
Description=A description to help you remember what this connection is for
Server=fr0-iacls-190.eu.company.corp
Port=10001
Database=dbname
UID=user
PWD=password
If you have isql installed (comes as part of the unixODBC package if not), then you can test with:
$ isql -3 -v friendly_database_name
Ensure there's not a firewall blocking you:
I'm not sure why the tsql command is failing for you, it should return the 1> prompt. Ensure that you can establish a telnet connection to the database server:
$ telnet fr0-iacls-190.eu.company.corp 10001
which should give you something like:
Trying 12.34.56.78...
Connected to fr0-iacls-190.eu.company.corp.
Escape character is '^]'
and then to ctrl + ] and type quit to exit (or do ctrl + c to cancel if the telnet test fails).

Connect to SQL Server using SQLAlchemy

I'm trying to connect to a SQL Server Express database using SQLALchemy and pyodbc, but I'm continuously getting the error:
(pyodbc.Error) ('IM002', '[IM002] [unixODBC][Driver Manager]Data
source name not found, and no default driver specified (0)
(SQLDriverConnect)')
And I really don't understand if my engine url is wrong or what else.
My scenario is the following:
I'm on a Mac
I have a docker container (based on a Debian image with unixodbc and unixodbc-dev) in which my python app tries to connect to...
a virtualbox virtual machine running windows 8 with SQL express 2014...
I configured a user for the SQL express, with SQL Server authentication:
user: ar_user
password: ar_psw
...then:
I configured TCP ports as 1433 and disabled dynamic ports (SQL Server Configuration Manager > Network Configurations > Protocols).
I turned off Windows Firewall.
I used an Host-only adapter for the VM running windows8
now...
The VM is accessible from the host (my mac), since a:
ping -c 3 vm-ip
succeed!
But although I tried every possible permutation of user, password, ip, server name and port:
'mssql+pyodbc://ar_user:ar_psw#vm-ip/master'
'mssql+pyodbc://ar_user:ar_psw#vm-ip:1433/master'
'mssql+pyodbc://IE10WIN8\\SQLEXPRESS'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS:1433'
'mssql+pyodbc://ar_user:ar_psw#IE10WIN8\\SQLEXPRESS:1433/master'
...and many more!
I always get the "datasource not found error".
What should I do?
ps: the vm is pingable even in the docker container!
UPDATE (solved but not 100%):
I solved in this way:
I configured FreeTDS driver using /etc/odbcinst.ini in this way:
[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
client charset = UTF-8
and in /etc/freetds/freetds.conf:
[global]
tds version = 7.3
client charset = UTF-8
Then I created the engine using the following string:
'mssql+pyodbc://my_user:my_psw#machine_ip:1433/my_db?driver=FreeTDS'
It seems to work properly, but I get this warning:
SAWarning: Unrecognized server version info '95.12.255'. Version
specific behaviors may not function properly. If using ODBC with
FreeTDS, ensure TDS_VERSION 7.0 through 7.3, not 4.2, is configured in
the FreeTDS configuration.
I also defined the TDS version using environment variables but it doesn't fix the issue... any idea?
I wrote a tutorial here of how to do this. Essentially, you need to:
brew install unixodbc
brew install freetds --with-unixodbc
Add the freetds driver to odbcinst.ini
Add a DSN (Domain Source Name) to odbc.ini named "MY_DSN"
pip install pyodbc
e = create_engine("mssql+pyodbc://username:password#MY_DSN")
The walkthrough here does a much more thorough job of explaining this, including issues with SQL Server/FreeTDS Protocol Version Compatibility.

FreeTDS problem connecting to SQL Server on Mac (Unexpected EOF from the server)

I have setup FreeTDS to connect to SQL Server database. When I try to open TSQL in terminal on Mac (lion) I get this error:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20017 (severity 9):
Unexpected EOF from the server
OS error 36, "Operation now in progress"
Error 20002 (severity 9):
Adaptive Server connection failed
There was a problem connecting to the server
Does anybody have a clue what could be causing this?
I think I got it, it's the TDS configured version, I had 5.0, which caused the error
Try running tsql -C to check what version are you using, and then you can invoke tsql overriding the version with:
TDSVER=7.0 tsql -H hostname -p port -U domain\\\username -P password
and it worked!
To make this permenant I found I need to edit the freetds.conf file and set it globally in there
vim /usr/local/freetds/etc/freetds.conf
tds version = 8.0
running tsql -C still showed that the version was 5.0 but connecting now worked every time.
TDS version need to match the correct tds protocol to connect to your db server, see below -
http://www.freetds.org/userguide/choosingtdsprotocol.htm
Choosing a TDS protocol version
***DB SERVER | TDS VERSION ***
Microsoft SQL Server 6.x = 4.2
Sybase System 10 and above = 5.0
Sybase System SQL Anywhere = 5.0
Microsoft SQL Server 7.0 = 7.0
Microsoft SQL Server 2000 = 7.1
Microsoft SQL Server 2005 = 7.2
Microsoft SQL Server 2008 = 7.2
Also missing symlink on libtdsodbc.so in /usr/lib/odbc (for me)
sudo ln /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so libtdsodbc.so
should help
I was having the same problem. In my situation, the username for the Microsoft SQL Server database was in the form, DOMAIN\userid. However, when I typed it on the command line, I think it was interpreting the slash as an escape character. When I tried putting the slash the other direction, it still failed. Finally, I tried putting two slashes in the original direction and it worked. Thus the following worked for me:
tsql -S myhost -U DOMAIN\\userid
I encountered the same error. Then I did the following, and the error went away:
pip install pyodbc
Previously, I had used quotes for the DB server, username, and pwd in the TSQL command. This time, I removed the quotes.

Resources