You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

无需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:

Using SQL*Plus to Connect

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_name
    

    Breakdown 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 server
    • db_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)))"
Using SQLcl to Connect

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_name
    

    Just like SQL*Plus, you can omit the password for a secure prompt:

    sql your_username@//db_hostname_or_ip:db_port/db_service_name
    
  • Full 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 --connect flag 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.ora file (if present) to ensure NAMES.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

火山引擎 最新活动