Encrypted database connection from SQLAlchemy - python

What are the steps required to configure SQLAlchemy with SSL support on RedHat Linux so I can connect to a MS SQL Server instance that requires an encrypted connection?

If you really want to use pymssql (as suggested by the tag on your question) then you'll need to build it from source in order for it to support SSL connections. The process will be similar to the one described in the answers to this question.
Alternatively, you may find it easier to use pyodbc along with Microsoft's SQL Server ODBC Driver for Linux.

Related

Can you ping SQL Server and request to know what driver it needs to connect?

We recently ran into an issue where I have to update all of my Python tools that connect to our SQL Server. As I understand it now the SQL Native Client 11.0 is being deprecated and replace with a different drive.
I do not want to have to update all my tools again in the future so I am trying to figure out if there is a programmatic way to check or request the correct driver information from the server directly.
Maybe it would be a library for Python that can do this or some API call that can be done against an SQL Server.
Is anyone aware of a method of checking for what driver a server might require so one can programmatically select the correct driver for connection to an SQL Server via some library such as pyodbc?
To be clear this is how we currently connect:
import pyodbc
conn = pyodbc.connect(driver='{SQL Server Native Client 11.0}', host='server_name',
database='dbNAME', trusted_connection='yes')

is pytds the only way to connect with TLS to Microsoft SQL Server using python?

according to pytds connect documentation we have a way to provide here path to cafile to enable TLS when connecting to Microsoft SQL server. good.
I am not python specialist, what are the other ways to enable TLS using other python database drivers ?
pyodbc, (would it be located on the odbc driver config level) ?
adodbapi...
on windows the certificates are automatically found in repository, it's not the case on unix
the answers provided here are not complete enough concerning TLS in the connection string
(the finality is to use Robotframework-Database-Library to connect to MSSQL enabling only TLS v1.2 connection)
thanks

pyodbc fails to connect to database, but IBM data studio connects with same credentials

I'm trying to connect to a database on an IBM machine, and I can connect just fine via the IBM desktop client "IBM Data Studio." However, when I try to connect with pyodbc it fails to connect. I've received a series of errors but it seems the main response is along the lines of the following
pyodbc.OperationalError: ('08001', u'[08001] [Microsoft][ODBC SQL Server Driver]
[TCP/IP Sockets]SQL Server does not exist or access denied. (17)
(SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][TCP/IP Sockets]ConnectionOpen (
Connect()). (10061); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')
My code follows:
import pyodbc
# Specifying the ODBC driver, server name, database, etc. directly
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=*****; PORT=50000;DATABASE=****;UID=***;PWD=***')
# Create a cursor from the connection
cursor = cnxn.cursor()
cnxn.close()
I am working on a Windows machine.
You cannot use an ODBC Driver for Microsoft SQL Server, to connect to an instance of IBM DB2 (nor to any DBMS other than Microsoft SQL Server).
You need an ODBC Driver for IBM DB2 for this connection, such as those from my employer.
You could also use an ODBC-to-JDBC Bridge Driver, in combination with a JDBC Driver for IBM DB2, such as JTOpen (open source, from IBM).
Just because a jdbc connection from Data-Studio is working, it does not mean that your pyodbc will connect to Db2. pyodbc does not use jdbc, instead it will use the CLI/ODBC interface to Db2 which gets implemented by a suitable driver.
To use Db2 from pyodbc on Windows, ensure you first have either a Db2-client installed on Windows, or a Db2-server installed installed on Windows.
There are many kinds of Db2-clients. Much depends on the operating-system that runs your Db2-server (Z/OS, i-Series, Linux, Unix, Windows), and what kind of activities you want to perform on the client (developing, administering/monitoring, querying, or all three).
For some target platforms there are non-IBM drivers, but I won't discuss those.
In python, you can choose to connect to a database either with a DSN (Data Source Name) (usually this involves a shorter connection string)
or without a DSN (longer connection string containing all the details).
When learning, it may be easier to get Microsoft Windows to do most of the initial work with odbcad32.
This is most easy if the Db2-client is already configured to access one or more Db2-databases . The prereq is that the driver supports CLI/ODBC.
To define a DSN (either a system-DSN or a user-DSN) use the Microsoft odbcad32 tool to point to your Db2 database and verify connectivity.
If the Db2-server runs on Z/OS or i-Series, special licensing requirements may apply depending on whether you are directly connecting to the target Db2-server or whether you are using a Db2-connect gateway.
Take a note of the exact DRIVER string inside odbcad32 for the Db2-database, including case and spaces because you will need that in your python code.
For example, that driver name might look like 'IBM DB2ODBC DRIVER - DB2COPY1' if you have a local Db2-server installed on Windows (such as the free Db2-Express-C).
Verify that the connection to the Db2-database is successful inside odbcad32. That is crucial.
When odbcad32 succeeds to connect then pyodbc will succeed to connect usually.
In your python code, your connection-string can either use the DSN or explicitly quote the DRIVER/SERVER/PORT/DATABASE/UID/PWD, along with any other required settings on the connection string.
Remember also that you don't have to use pyodbc. There are other options for python to interact with Db2-Servers. Make an informed choice and do your research.
You can also use the IBM supplied module "ibm_db" or the DBI interface module "ibm_db_api", or if you are using an object relational mapper you can use the SQLAlchemy adapter (ibm_db_sa), or you can use django framework.
Read all about that in the Db2 documentation here.
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.swg.im.dbclient.python.doc/doc/c0054366.html
I have solved the problem! The issue was that I was missing the "iSeries Access ODBC Driver". I don't think it's available online, although I could be wrong, I had to have our database guy help install it. Now I can connect through both the ibm_db and pyodbc libraries, after setting up my DSN under "odbcad32.exe." This took a while to solve but mostly because of the lack of informative documentation from IBM. Hopefully, this helps anyone in the same situation.

setting up a dsn to a pervasive DB on Debian through pyodbc

I am trying to use python to pull data from a pervasive database, and put it in to a postgresql databse.
The postgresql connection is local and I can make that connection just fine.
However, the pervasive connection requires a dsn but I can not find the correct driver to use.
Can anybody shed some light on this problem that has been a huge problem for me these last few days?
You'll need to install the Pervasive Linux client for the version of Pervasive you are using. For example, if your Pervasive server is v11, you'd need the v11 client. If you've got v10, you need the v10 client. The v11 client s available at http://www.pervasive.com/database/Home/Products/PSQLv11.aspx and is available on Linux as an RPM or TAR.
Once you've installed the client, you'll need to use the dsnadd command to add the Client DSN as documented.

Install MYSQLdb python module without MYSQL local install

I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.

Categories

Resources