I host a web app on Windows Azure (Flask), which runs on Windows Server.
I have a linux development environment (some other contributors may use windows in the future), so we use pyodbc to communicate with the SQL Server. Unfortunately, I have to change my connection string every time I pull or push from the Azure deployment due to linux and windows ODBC connection differences.
Consider my connection string:
pyodbc.connect('Driver=SQL
Server;Server=tcp:mydbname.database.windows.net,1433;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
This works fine once it's deployed on the server (Windows Server), or on the windows box, but doesn't work on the linux dev box. I found some over SO questions and a guide on how to setup FreeTDS (e.g. Connecting to Microsoft SQL Server through pyODBC on Ubuntu and http://www.gazoakley.com/content/connecting-sql-azure-python-ubuntu-using-freetds-and-unixodbc, http://blog.tryolabs.com/2012/06/25/connecting-sql-server-database-python-under-ubuntu/).
However, after following those guides through you have to remove the server name and replace it with a the DSN (data source name), which is a locally configured variable. While that will work on a local linux box, that doesn't work once it's deployed to the Azure web server (windows).
pyodbc.connect('DSN=SQL Server;Driver=SQL
Server;Database=mydbname;Uid=dbuser#mydbname;Pwd=topsecret;Encrypt=yes;TrustServerCertificate=no;Connection
Timeout=30;')
You have to remove the servername otherwise it conflicts with the odbc/FreeTDS config.
With no DNS specified (first connection string in this post):
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source
name not found, and no default driver specified (0) (SQLDriverConnect)
With DNS and servername:
pyodbc.Error: ('HY000', '[HY000] [unixODBC][FreeTDS][SQL Server]Only
one between SERVER, SERVERNAME and DSN can be specified (0)
(SQLDriverConnect)')
With DNS and no server name
When deployed to the server it wont work.
We use git for version control, and I don't want to have continuous merge conflicts as developers change their connection string to match their dev enviroments and/or deploy to the Azure site.
Is there a clean fix for this. Gut tells me I'm doing something wrong, and this is very trivial.
First of all, if you have successfully connected to SQL Server both in Windows and Linux platforms, congratulations. In python, there is a package installed by python default named ‘platform’, we can use it to check the what platform python script run in. And we can set 2 connection string in advance, and select the specific string in different platform.
Here is my python code snippet:
import platform
def getOBCDString():
stsos = platform.system()
bol = False
odbcstring=''
if(stsos == "Windows"):
bol = True
odbcstring = 'windows_odbc_string'
elif(stsos == "Linux"):
bol = True
odbcstring = 'Linux_odbc_string'
#else:
# custom error handle
return (bol,odbcstring)
bol,string = getOBCDString()
if(bol):
print(string)
Related
I am trying to connect DB2 with pyodbc on linux and I get the error
"('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'IBM DB2 ODBC Driver' : file not found (0) (SQLDriverConnect)")## Heading ##
Is there some installation required or am I missing path?
# Create a connection object
cnxn = pyodbc.connect(
'Driver={IBM DB2 ODBC Driver}; '
'Hostname=nn.nn.nnn.nnn; '
'Port=50000; '
'Protocol=TCPIP; '
'Database=myDB; '
'CurrentSchema=myschema; '
'UID=user1; '
'PWD =test1;'
)
I tried to configure by following the link at https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/t0061216.html
I did a
yum install unixODBC
But I could not find the path where libdb2.so was installed for configuring
odbcinst.int as given here
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.apdv.cli.doc/doc/t0061216.html
What is the path for libdb2.so?
The driver-manager (unixODBC) does not supply a driver for the Db2 rdbms. Normally you will use a Db2-driver supplied by IBM which works with unixODBC.
If your Db2-server is remote from Linux then you need to install Db2 driver separately and configure it.
If your Db2-server is already installed locally on Linux, then it includes a local Db2-client already, and libraries are symlinked from the Db2-instance home directory sqllib/lib64 directory ( for example /home/db2inst1/sqllib/lib64 will contain the symlinks to the relevant libraries, if your Db2-instance is called "db2inst1"). In this case you don't need to install anything else.
Otherwise, there are various IBM supplied Db2-clients, so you have to choose the right one, which depends both on your Db2-server platform and your programming needs (different footprints for different drivers). There are also third party drivers, not mentioned here. See here for Db2 client types:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.5.0/com.ibm.swg.im.dbclient.install.doc/doc/c0022612.html
[ 1 ] If your remote Db2-server runs on Linux/Unix/Windows/zLinux then download your IBM driver from Passport Advantage (choose the latest driver version for your Db2-server version). Your Db2 DBA (if you have one) should also have an installable copy although it's always wise to download the latest version from Passport Advantage website as long as you have that access.
If your Db2-server runs on Z/OS or i-Series AND your company already has a Db2-connect gateway server installed and configured then you can use the same driver as [1] above. The Db2-connect gateway is a separate hostname that does the protocol conversion (if necessary) and allows easier licensing at a price.
If your remote Db2-server runs on i-Series (formerly OS/400) and your company is not using a Db2-connect gateway, ask your i Admin for the correct driver and install that on Linux. For i-Series you can either use a driver from IBM or from a third party, as long as it supports ODBC/CLI on Linux for your version and bitness.
If your Db2-server runs on Z/OS and your company does not use a Db2-connect gateway (i.e. instead you are connecting directly from Linux to Db2-for-Z/OS) then you will need to use the same driver as in [1] but additionally apply a suitable license file to your Linux Db2 driver. Your admin should know the details.
I am having issues connecting to an Azure database from a Virtual Machine running Ubuntu 16.04. This VM is in Google cloud. I get the following error
pypyodbc.Error: (u'IM002', u'[IM002] [unixODBC][Driver Manager]Data source
name
not found, and no default driver specified')
This code works fine locally on my PC in a Jupyter notebook. I have added the Google Cloud VM's IP address to the let it through the Azure firewall- so that doesn't appear to be the cause of the error. I have observed that I can replicate the original error message in Jupyter notebook if I change the driver "SQL Server Native Client" from 11.0 to 13.0
This all points to driver issues. A similar issue has been discussed here
With the problem being linked to "not defining that driver in the "/etc/odbcinst.ini" file."
My odbcinst.ini file contains the following information:
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.8.0
UsageCount=1
My odbc.ini file contains no information.
If I change my connection string in my code to "Driver={SQL Server Native Client 13.0" I get the same error. My other thought was to run with a version 11 of the driver. However there doesn't appear to be one for Ubuntu (only SUSE & RedHat Linux).I am new to this subject area- so feeling stuck. Any suggestions on a next logical step to resolve the connection issue.
Regards
Mike
I am trying to connect to the Oracle instance which is running on Windows 10 through python using cx_Oracle package from a mac machine.
Now while connecting it throw below error.
'ORA-21561: OID generation failed\n'
My Sample code:
import cx_Oracle
DSN = cx_Oracle.makedsn(host=server, port=port, service_name=database)
# Below is the DNS
# (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.9)(PORT=50244))(CONNECT_DATA=(SERVICE_NAME=devXDB)))
con = cx_Oracle.Connection(user, password, DSN)
However I am able to connect from same machine (mac) using SQL developer and PyCharm's database browser. I searched across and did not find any solution related to remote instance. The solutions suggested for seems to be working only for the local instances in which one has to edit/update etc/hosts or related file on windows 10.
Thanks in advance.
This was indeed the problem of /etc/hosts file issue.
One thing to note here even if the oracle instance is running on a remote machine you client machine's (from where you are connecting to the oracle instance) /etc/hosts file should have the entry like this.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost localhost.localdomain Amits-iMac.local
Replace 'Amits-iMac.local' to your client's hostname.
I'm using Python 3.5 + Spyder 2 (from Anaconda) on Windows 10. I have an Ubuntu 16.04 desktop machine which is running a MySQL server on a LAN addressable IP. PhpMyAdmin works fine with this remote server. However, every time I attempt to connect to this server from my Windows 10 Spyder I get the following error:
OperationalError: (1045, "Access denied for user 'root'#'10.0.0.30' (using password: YES)")
The command I'm using is:
import os
from peewee import *
from playhouse.db_url import connect
db = MySQLDatabase('test', host="10.0.0.80", port=3306, user='root', passwd='*********')
db.connect()
The IP of the machine I'm using to make the call is 10.0.0.30 shown in the Error. I originally tried it before the "test" db existed. Then I created the "test" db using PhpMyAdmin. I then tested again. Same error. I have created a table in the "test" db via PhpMyAdmin to confirm that root has the appropriate privileges (which wasn't really a question but I wanted confirm).
I have downloaded and installed what I could find via several StackOverflow questions prior to posting this question. I have also rebooted to make sure that any new drivers and such that I installed were actually up and running.
Note my issue is not the same as this one:
Peewee - Can't connect to MySQL server on host
My connection error shows that despite using the host argument and setting it to "10.0.0.80" that peewee is still trying to connect to 10.0.0.30. IP 10.0.0.30 is the Windows machine I'm running peewee from while 10.0.0.80 is the machine I'm attempting to connect to.
TIA
Is it possible connect using local Domain credentials that I use to log into my OSX System to connect to a SQLServer DB with python, pyodbc, unixodbc, and FreeTDS like I would when connecting from a Windows system?
Windows Connection String that works:
pyodbc.connect('Trusted_Connection=yes', driver='FreeTDS', TDS_Version = 7.0, server='<SERVER>', port=<PORT>, database='<DATABASE>')
When I attempt this from a Mac I get the following error:
pyodbc.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
I am attempting to avoid hard coding usernames and passwords.
The Trusted_Connection setting indicates whether to use Windows Authentication Mode for login validation or not. Given that you're using a Mac, I suspect that Windows Authentication Mode will not be possible and so it will be necessary to pass the username and password to the connection string.