trying to connect to an azure SQL database with python / pyodbc and a raspberry pi - raspberry-pi

with following code I try to connect to an azure database :
server = 'tcp:myserver.database.windows.net'
database = 'DBFREE'
username = 'user'
password = 'password'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
but getting following error :
pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")
I used following commands to install pyodbc :
sudo apt-get install python-pyodbc
sudo apt-get install unixodbc-dev
pip3 install pyodbc
thanx in advance

As mentioned in my comment I recently walked this path and could never get the drivers from Microsoft to work on my Rasberry PIs. I speculate it has something to do with the ARM architecture. So even though Rasberry PI OS is based on Debian and Microsoft supplies the drivers, I could never get them to work.
I ended up using FreeTDS
First, install using the following commands:
sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
sudo pip install pyodbc sqlalchemy
When that is done, open the following file in a text editor:
/etc/odbcinst.ini
Add the following:
[FreeTDS]
Description=FreeTDS Driver
Driver=/usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup=/usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
Then in the python connection string use FreeTDS as the driver and add ;TDS_Version=8.0 to the end of the connection string:
server = 'servername.database.windows.net'
database = 'database'
username = 'user'
password = 'password'
driver = 'FreeTDS'
pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';TDS_Version=8.0')

Please ref the pyodbc here: Quickstart: Use Python to query a database in Azure SQL Database or Azure SQL Managed Instance
import pyodbc
server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
with pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()
You didn't set the ODBC driver version and the server don't have prefix tcp: .

Related

How to configure a ODBC connection to teiid on rhel8? GSSAPI negotiation fails

I have a default Teiid 12.2 installation on RHEL 8.
Now I'm trying to configure an ODBC connection that would be used by PHP.
This always results in an error "[unixODBC]received invalid response to GSSAPI negotiation: R"
This is my ODBC configuration
[TEIID12]
Driver = PostgreSQL
Trace = No
Description = PostgreSQL Data Source
Servername = servername
Port = 35432
Protocol = 7.4-1
UserName = someusername
Password = xxxx
Database = vdb
ReadOnly = no
ServerType = Postgres
ConnSettings =
UseServerSidePrepare=1
Debug=0
Fetch = 10000
A regular isql command also fails with the same information
isql -v TEIID12 someusername xxxx
Results in:
[08001][unixODBC]received invalid response to GSSAPI negotiation: R
[ISQL]ERROR: Could not SQLConnect
Additional information:
The same configuration used to work on a different Linux Distro (Ubuntu) but on this machine, kerberos was not configured. So I assume that this is influincing some sort of "preference".
The standalone-teiid.xml configuration has <ssl mode="disabled" /> for the odbc transport. And yet the GSS API errors occur.
In combination with the later, is it possible that the postgresql ODBC driver is requiring GSS to be used? Is there a setting in odbc.ini where this can be disabled?
On Ubuntu the driver version was 10.01, on RHEL it is 10.03
A solution that worked was to set the environment variable PGGSSENCMODE=disable in /etc/profile.

When trying to connect to redshift through python using psycopg2 module following error is bring displayed

I was getting the following error when I trying to connect to redshift via python and using the psycopg2 module.
import psycopg2
my_db = 'dbname'
my_host = 'red-shift hostname'
my_port = '5439'
my_user = 'username'
my_password = 'password'
con = psycopg2.connect(dbname=my_db,host=my_host,port=my_port,user=my_user,password=my_password)
Error:
OperationalError: could not translate host name "redshift://redshift-cluster-1.cqxnjksdfndsjsdf.us-east-2.redshift.amazonaws.com" to address: Unknown host
I faced similar issue. Seems like this is an SSL issue when trying to connect.
Use sqlalchemy-redshift to connect to your redshift cluster, it will work.
This is how the docs show to connect
import sqlalchemy as sa
db_uri = 'redshift+psycopg2://username:password#redshift-cluster-1.cqxnxldsfjjbsdc.us-east-2.redshift.amazonaws.com:5439/dbname'
eng = sa.create_engine(db_uri)

CentOS 6.7 and Windows MSSQL connection - FreeTDS and PHP

I searched for answers related to my question but not found what I'm looking for.
I have server with Linux CentOS 6.7. I need to connect to MS SQL Server 2005 on Windows Small Business Server 2003. In CentOS I installed FreeTDS and I managed to connect to SQL Server from terminal using this command:
# TDSVER=7.0 tsql -H ServerIPAdress -p 1433 -U username -P password
As far as I know this command bypass settings in freetds.conf.
Now I need to connect from PHP script. I'm using PDO extension and I tried this DSN string:
$db = new PDO("dblib:version=7.0;host=ServerIPAdress;dbname=Database;","username","password");
This leads me to first error:
Could not find driver
Ok, I understand that - my PHP 5.3.3 installation doesn't have installed PDO driver dblib.
My question: How can I install pdo_dblib driver in CentOS? Many tutorials and answers suggest to install through this command:
yum install php5-sybase
But that package doesn't exist:
No package php-sybase available.
If I check tsql configuration with command
tsql -C
I get these output:
Version: freetds v0.95
freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 5.0
iODBC: no
unixodbc: yes
As I understand I need to configure FreeTDS:
./configure --enable-msdblib --with-pdo-dblib=/usr/local
But how can I configure FreeTDS after instalation? I tried to remove current version but I can't find way to do that. Also I tried to install another version but configuration doesn't change.
Any advice are appreciated.
When I run command # odbcinst -j I can see this output:
unixODBC 2.3.0
DRIVERS ...........: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLSETPOSIROW Size.: 8
Content of my freetds.conf:
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = 7.0
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# A typical Sybase server
[egServer50]
host = symachine.domain.com
port = 5000
tds version = 7.0
# A typical Microsoft server
[MSServer]
host = 192.168.1.55
port = 1433
tds version = 7.0
odbcinst.ini:
[FreeTDS]
Description = FreeTDS v7.0
Driver = /usr/local/lib/libtdsodbc.so
UsageCount = 1
odbc.ini:
[MSSQL]
Driver = FreeTDS
Description = MSSQL Server Connection
TDS_Version = 7.0
Trace = No
Server = MSServer
Port = 1433
Database = MyDatabase
Username = DOMAIN\MyUsername
Password = MyPassword
I can make connection with isql command when I specify username and password:
# isql -v MSSQL "DOMAIN\MyUsername" MyPassword
But if I omit username and password isql generate error:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed
I suspect that my username make problems because I have to use DOMAIN name followed by backslash before username when I connect to SQL Server 2005.
UPDATE:
I succesfully made connection in PHP and now I can run queries. I added this line on top of my script: putenv("FREETDSCONF=/usr/local/etc/freetds.conf")
Why is this necessary?

Connection pooling issues with unixODBC, IBM DB2, and pyodbc

tldr; Have a working setup of Python->DB2, using unixODBC, but connection pooling is not working.
I've tried using the driver directly:
conn = pyodbc.connect('DRIVER=/opt/ibm/db2/lib/libdb2.so;Hostname=mydb2.domain.com;PORT=1234;DATABASE=database1;UID=user1;PWD=password1')
By driver name:
conn = pyodbc.connect('DRIVER=DB2;Hostname=mydb2.domain.com;PORT=1234;DATABASE=database1;UID=user1;PWD=password1')
DSN:
conn = pyodbc.connect('DSN=server1UID=user1;PWD=password1')
I've been using this Python script to test:
import sys
import timeit
#import pypyodbc as pyodbc
import pyodbc
#conn = pypyodbc.connect('DSN=server1;UID=user1;PWD=password1')
def query():
try:
conn = pyodbc.connect('DSN=server1;UID=user1;PWD=password1')
cur = conn.cursor()
cur.execute("select current timestamp from sysibm.sysdummy1")
row = cur.fetchone()
print('.', end='', flush=True)
conn.close()
except Exception as ex:
print(ex, end='', flush=True)
t = timeit.Timer(query)
count = 50
duration = t.timeit(count)
print("\n{count} # {duration}/sec".format(count=count, duration=count/duration))
(When logging is enabled, in odbcinst.ini, it shows a connection open/close for every query)
In my dev environment the above gets around ~10 query/sec
If the connection is created outside of the loop, and cached, then ~120 query/sec
If I install the ibm_db_dbi and use connect the results are ~10 query/sec, and using pconnect ~60 query/sec.
I've also tried iODBC, but I can't get it to connect at all, and on Windows, using ODBC, connection pooling appears to be working as expected.
Why unixODBC and not ibm_db_dbi?
connect doesn't pool
pconnect fails catastrophically, and never recovers, after network interruptions. It's also half the speed of a cached connection.
pyodbc seems better maintained than the IBM driver.
Setup:
Debian Jessie
Docker 1.7 container
IBM DB2 ODBC v10.5 library installed
unixODBC 2.3.4
Python 3.5
pyodbc 3.0.10
Configuration:
odbcinst.ini
[ODBC]
Pooling = Yes
Trace = No
TraceFile = /var/log/odbc.log
[DB2]
Description = IBM DB2 Adapter
Driver = /opt/ibm/db2/lib/libdb2.so
Driver64 = /opt/ibm/db2/lib/libdb2o.so
FileUsage = 1
DontDLClose = 1
CPTimeout = 1000
CPTimeToLive = 100
Threading = 2
Pooling = Yes
odbc.ini
[server1]
Description = Server 1
Driver = DB2
db2cli.ini
[server1]
Database = database1
Protocol = TCPIP
Hostname = mydb2.domain.com
ServiceName = 1234
References:
Using IBM DB2 with unixODBC (from unixodbc.org)
Setting up unixODBC with a remote DB2
Setting up the unixODBC driver manager (from IBM)
Similar question on SO that is stale, unanswered
It looks like you have Pooling = Yes in the wrong place, it needs to be under the [ODBC] heading, not the driver heading
http://www.unixodbc.org/doc/conn_pool.html

Uncaught exception 'MongoConnectionException'

Fatal error: Uncaught exception 'MongoConnectionException' with message
'Failed to connect to: localhost:27017: Permission denied' in
/var/www/html/test.php:8 Stack trace: #0 /var/www/html/test.php(8):
MongoClient->__construct() #1 {main} thrown in /var/www/html/test.php
on line 8
Hi Mongo experts...
I am a developer wanting to try out MongoDB. So installed centoOS 6.5 64bit in a test machine (Dell E520 Intel Dual Core 4GB Ram), installed PHP (Apache was already present).
Then installed MongoDB (yum install mongo-10gen mongo-10gen-server), then installed "pecl install mongo" (install ok: channel://pecl.php.net/mongo-1.4.5), added extension=mongo.so to php.ini.
To install pecl, I installed few other things like C++ compiler & php-pear. php5-dev & php5-cli was not available in yum so installed php-devel & php-cli (installed versions are php-devel-5.3.3-27.el6_5.x86_64 & php-cli-5.3.3-27.el6_5.x86_64
I turned off iptables firewall. Ran mongo --host localhost:27017 from shell & connected without problems
[root#localhost ~]# mongo --host localhost:27017
MongoDB shell version: 2.4.8
connecting to: localhost:27017/test
>
These are the contents of test.php
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
// connect
$m = new MongoClient();
//$m = new MongoClient("mongodb://localhost:27017");
// select a database
$db = $m->comedy;
// select a collection (analogous to a relational database's table)
$collection = $db->cartoons;
// add a record
$document = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($document);
// add another record, with a different "shape"
$document = array( "title" => "XKCD", "online" => true );
$collection->insert($document);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo $document["title"] . "\n";
}
?>
As you can see I have tried both
$m = new MongoClient(); & $m = new MongoClient("mongodb://localhost:27017");
But I am getting the same error. Where am I going wrong?
This problem is documented at http://ca3.php.net/manual/en/mongo.installation.php#mongo.installation.fedora
Red Hat,Fedora and CentOS:
The default Apache settings on these systems do not let requests make network connections, meaning that the driver will get "Permission denied" errors when it tries to connect to the database. If you run into this, try running:
$ /usr/sbin/setsebool -P httpd_can_network_connect 1
Then restart Apache. (This issue has also occurred with SELinux.)
Thanks for all your support!
Hope this thread helps someone from going in circles!