无需TNSNames.ORA文件,如何用SQLcl与SQL*Plus连接Oracle数据库?
Great question! Since you already have all the necessary TNS details but don’t want to deal with creating or maintaining a TNSNames.ORA file, Easy Connect (EZCONNECT) is your best bet here—it lets you plug your database connection info directly into the command line for both SQL*Plus and SQLcl. Let’s walk through each tool:
SQL*Plus fully supports EZCONNECT syntax, which eliminates the need for TNSNames.ORA. Here’s how to use it:
Basic connection command (replace placeholders with your actual info):
sqlplus your_username/your_password@//db_hostname_or_ip:db_port/db_service_nameBreakdown of each part:
your_username/your_password: Your database credentials (for better security, omit the password and you’ll be prompted to enter it interactively:sqlplus your_username@//db_hostname_or_ip:db_port/db_service_name)//db_hostname_or_ip: The hostname or public IP address of your Oracle database serverdb_port: The listener port (default is 1521 if you didn’t customize it)db_service_name: The database’s service name (check your TNS info for this—note that this is often different from the SID, though some setups use the same value)
Example command:
sqlplus scott/tiger@//prod-db.example.com:1521/orclpdb1
If you need to use advanced connection parameters (like SSL or custom driver settings), you can also pass the full TNS descriptor directly in quotes:
sqlplus your_username/your_password@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=prod-db.example.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orclpdb1)))"
SQLcl (Oracle’s modern command-line tool) works even more smoothly with direct connection strings, and it supports both EZCONNECT and full TNS descriptors:
Basic EZCONNECT connection:
sql your_username/your_password@//db_hostname_or_ip:db_port/db_service_nameJust like SQL*Plus, you can omit the password for a secure prompt:
sql your_username@//db_hostname_or_ip:db_port/db_service_nameFull TNS descriptor connection (for advanced configurations):
sql your_username/your_password@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=prod-db.example.com)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orclpdb1)))"Bonus: SQLcl also supports a
--connectflag for clarity if you prefer:sql --connect your_username@//db_hostname_or_ip:db_port/db_service_name
Quick Notes
- EZCONNECT is enabled by default in most Oracle client setups, but if you hit errors, check your
sqlnet.orafile (if present) to ensureNAMES.DIRECTORY_PATH=(EZCONNECT, TNSNAMES)is set (EZCONNECT should be included in the list) - SQLcl doesn’t require a full Oracle client installation, but since you already have one, you’re all set to use it right away
内容的提问来源于stack exchange,提问作者thatjeffsmith




