I am connecting to an Oracle DB using LDAP and the Python cx_Oracle library. I have proper sqlnet.ora, ldap.ora and tnsnames.ora files. On my Windows machine everything works fine using the 12.1 Oracle client and the following Python code:
import cx_Oracle
connection = cx_Oracle.connect(user/password#db, mode=cx_Oracle.SYSDBA)
I have installed the Oracle instant client 12.1 on my Linux machine (Debian) following the zip file installation method advised on the Oracle website (at the bottom of https://www.oracle.com/be/database/technologies/instant-client/linux-x86-64-downloads.html).
ldconfig correctly lists the oracle client libraries and their paths.
I copy the exact same sqlnet.ora, ldap.ora and tnsnames.ora files from my Windows machine to my Linux machine
in /opt/oracle/instantclient_12_1/network/admin, as advised by the cx_Oracle documentation.
Now running the above Python code on my Linux machine I have the following error:
cx_Oracle.DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
I tried as well setting the environment variable TNS_ADMIN=/opt/oracle/instantclient_12_1/network/admin and ORACLE_HOME=/opt/oracle/instantclient_12_1/ with no success neither
Could any of you help me debugging this and understand why the client config on my Linux machine does not work?
Installed the latest 19 client and everything works as expected.
Related
In my local machine
I have created a script in python that retrieves data from an Oracle database.
The connection to the DB is done using cx_Oracle:
con = cx_Oracle.connect (username, password, dbService)
When using SQL developer the connection is established using custom JDBC.
Replicate procedure on a Linux server.
I have created a python virtual environment with cx-Oracle pip installed in it.
I have Oracle Client 19.3.0 installed in the server, and the folder instantclient is in place.
When I try to execute the python script as is I get the following error.
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle
Client library:
DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
I assumed that the problem was the Oracle path which is not the one that python expected. So, I added this extra line of code pin-pointing the path where the Oracle libraries are located.
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
This leads to a different error:
cx_Oracle.DatabaseError: Error while trying to retrieve text for error
ORA-01804
Any clues?
The answer to my question was indicated by the ORA-1804 message.
According to the Oracle initialization doc: https://cx-oracle.readthedocs.io/en/latest/user_guide/initialization.html#usinginitoracleclient
Note if you set lib_dir on Linux and related platforms, you must still
have configured the system library search path to include that
directory before starting Python.
On any operating system, if you set
lib_dir to the library directory of a full database or full client
installation, you will need to have previously set the Oracle
environment, for example by setting the ORACLE_HOME environment
variable. Otherwise you will get errors like ORA-1804. You should set
this, and other Oracle environment variables, before starting Python,
as shown in Oracle
Even though defining the ORACLE_HOME should be done before starting Python (according to Oracle documentation) it is possible to do so by modifying the python script itself. So before the oracle client initialization command the following commands had to be added:
import os
# Setup Oracle paths
os.environ["ORACLE_HOME"] = '/apps/oracle/product/19.3.0'
os.environ["ORACLE_BASE"] = '/apps/oracle'
os.environ["ORACLE_SID"] = 'orcl'
os.environ["LD_LIBRARY_PATH"] = '/apps/oracle/product/19.3.0/lib'
import cx_Oracle
# Initialize Oracle client
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
The cx_Oracle initialization doc points out that on Linux init_oracle_client() doesn't really do what you think it does. You must still set the system library search path to include the Oracle libraries before the Python process starts.
Do I understand correctly that the machine with Python has both the DB installed and Instant Client??
If you do want Python to use the Instant Client libraries, then set LD_LIBRARY_PATH to its location and do not set ORACLE_HOME.
If you have a full Oracle DB installation on the machine with Python, then you can delete Instant Client. You need to set ORACLE_HOME, LD_LIBRARY_PATH and whatever else is needed before starting Python - in general run source /usr/local/bin/oraenv. This should set the system library search path to include /apps/oracle/product/19.3.0/lib . A code snippet like this (untested) one may help: export ORACLE_SID=ORCLCDB;set ORAENV_ASK=NO; source /usr/local/bin/oraenv. Make sure the Python process has read access to the ORACLE_HOME directory.
The cx_Oracle installation guide discusses all this.
I'm using VirtualBox as my VM, and on it I have SQL server, and my python script runs on local host.
My connection string looks like this engine = create_engine('mssql+pyodbc://'+username+':'+password+'#127.0.0.1:1433/'+database+'?driver=SQL+Server+Native+Client+11.0')
I'm getting "Data source name not found and default driver not specified" error.
I've tried a lot of stuff, and I can't make it work still.
Thanks
If you running Python script on System
The pythons script gets connected to localhost server of the system not Virtual Box
The problem was in database drivers. I worked on a pc with SQL Server Native Client 11.0 driver, but I didn't have it on the pc where I was deploying the script and on which the VM was on.
I try to open a connection with my MySql DB from my python script. The DB is a MySql 8 instance running on Google Cloud.
For that, I use the Cloud SQL Proxy from Google Cloud.
I correctly created my service account and everything is working perfectly on my Ubuntu laptop. The Unix Socket is created when I execute this command :
./cloud_sql_proxy -dir=/cloudsql -instances=my_connection_name -credential_file=path_to_cred_file
And I can open my connection with my python script.
The problem is when I try to do the same with my Macbook Pro under Mac OSX Catalina :
Since the "/" folder is now read only, I created the "cloudsql" folder under : /Users/myname/cloudsql and gave it all the permissions needed.
But, when I execute that script :
./cloud_sql_proxy -dir=/Users/myname/cloudsql -instances=my_connection_name -credential_file=path_to_cred_file
The proxy is running fine except it doesn't create the Unix Socket in the folder
It's then impossible to connect to the db since the socket doesn't exist.
Is there a workaround on Catalina or is it an other problem ?
I have been trying to connect Python 3.7.1 to TOAD 11, with a client version 10.2.
When I try running the code below
import cx_Oracle
connection = cx_Oracle.connect('myusername/mypassword#orcl')
cursor = connection.cursor()
querystring = "select * from TABLE_NAME"
cursor.execute(querystring)
I get the following error:
DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "C:\ORACLE\PRODUCT\10.2.0\CLIENT_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help
I have been trying to download a 64 bit client, but I don't understand why I need one since TOAD and it's current client are 32-bit. The issue is that with my company, it does not say the bits in the description whenever something is requested to be installed (due to the security in the network, I cannot download anything unless it is approved by the IT department, and it can only be downloaded through their server). I attempted to download the instant client 11g, but it seems it was installed but the 10.2 version it's still the only one I can find in my machine. I cannot see where it is located anywhere, so I cannot even set an environment variable for it, but I can see it in the "uninstall wizard", but it doesn't show it's location. I don't even know if 11g is 64 bit or 32 bit.
Has anybody fixed this problem before?
Is my code off and maybe that's why its not working?
The error message is saying you need a 64-bit Oracle Client because Python is 64-bit.
For cx_Oracle you need Oracle client libraries version 11.2 or later. You can get the libraries as part of Oracle Instant Client from http://www.oracle.com/technetwork/topics/winx64soft-089540.html On Windows you will also need the Redistributable noted on that download page.
Also see the cx_Oracle installation instructions at https://cx-oracle.readthedocs.io/en/latest/installation.html#installing-cx-oracle-on-windows
You will have to work with your IT people to make this happen.
I have configured the Oracle client and try with the below code, But error prompted from the code.
import os
os.chdir("C:\\oreclient_install_dir\\instantclient_12_2")
import cx_Oracle
ORACLE_CONNECT = "user/pass#(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521)))(CONNECT_DATA=(SID=sid)(SRVR=DEDICATED)))"
orcl = cx_Oracle.connect(ORACLE_CONNECT)
print("Connected to Oracle: " + orcl.version)
Error :
Python.exe - Entry Point Not Found
The Procedure entry point K32GetModuleBaseNameA could not be located in the dynamic link library KERNEL32.dll
I am using Server 2008 32 bit as client machine and remotely connect to Oracle Data Base 12c Enterprise Edition Release 12.2.0.1.0 - 64bit
Tried both Python 2.7 and 3.6.
Instant client version is "instantclient_12_2"
Appreciate if you can give me a feedback on this.
Thanks,
Tharindu Panagoda