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

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.

Related

Oracle cannot get sqlldr to reach listener, get ORA-12514 error

Every time I try to run the following sqlldr command on a terminal only oracle 19c system:
sqlldr username/password#$HOSTNAME/$ORACLE_SID control=rules.ctl log=backup_error_logs.txt data=output.csv errors=1000000
I get the following error message:
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
Things I have tried:
Verified that the listener is operable and listener.ora file is properly formatted with lsnrctl.
Verified the connection and oracle environment variables are working as the following sqlplus command runs without problem:
sqlplus username/password#$HOSTNAME/$ORACLE_SID
Verified the tnsname.ora is running properly configured with the following tnsping command:
tnsping $ORACLE_SID
Cannot think of anything else to try or to check. Does anyone have any advice without resorting to the GUI tools?
Could be a problem of quotes in the variables, the behavior of both tools is different regarding quotes.

How to connect SQLPlus with tnsnames.ora

I want to connect with my Oracle database without installing Oracle Client.
I downloaded:
"Instant Client Package - Basic" and
"Instant Client Package - SQL*Plus"
Then I created folder on C:\Oracle\instantclient, where I extracted all packages.
I have been set system environment like:
Path - C:\Oracle\instantclient
NSL_LANG - with properly key
ORACLE_HOME - C:\Oracle\instantclient
ORACLE_SID - C:\Oracle\instantclient
TNS_ADMIN - C:\Oracle\instantclient
Then I created tnsnames.ora file with configuration in C:\Oracle\instantclient
and when I puted a command to cmd:
sqlplus user/password #HOST
I have a message like:
ERROR:
ORA-12560: TNS:protocol adapter error
but when I tried like:
sqlplus user/password#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=address to host)(Port=1521))(CONNECT_DATA=(SID=address to SID)))
everything works properly. Why SQL have a problem with recognize tnsnames.ora file?
Your command should be:
sqlplus user/password#HOST
with no space between the password and #HOST part.
With the space it treats the #HOST as a script to execute once you've logged in, and it tries to connect locally, which produced that TNS error. (As you don't log in the HOST isn't ever evaluated to establish if it exists, so it's effectively noise at this point).
C:\>sqlplus -l -s x/y #HOST
ERROR:
ORA-12560: TNS:protocol adapter error
SP2-0751: Unable to connect to Oracle. Exiting SQL*Plus
With the space removed it looks for HOST as a TNS alias:
C:\>sqlplus -l -s x/y#HOST
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
SP2-0751: Unable to connect to Oracle. Exiting SQL*Plus
For me that still gets an error since I don't have HOST in my tnsnames.ora, but it's a different error and you can see it's at least trying to use it as a TNS alias. If you have it defined properly it will be able to connect to your database.
NEVER edit $ORACLE_HOME/network/admin/tnsname.ora manually, use "netmr" to create Local Name.
Don't know why it's same but do not work.
This commando works fine for me:
sqlplus /nolog
connect username/password#hostname:port/SERVICENAME

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

How to find Oracle database connection details

I am new to Oracle DB and have created a new Oracle 12c database. I am creating a new connection through SQLDeveloper and where can I get the connection details? HostName? Port? SID?
I tried connecting using default details given in documentation but got the below error
Test failed: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor>
You really need to give more info if you need help :)
What Server are you using? Linux? Windows?
Post your oratab file (/etc/oratab)
Post your TNS file ($ORACLE_HOME/network/admin/tnsnames.ora)
Post your Listener file ($ORACLE_HOME/network/admin/listener.ora)
Assuming you're using Linux :D
You need to look at the file "/etc/oratab" if you're on Linux.
This will contain your SID.
Your TNS (located in $ORACLE_HOME/network/admin/) should contain the TNS Entry with SID as per your oratab file.
This is an example entry you will find in the oratab file:
orcl:/opt/oracle/product/10.2.0/db_1:Y
The let hand side "orcl" is your SID, and you will use this for your SID in the Oracle TNS.
If you find that your TNS is configured correctly, then the error message you provided may also indicate that your Database was not registered to the Listener.
You may connect to the sql database as follows:
Set your Oracle Environment
$ . oraenv
Prompts for SID: Enter the value "orcl" (if this is your SID?)
now login:
$ sqlplus / as sysdba
This will log you into oracle as a sysdba.
Execute the follwing:
SQL> Alter System Register;
You should notice a confirmation message that the system is altered.
now exit SQLplus, and try again
SQL> exit
On Windows ISTR, to get the SID, look at the running processes and you should see something called "oracleXXX.exe" or similar. The XXX will be your SID.
You should also see a "TNS" or "Listener" process, probably with the same "XXX".
To get the port number, (usually 1521-1526), find the folder where Oracle was installed to "the "Oracle Home") and in there go to the "network\admin" folder. You should see a "listener.ora" file in there, and if you view this is notepad you should be able to figure out what the port is set to.
This file should also tell you what host/network address the listener is attaching to - you use this IP as the "Hostname" in you connection. Sometimes (again I'm a bit cloudy with Oracle on Windows) the listener.ora might be named listnereXXX.ora, with XXX matching the value you will have seen in SID the checks above.
Obviously if you are new to Oracle, do not amend these files.
If you have netstat/wireshark or similar you can see what the port is attached to the TNS/Listener process.
If you have installed it on same machine on which you are running sql-developer then
host: 127.0.0.1
port: 1521
username and password same as you put while installing oracle datatabse.
keep sid blank

Oracle connectivity in 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.

Resources