Oracle connectivity in Ruby - ruby

In my ruby script, I set to connect to oracle db via:
conn = OCI8.new(username, password, database)
It works well on my own desktop (Ubuntu), however, when I deploy it onto a lab machine (Ubuntu VM), it has oracle connection error. The machine has all oracle drivers needed because I can connect to it by commandline sqlplus. Also I did echo $ORACLE_HOME and it's set correctly.
The error is like:
env.c:257:in oci8lib.so: ORA-12154: TNS:could not resolve service name (OCIError)
I had the same error on my desktop when ORACLE_HOME was not set correctly. Once I set it right, it started to work. But on this lab machine, although I can launch sqlplus w/o problem, I can't connect via ruby script. Any advice?

The ORA-12154 error means that the value of the "database" argument in your OCI8.new call is not being found in the tnsnames.ora file that the environment is using. This could be due to:
an incorrect value being passed through the variable
something in the environment that is
pointing to the wrong location for
tnsnames.ora. By default it should
be in $ORACLE_HOME/network/admin, but
this can be overridden by the
TNS_ADMIN environment variable.
You say that you can launch sqlplus on the lab machine without any problem, and I assume that this means you can connect to the database of interest when you do this. If this is the case, it seems that the environment present when you run the Ruby script must be somehow different.

Related

Using DBI to connect to Oracle Database in Perl getting ERROR OCIEnvNlsCreate in Windows

In windows machine, When trying to connect to Oracle Database via Perl (strawberry-perl-5.32.0.1) getting below error.
Error:
failed: ERROR OCIEnvNlsCreate. Check ORACLE_HOME (Linux) env var or PATH (Windows) and or NLS settings, permissions, etc.
Environment variables ORACLE_HOME & PATH are correctly pointing to oracle insta client 12.2 version.
Code shown below:
my $dbh = DBI->connect('dbi:Oracle:host=hostname;service_name=servicename;port=xxx','username','password');
Issue Resolved. ORACLE_HOME needs to be set at system level environment variable rather than account level environment variable

Can connect to database with sqlplus directly, but not when setting ORACLE_SID

I am trying to connect to a database in a shell script. I can connect fine if I use this:
sqlplus user/pass#db_service_name
However, when I try something like this, it is not working:
ORACLE_SID=db_sid
export ORACLE_SID
sqlplus /nolog
connect user/pass
I get an error:
ERROR:
ORA-12545: Connect failed because target host or object does not exist
Obviously I am missing something, but this is an area I am not fully familiar with, so I am not sure where I am going wrong. My assumption was that if I set the ORACLE_SID, that I would not need to identify my db when connecting in sqlplus. Is my assumption incorrect? Or am I missing some required step or configuration?
Using sqlplus user/pass#db_sid refers to a TNS alias in $ORACLE_HOME/network/admin/tnsnames.ora. If the instance is running on the same host as your sqlplus command, then you need to look at the text of the TNS alias in tnsnames.ora and set ORACLE_SID to the SID referenced in the file and then execute . oraenv. If the instance is not running on the same host as your sqlplus command, then your latter attempt will never work.
If you want more secure connections, you might consider using a wallet.

Why can I log in with SQL Developer but not with SQL*Plus?

I'm trying to connect to from SQL*Plus and get error ORA-12154, but I can connect from SQL Developer without any error. I can tnsping and also lsnrctl is okay and the listener is up and running.
I can connect as sys like sqlplus sys as sysdba which works fine; but with another user I do sqlplus username and when I type the password it gives me:
ORA-12154: TNS:could not resolve the connect identifier specified
SQL*Plus interprets an # symbol as the start of a TNS alias. If your password has that in it then it will be interpreted that way even if you are not trying to connect over SQL*Net, but with a local connection. So if you do:
sqlplus user/p#ssword
... then it will take just the p as the password, and the #ssword as the alias, so it will try to find a TNS alias of ssword in your tnsnames.ora file. Since that doesn't exist, you'll get the "ORA-12154: TNS:could not resolve the connect identifier specified" error.
That applies when you put your password in at the prompt too, so if you do:
sqlplus user
Enter password: p#ssword
you'll get the same thing. You can connect by enclosing the password in double-quotes:
sqlplus user
Enter password: "p#ssword"
but you can't do that with the unprompted version.
You're probably better off changing your password to something without that particular character if you want to be able to connect from SQL*Plus as well as from other clients that don't interpret TNS in the same way.
SQL Developer, for example, accepts the password and connection settings as separate fields; it isn't trying to parse the string you entered to identify its components, so it doesn't get confused. Even if you tell SQL Developer to connect using a TNS alias, it will use the tnsnames.ora value you select from its drop-down list, it won't try to take it from the user or password fields.
This one worked for me ( and was easier to implement from my behalf )
From : TNSPING OK but sqlplus gives ORA-12154?
Create an environment variable TNS_ADMIN that points to the directory where your tnsnames.ora file resides. Then try to connect with sqlplus.
TNS_ADMIN = D:\oracle\12.1.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
If that works, then my guess is you maybe installed the Oracle client software too, and when you run sqlplus, it looks for the tnsnames.ora file in your client home.
-- Instructions for Adding the Environment variable TNS_ADMIN in windows
Go to control panel / system
select Advanced system settings
Select "Advanced" tab, and the environment variable button is at the bottom.
create new variable TNS_ADMIN and give the path where the .ora files are stored. e.g. C:\app\oracle\product\11.2.0\client_1\network\admin

Oracle Pro*C ORA-12547

I'm working on re-compiling some Pro*C code that no one currently at my company has ever compiled. It's compiling OK, but when I copy it to the production server and run it I'm getting Oracle error ORA-12547 (TNS: lost contact)
Any ideas?
Oh, figured it out. :-)
The C executable isn't run directly on the server, but launched through a script that sets up the connection environment. i.e. something like this:
export SHLIB_PATH=/oracleDatabaseSidHere/oracle/10.2/lib:/usr/lib:/oracleDatabaseSidHere/oracle/10.2/odg/lib
export ORACLE_BASE=/oracleDatabaseSidHere/oracle
export ORACLE_SID=oracleDatabaseSidHere
export ORACLE_HOME=/oracleDatabaseSidHere/oracle/10.2
# Set up the path and executable to run
PATH=.\:$PATH:$ORACLE_HOME/bin:/usrlocal/bin
PATH=$PATH:/path_to_application_logs
cd /path_to_application
application
Related to this, I just found out that I get the same error ORA-12547 TNS lost contact on my Windows machine when I try to connect to an Oracle database with PL/SQL and at the same time I have a VPN connection open.
I had a similar problem on Linux, Oracle 11gR2. Oracle owner could run SQL*Plus and get in via either TNS or local connection. Non-oracle user could log in via TNS, but not as local connection.
Turns out the answer was that the ORACLE_HOME filesystem was mounted "nosuid". Removing that mount option solved my issue.

connecting to remote oracle via cygwin sqlplus

I'm trying to use cygwin sqlplus to connect to a remote oracle installation located at myserver.mycompany.com port 1530. When I try
sqlplus username#myserver.mycompany.com:1530/orcl
I get the error:
ORA-12154: TNS:could not resolve the connect identifier specified
When I set ORACLE_HOME to /cygdrive/c/oracle/product/10.2.0/client_1, I get a different error:
Error 6 initializing SQL*Plus
Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
I can telnet to the server's port 1530, and the SQL Developer installed locally is also able to connect to the database. What am I doing wrong?
I wasn't aware there was a native cygwin client for Oracle (correct me if I'm wrong here but I can't find any mention of it on Oracle's web site either). If you're using Cygwin with the Windows client you need to use a native windows path. It won't understand your /cygdrive path.
However, I have used Win32 Oracle clients with cygwin and made it work - the same shell scripts would work on Cygwin, Linux and Solaris.
There isn't a cygwin Oracle client. As above answer, use the ORACLE_HOME set to the windows path.
For the first half of your question, try seeing what the TNS_NAMES env variable is set to, and if not set, you may need to try setting it to the correct place. Usually it defaults to %ORACLE_HOME%/network/admin/tnsnames.ora . SQL Developer keeps it's own copy of the tnsnames file, so you can't reply upon that to fix the problem.
Combining the details from the other answers (which took me a little, so I post it as separate answer instead of comments):
The $ORACLE_SID and, if set, $TNS_ADMIN (and probably other path-related Oracle variables) must use Windows-paths. Your Cygwin $PATH variable should of course use the Cygwin notation (/drives/c/…). So I've included into my .bashrc:
export ORACLE_HOME=c:\\Oracle\\product\\12.1.0\\dbhome_1
export TNS_ADMIN=d:\\Oracle\TNSAdmin
export PATH=$PATH:$(cygpath -u $ORACLE_HOME)/BIN
After that, I had no more issues calling e.g. tnsping or sqlplus. As the above is just an example, you must of course adjust the paths to reflect your installation/configuration :)
NXC is right - it wasn't a cygwin client I was using, but the windows sqlplus client.
I set the windows environment variables for ORACLE_HOME and ORACLE_SID and was then able to run sqlplus in a cygwin bash shell using the net_service_name from tnsnames.ora.
The way it worked for me was to remove /cygdrive/d or /cygdrive/c and replace them with with d:/ or c:/ and then your sqlplus call within cygwin session will work. The /cygdrive/DriveLetter is confusing it.
I have same error like you,
you should set not only ORACLE_HOME in windows Variables,
But also PATH include %ORACLE_HOME%\bin
then you can open windows cmd,
try sqlplus username/password#your_define_tnsname
if okay, then remove all ORACLE PATH setting in cygwin, restart cygwin,try it.
if failure, should let sqlplus works well in windows cmd at first
I have TNS_ADMIN variable configured and it works properly in Win Environ
I used cygpath mnodification in .bashrc
If TNS_ADMIN is configured add following row to .bashrc
export TNS_ADMIN=$(cygpath -m $TNS_ADMIN)

Resources