Search code examples
oracle-databasesqlplus

can't login via sqlplus command but manual login works


I am trying to login to my database using sqlplus command. So I passed my username(aryan) and password(12345) but it says invalid username/password

But when I am asked to manually enter same user-name and password, I am logged in successfully. I have no idea what's wrong

bash-4.4$
sqlplus aryan/12345@localhost:1521/ORCLPDB1

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Feb 24 03:37:54 2024
Version 19.19.0.0.0

Copyright (c) 1982, 2023, Oracle.  All rights reserved.

ERROR:
ORA-01017: invalid username/password; logon denied


Enter user-name: aryan
Enter password:
Last Successful login time: Fri Feb 23 2024 18:10:39 +00:00

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.19.0.0.0

SQL>

How can I login with one line sqlplus command by passing username/password?


Solution

  • Local connections don't go through the listener, so they don't use a TNS string or connection string of any kind. They use the bequeath protocol whereby your client process becomes your database process (e.g. there is no separate shadow process like a listener-created connection makes). Because they don't go through the listener via TCP, they don't use a host, a port, or a service name.

    Rather than the command line, your environment (principally $ORACLE_SID naming the instance, but also requiring $ORACLE_HOME, a $PATH that includes $ORACLE_HOME/bin and in some cases also $LD_LIBRARY_PATH that includes $ORACLE_HOME/lib) will determine which instance you connect to, as you can have multiple instances on the box, and which software installation/version to use (as you can have multiple software installations, and you must use the one that the instance is using).

    Set this environment either manually, in login scripts, or with . oraenv (which you apparently already have, as you are able to connect in one of your tests), and then invoke sqlplus simply with:

    sqlplus aryan/12345

    All by itself. Be aware though, you are putting your password on the command line, visible to anybody logged into the box who does a ps or anyone logging into the same OS account later and browsing command history. For scripting, far better to use /NOLOG and use a here-doc (simplest), or pipe in via stdin from a file to provide the login info:

    Here-doc:

    sqlplus /nolog << EOF
    connect aryan/12345
    --do stuff...
    exit
    EOF
    

    Or file via stdin:

    file contents:

    connect aryan/12345
    --do stuff...
    exit
    

    And pipe it in:

    sqlplus /nolog < file
    

    Or

    cat file | sqlplus /nolog
    

    That way your password isn't part of the command string. Far safer.