Whats wrong with instant client? | cx_Oracle.Database: ORA-12514 - linux

With the help of cx_Oracle package I am trying to connect to remote Oracle database. Unfortunately I have error when try to test next python code:
import cx_Oracle;
dsn_tns=cx_Oracle.makedsn('HOST', 'PORT', service_name='NAME')
connection=cx_Oracle.connect('USERNAME', 'PASSWORD', dsn_tns)
ERROR:
cx_Oracle.Database: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Dependencies:
1) Ubuntu 14.04 (64 bit) (local computer)
2) Python 2.7.14 (64 bit)
3) cx_Oracle 6.4.1
4) Instant Client 18.3
5) Oracle 11g database (located in remote server)
I used official documentation to install and configurate instant client.
Official documentation say that:
Oracle Call Interface 18.3 can connect to Oracle Database 11.2 or
later.
For thats why I load basic package of instant client 18.3, then unzip it:
cd /opt/oracle
unzip instantclient-basic-linux.x64-18.3.0.0.0dbru.zip
Then I install libaio:
sudo apt-get install libaio1
In terminal I wrote next 2 command:
sudo sh -c "echo /opt/oracle/instantclient_18_3 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
Inside /opt/oracle/instantclient_18_3/network/admin I put tnsnames.ora file.
After that I set environment variable in ~/.profile:
export LD_LIBRARY_PATH=/opt/oracle/instantclient_18_3:$LD_LIBRARY_PATH
export TNS_ADMIN=/opt/oracle/instantclient_18_3/network/admin
export PATH=/opt/oracle/instantclient_18_3:$PATH
What else should I have done to fix the problem?

What's wrong with instant client? Absolutely nothing from the information you supplied. You are connecting to the server (listener) but the server is not configured with the database you specified.
You can confirm this with a different client / different client host.
Go check the config on the server.

Related

How to install and start Oracle Database Express Edition (XE) 18c on Fedora Linux with SQL-Developer IDE

I have put together a comprehensive guide to installing and starting Oracle Database Express Edition on Fedora 33 Linux. I had to cobble this together from numerous sources, and it works. It's not just the installation, it's also getting the database to start with the listener after installation when the machine is rebooted, how to setup a non-system database user, and how to add a connection. I hope this guide is useful to whoever comes across it. There are precious few working guides for Fedora 33 Oracle DB XE 18c and SQLDeveloper setup. Here it is:
INSTALLATION INSTRUCTIONS:
First install Fedora 33 Workstation Edition from a bootable key (to dual-boot, just leave an empty partition you want the OS to reside (recommend shrink space from Disk Management in Windows 10) and select"Automatic Partitioning" in the Fedora installer.
Install Oracle Database 18c Express Edition:
sudo dnf -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/compat-libcap1-1.10-7.el7.x86_64.rpm
sudo dnf -y install http://mirror.centos.org/centos/7/os/x86_64/Packages/compat-libstdc++-33-3.2.3-72.el7.x86_64.rpm
dnf -y install libnsl
curl -o oracle-database-preinstall-18c-1.0-1.el7.x86_64.rpm -L https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-18c-1.0-1.el7.x86_64.rpm
dnf -y install oracle-database-preinstall-18c-1.0-1.el7.x86_64.rpm
curl -o oracle-database-xe-18c-1.0-1.x86_64.rpm -L https://download.oracle.com/otn-pub/otn_software/db-express/oracle-database-xe-18c-1.0-1.x86_64.rpm
sudo dnf -y install oracle-database-xe-18c-1.0-1.x86_64.rpm
Configure the database by logging in as root then running:
sudo su
/etc/init.d/oracle-xe-18c
At the prompt, specify the same password for the SYS, SYSTEM, and PDBADMIN accounts. Include no special characters and do not place a number at the beginning of the password.
Install Java JDK11:
sudo dnf -y install java-11-openjdk-devel java-11-openjdk-jmods java-11-openjdk-src
Add the following lines to the bottom of the home user's .bashrc:
# User specific aliases and functions
export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which javac)))))
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=/opt/oracle/product/18c/dbhomeXE
export ORACLE_SID=XE
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
and use
source .bashrc
Login as oracle user using:
sudo su oracle
Add the following to the end of oracle user's .bashrc:
# User specific aliases and functions
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=/opt/oracle/product/18c/dbhomeXE
export ORACLE_SID=XE
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
and type
source ~/.bashrc
Exit and go to the following site and download sql-developer:
https://www.oracle.com/tools/downloads/sqldev-downloads.html
Install the .rpm and run the setup script:
sudo dnf -y install sqldeveloper-20.4.1.407.0006-20.4.1-407.0006.noarch.rpm
cd /opt/sqldeveloper/
./sqldeveloper.sh
For HiDPI displays, append to /opt/oracle-sqldeveloper/sqldeveloper/bin/sqldeveloper.conf the following:
AddVMOption -Dsun.java2d.uiScale=2
How to add a non-system database user (so you don't have to see system tables):
In a terminal type:
sqlplus sys as sysdba
Enter the lines:
SQL> alter session set "_ORACLE_SCRIPT"=true;
Create a local user called whatever you want. Type the password for the user in
the following line after "identified by"
SQL> create user usernamehere identified by passwordhere;
Grant your user admin privileges:
SQL> grant all privileges to usernamehere;
Then exit the command prompt:
SQL> exit;
Now you can connect to your local database in the Oracle SQL Developer IDE. Search for sqldeveloper in the system shell menu. It should start up fine, now you want to add a connection to your local machine database. On the top left side of the IDE window, you should see a label called "Connections" with a green plus sign. Click on the plus sign and you will get a connection prompt. Under "Name" enter a suitable name for your local database. I called mine "LocalDatabase". Avoid using spaces or special characters besides the underscore when naming your database. For "Username" and "Password" enter the username and password you created for your non-system account in the SQL Plus command prompt. Check the "Save Password" box. Keep the Hostname (localhost), Port (1521), and SID (xe) the default values to connect to your local database. At the bottom of the Connection window, click "Test" and it should say Status: Success. Then click the "Save" button and you should see your connection added among those in the Connections window. Click "Connect" and the window will close and you will be successfully connected to the database on your local machine. Congratulations, now you can use Oracle SQL Plus on your own computer!
Restart your machine and follow the instructions at the below for reliably reconnecting to your database.
OPERATION INSTRUCTIONS (AFTER INSTALLATION):
If you have done installation steps already:
First you want to start the database service. From your home user account in a terminal, type
sqlplus sys as sysdba
then enter your database password you assigned on installation.
At the SQL prompt, enter
SQL> startup
Wait for the database to start,
then exit with
SQL> exit
then login as oracle user:
sudo su oracle
And run:
lsnrctl start
Then for the status:
lsnrctl status
Exit to home user then open sqldeveloper and connect successfully to your locally installed database!
That's it, I hope someone finds this useful! :)
You cant create a user on the main db as you have it, you have to go into the xepdba1 in order to create a user / schema and then connect there to create tables and insert rows.
I had to install it twice because I had my ENV variables configured for previous software installed caused a mess.
Removed it and installed again, with correct ORACLE_HOME defined.

How to fix: cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library - Python

I am establishing a connection to oracle 11g which is in a remote server using cx_oracle 7 with python 3.6.7. my OS in Ubuntu 18.04
I have installed oracle instant client library with libclntsh.so but I am not getting the expected output.
here is the code which i am using to connect to the oracle db
connection = cx_Oracle.connect("username/password#host/port")
print (connection.version)
connection.close()
when the script runs i expect to get the connection version instead i am getting the following error message
File "script.py", line 13, in
connection = cx_Oracle.connect("username/password#host/port") cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle
Client library: "libclntsh.so: cannot open shared object file: No such
file or directory". See
https://oracle.github.io/odpi/doc/installation.html#linux for help
After some more research i got the solution from Ubuntu community , after you have installed oracle instant-client you will have to integrate oracle libraries as follows:
export LD_LIBRARY_PATH=/usr/lib/oracle/<version>/client(64)/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
An example for 12.1 version for Linux x86_64 can be:
export
LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
where <version> indicates the version of your of your oracle instant-client e.g 11.2, 12.2
The connection parameter should be as follows
connection = cx_Oracle.connect("username/password#host/service_name e.g orcl")
to get the listener/service_name type the following in the oracle sqlplus
SQL> show parameter local_listener
literal under VALUE is your listener/service_name.
I was facing the exact same problem. This is what worked for me:
First, I downloaded the Oracle Basic zip file. In my case, I got the 64-bit version.
After that, I unzipped it in an opt directory. I had to use sudo in my system
$ sudo mkdir -p /opt/oracle
$ cd /opt/oracle
$ sudo unzip /opt/oracle/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
Then I installed libaio1. Note that I am working with Ubuntu
$ sudo apt-get install libaio1
Finally, I added the path to the external variable LD_LIBRARY_PATH
$ vim ~/.bashrc
And added this line to the .bashrc file
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_8:$LD_LIBRARY_PATH
After saving the .bashrc file, I sourced it:
$ source ~/.bashrc
Then my Python script worked nicely again.
See also the cx_oracle documentation
For Ubuntu Linux 20.04 LTS server what worked for me (which may be obvious but wasn't to me!) is 1) when you perform the export, you need to be in the folder you intend to run the app/ command connecting to Oracle from, and although this worked, after closing the SSH terminal to the EC2 server, was then not available again which was resolved by 2) Add it to ~/.bashrc
Steps in full:
With the Oracle instant client unzipped in for example: /opt/oracle/instantclient_19_9
sudo apt-get install libaio1
cd ~/your-project-folder
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9
I then added to ~/.bashrc with:
sudo nano ~/.bashrc
And add this line:
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9
And in the terminal run:
source ~/.bashrc
Mine worked as expected installed on an EC2 server under 'ubuntu' user with requisite nvm/ nodeJs installed
In nodeJs an example connection might look something like:
const testOracleConnection = async () => {
let conn;
try {
conn = await oracledb.getConnection(oracleConfig);
const query1 = 'select ID, anotherColumn from someTable where ID = 1111';
const result = await conn.execute(query1);
console.log(result);
} catch (err) {
console.error(err);
} finally {
if (conn) {
try {
await conn.close();
} catch (err) {
console.error(err);
}
}
}
};
I have almost given up on this error. I tried potentially all the solution on the internet and nothing worked.
I was writing a similar script in Unix where I was getting the same error. I have applied the same fix that I used in shell script and it worked like charm.
The Python script worked fine accessing DB via cx_Oracle if I execute it directly. But when I schedule it through crontab I was constantly getting the error "cx_Oracle.DatabaseError: DPI-1047"
Here is the fix. Import the OS module and add the below code.
os.environ["ORACLE_HOME"] = "Your oracle lib ". In my case it is /u01/oracle/product/12.1.0.2/
If you're working with aws lambdas to connect to your RDS/OracleDB, then try this approach using Docker to automated the build for the aws lambda layer - https://medium.com/#sabithvm/building-up-on-lambda-layers-a4771d3b9c7

How to use cx_Oracle within AWS SageMaker (Jupyter Notebook)

I've followed the official installation guide but haven't had any luck so far. I wonder if cx_Oracle can work on AWS SageMaker's virtual environment. The steps I've used so far are:
Create a /opt/oracle directory and unzip the basic instantclient in it.
sudo yum install libaio
sudo sh -c "echo /opt/oracle/instantclient_18_3 > /etc/ld.so.conf.d/oracle-instantclient.conf" and
sudo ldconfig
And finally exported the LD_LIBRARY_PATH with: export LD_LIBRARY_PATH=/opt/oracle/instantclient_18_3:$LD_LIBRARY_PATH
When trying to run a connection inside the notebook with connection = cx_Oracle.connect(usr + '/' + pwd + '#' + url), I receive the DPI-1047 error code that says that libclntsh.so cannot be open, however that library is in the /opt/oracle folder. As another option, when running the same connection through the terminal Python console, I get the ORA-01804 error code, which says that the timezone files were not properly read, which is something I'm trying to fix also but suspect is related to cx_Oracle not finding its library folder. (Now, explain to me: why does it have to be so difficult for a billionaire company to create a decent library import and installation?)
Is there a step I'm missing? Is there a detail about AWS SageMaker that I should account for? Also, is there another option for extracting data from an oracle server through Python and AWS?
Hi and thank you for using SageMaker!
After some effort, I was finally able to figure out a sequence of steps that allowed me to query an Oracle 12 database from within a SageMaker notebook instance. Here are the steps that I took:
I created an Oracle 12 database using Amazon RDS for testing purposes. (You can of course skip this step if you already have an Oracle database available.)
I downloaded the Oracle 12 Instant Client RPM as described here. Note that you will need an Oracle Account in order to download this file.
I uploaded the RPM to my SageMaker Notebook Instance from within JupyterLab. Note that this can take 2-3 minutes to completely upload before proceeding to the next step. (I initially had problems running the installation because the upload was still in progress.)
I ran all of the following commands from the Jupyter terminal as prescribed in the Oracle instructions:
cd SageMaker
sudo yum install oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
sudo sh -c "echo /usr/lib/oracle/12.2/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
sudo mkdir -p /usr/lib/oracle/12.2/client64/lib/network/admin
# Restart Jupyter...
sudo restart jupyter-server
I then installed the cx_Oracle library:
source activate python3
pip install cx_Oracle --upgrade
Then lastly, I created a new notebook using the conda_python3 kernel:
import cx_Oracle
db = cx_Oracle.connect("my_username/my_password#my-rds-instance.ccccccccccc.us-east-1.rds.amazonaws.com/ORCL")
# Example query
cursor = db.cursor()
for row in cursor.execute('select * from DBA_TABLES'):
print(row)
...and then it worked!
Note that it took me a few tries to figure out the exact connection string, which can differ depending on how your database is configured. Unfortunately, the error messages were hard to understand - in my case, I had the error ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA until I had specified the /ORCL at the end of the connection string.
If you need to do these steps frequently, you can add the installation and configuration of the Oracle client in a SageMaker Lifecycle Configuration script. I haven't tested that scenario, but it might be worth a try!
One last thing, I noticed in your question that you are using the Oracle 18 client. I didn't test that exact scenario, since I only have access to an Oracle 12 database. However, the Oracle 12 client should be able to connect to an Oracle 18 database, too.
Best,
Kevin

How to install SQL * PLUS client in linux

I am working on AWS services. I have an ec2 ( centos ) instance. I need to configure SQL*Plus client on this centos machine.
The server with whom I want to connect is at some remote area. The server version is oracle-se(11.2.0.2)
How can I get the client installed on the CentOS machine?
Go to Oracle Linux x86-64 instant clients download page
Download the matching client
oracle-instantclient11.2-basic-11.2.0.2.0.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.2.0.x86_64.rpm
Install
rpm -ivh oracle-instantclient11.2-basic-11.2.0.2.0.x86_64.rpm
rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.2.0.x86_64.rpm
Set environment variables in your ~/.bash_profile
ORACLE_HOME=/usr/lib/oracle/11.2/client64
PATH=$ORACLE_HOME/bin:$PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib
export ORACLE_HOME
export LD_LIBRARY_PATH
export PATH
Reload your .bash_profile by simply typing source ~/.bash_profile (suggested by jbass) or Log-out user and log-in again.
Now you're ready to use SQL*Plus and connect your server. Type in :
sqlplus "username/pass#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.2.1)(PORT=1521))(CONNECT_DATA=(SID=YOURSID)))"
The solution by #ChamaraKeragala is good, but it is unnecessary to logout/login. Instead type:
source ~/.bash_profile
For everyone still getting the following error:
sqlplus command not found
The original post refers to a set of environment variables, the most important of which is ORACLE_HOME. This is the parent directory where the oracle binaries get installed.
Depending on what version of oracle you downloaded you'll have to change the ORACLE_HOME accordingly. For example, the original question's ORACLE_HOME was set to:
ORACLE_HOME=/usr/lib/oracle/11.2/client64
My version of Oracle happens to be 12.1, so my ORACLE_HOME is set to:
ORACLE_HOME=/usr/lib/oracle/12.1/client64
If you are unsure of the version that you downloaded, you can:
cd /usr/lib/oracle after the installation and find the version.
Look at the RPM file oracle-instantclient12.1, where the bolded bits would refer to the version number.
There's a good blog post[1] on $subject. setup oracle client in ubuntu with minimum effort. Following are the main steps on how to step up the client.
In my case, I was installing rpm files using alien package.
Install alien and related packages
sudo apt-get install alien
Install oracle client packages using alien.
sudo alien -i oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
sudo alien -i oracle-instantclient11.2-sqlplus-11.2.0.3.0-1.x86_64.rpm
In my opinion these two steps are the easiest way to install oracle client rpm's on your ubuntu system. (I'm not going to mention about export oracle specific variables as it's already clearly explained in above answers)
Hope it helps someone.
[1] http://pumuduruhunage.blogspot.com/2016/04/setup-oracle-sql-plus-client-on-aws.html
For any one who is using proxy, you'd need to add an extra line to the bash profile. At least this is what made it work for me. I'm using cntlm.
export no_proxy=
Install via zip (tried with 12_2)
First of all there is no need to set ORACLE_HOME.
Simply download the .zip files from here starting with the first one Basic: followed by SQL*Plus: and any additional zips you may need.
Extract them all under /opt/oracle
You will then have a directory: /opt/oracle/instantclient_x_y
On ubuntu I had to do also:
sudo apt install libaio1
To run:
# This can be also done by adding only the path below in: /etc/ld.so.conf.d/oracle-instantclient.conf
export LD_LIBRARY_PATH=/opt/oracle/instantclient_x_y:$LD_LIBRARY_PATH
# This can be added in ~/.profile or ~/.bashrc
export ORACLE_HOME=/opt/oracle/instantclient_x_y
/opt/oracle/instantclient_x_y/sqlplus user/pass#hostname:1521/sidorservicename
At the bottom of the the above link page there are more details.

Getting error while executing bcp in UNIX box

Currently I am working on executing Perl script in Unix box. The Perl script internally uses BCP command for for fetching the data from MS-SQL DB. The same perl script is properly running in Windows. But when I am executing it into Unix (using Putty). It is showing following error message on console.
sh: bcp: command not found
Below is my sql command in perl script.
"bcp \"select drive_id, ilf, xcoord, ycoord
from $mdtdb\.dbo\.$mdtcentraldtdtable a where
drive_id = $driveid and not exists
(select 'x' from $mdtdb\.dbo\.$mdtcentralaudittable b where
a.drive_id = b.drive_id and a.ilf = b.ilf)\"
queryout $tempdatafile -o $bcpoutfile -S $mdtsvr -q -c -t ,
-U $user1 -P $pw1";
I am successfully able to connect with the DB.
I need your help to resolve above bcp issue.
On unix, there is a bcp tool for sybase clients. It works only for Sybase Products.
There is an open-source replacement for bcp that works with MSSQL server: freebcp But the freetds library is a bit difficult to configure, and (being a reverse engineering effort by volunteers) it works only with relatively simple datatypes.
To install it on ubuntu/debian based linux machines, run this
apt-cache search freetds
freetds-common - configuration files for FreeTDS SQL client libraries
freetds-dev - MS SQL and Sybase client library (static libs and headers)
libct4 - libraries for connecting to MS SQL and Sybase SQL servers
libdbd-freetds - Freetds database server driver for libdbi
libsybdb5 - libraries for connecting to MS SQL and Sybase SQL servers
tdsodbc - ODBC driver for connecting to MS SQL and Sybase SQL servers
freetds-bin - FreeTDS command-line utilities
libaprutil1-dbd-freetds - Apache Portable Runtime Utility Library - FreeTDS Driver
sqsh - commandline SQL client for MS SQL and Sybase servers
libqt4-sql-tds - Qt 4 FreeTDS database driver
This command is optional:
apt-file list freetds-bin
freetds-bin: /usr/bin/bsqldb
freetds-bin: /usr/bin/bsqlodbc
freetds-bin: /usr/bin/datacopy
freetds-bin: /usr/bin/defncopy
freetds-bin: /usr/bin/fisql
freetds-bin: /usr/bin/freebcp
freetds-bin: /usr/bin/osql
freetds-bin: /usr/bin/tdspool
freetds-bin: /usr/bin/tsql
These commands install most of what you need
apt-get install freetds-bin
apt-get install freetds-dev
You must then enter your connection data into configfile
/etc/freetds/freetds.conf
[OurMSSQLServer]
host = 1xx.xxx.xxx.xx
port = 1433
tds version = 8.0
Maybe something else in this configfile has to be changed. This I don't remember right now.
From here on you are on your own. Please read the freetds docs: http://www.freetds.org/userguide/
The freetds software is up-to-date (actively developed). But the dev team is very small I think.
You can also compile freetds from source, but that may take more effort.
The error is very clear: the bcp command is not found in your $PATH and therefore it fails, but the real issue is that bcp is a MS SQL server utility and you won't find it on UNIX
You might be able to get programmatic access to an MSSQL database from Perl on unix by using DBI ‌→ DBD::ODBC ‌→ Easysoft SQL Server ODBC Driver

Resources