Sub topic 3
Saving and reusing connections in SQLcl
This chapter assumes you can start SQLcl, know the host, port, and service name of your database, and hold an account on it. You should also know that a service name identifies the database service a client wants, not the machine itself.
Saving a named connection
SQLcl keeps named connections in a connection store, so you type the full connect string once. The -save option names the connection, and -savepwd stores the password with it:
SQL> connect -save orclpdb_hr -savepwd hr@//dbhost.example.com:1521/orclpdb1.example.com
SQL> connmgr list
SQL> connect -name orclpdb_hrOmit -savepwd if you want SQLcl to prompt for the password each time.
Connecting as SYSDBA
Privileged roles connect with as sysdba. Operating system authentication avoids a password on the command line:
SQL> connect / as sysdba
SQL> connect sys@orclpdb as sysdbaThe account must hold the SYSDBA privilege. Use these connections only for administration tasks.
Connecting to a cloud database with a wallet
An Autonomous Database uses a wallet, a zip file that holds certificates and service aliases. Point SQLcl at the wallet, then connect with one of its aliases:
SQL> set cloudconfig /path/to/Wallet_MYDB.zip
SQL> connect admin@mydb_highTroubleshooting common connection errors
| Error | Meaning | First check |
|---|---|---|
| ORA-12541 | No listener answers at the host and port | Host, port, and whether the listener is running |
| ORA-12514 | The listener does not know the service name | Spelling of the service name |
| ORA-12154 | The alias cannot be resolved | Location of tnsnames.ora and spelling of the alias |
| ORA-01017 | Username or password is wrong | Credentials and password case |
Handling credentials safely
Do not write passwords into scripts or leave them in shell history. SQLcl stores saved passwords in encrypted form under your user profile, so use -savepwd only on a machine you control. On shared machines, let SQLcl prompt instead.
Running SQLcl in scripts
Silent mode (-S) suppresses banners, and -L limits the login to one attempt so a failed login ends the run instead of waiting for input:
sql -S -L hr@orclpdb @report.sqlSupply the password from a secured source, not from the script file.
Exercise
A script fails with ORA-12514. What do you check first?
Show answer
Confirm that the service name in the connect string matches a service the listener registers. Run lsnrctl services on the database host to list them.