How to Connect to PDB on a Linux Server

How to Connect to PDB on a Linux Server

·

5 min read

When working with Oracle databases on a Linux server, connecting to a Pluggable Database (PDB) might seem tricky at first. But once you understand the steps, it becomes a straightforward task. A PDB is a portable and self-contained database within the Oracle Multitenant architecture. Whether you’re a Linux admin or a database enthusiast, knowing how to connect to a PDB is crucial for managing modern database environments.

In this guide, I’ll walk you through the exact steps to connect to a PDB on a Linux server. I’ll cover the commands, configurations, and tips that simplify the process. By the end, you’ll have the confidence to handle this task like a pro.


Step 1: Understand Your Oracle Environment

Before jumping into commands, it’s essential to know the setup of your Oracle database. Each Oracle database instance has a Container Database (CDB) and potentially multiple PDBs.

Key Terms You Should Know

  1. CDB (Container Database): The primary database that contains PDBs.

  2. PDB (Pluggable Database): Independent databases within the CDB.

  3. TNS Names: A configuration file that helps clients connect to a database.

Make sure you know:

  • The name of the PDB you want to connect to.

  • Whether the PDB is already open (ready for connections).

  • If the TNS Listener is configured correctly on the server.

Check your setup using the sqlplus tool:

sqlplus / as sysdba

Run this query to list all PDBs:

SELECT NAME, OPEN_MODE FROM V$PDBS;

Step 2: Open the PDB (If It’s Not Already Open)

Sometimes, PDBs might be in a mounted state, meaning they’re not ready for connections yet. You need to open them manually. Here’s how you can do it:

  1. Connect to the CDB:

     sqlplus / as sysdba
    
  2. Switch to the PDB:

     ALTER SESSION SET CONTAINER = pdb_name;
    
  3. Open the PDB:

     ALTER PLUGGABLE DATABASE OPEN;
    

Replace pdb_name with the name of your PDB. You can verify the status again using:

SELECT NAME, OPEN_MODE FROM V$PDBS;

If the status shows READ WRITE, your PDB is ready for connections.


Step 3: Configure TNS Names for the PDB

The tnsnames.ora file is crucial for database connectivity. It tells Oracle clients how to connect to a specific database. This file is usually located in:

$ORACLE_HOME/network/admin/tnsnames.ora

Adding a PDB Entry

Open the tnsnames.ora file with a text editor like vi:

vi $ORACLE_HOME/network/admin/tnsnames.ora

Add an entry for your PDB. Here’s an example:

PDB_NAME =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = your_server_ip)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdb_name)
    )
  )
  • Replace your_server_ip with the server's IP address.

  • Replace pdb_name with your actual PDB name.

Save the file and reload the listener to apply changes:

lsnrctl reload

Step 4: Connect to the PDB Using SQL*Plus

Now that your PDB is open and the TNS configuration is updated, it’s time to connect. Use the sqlplus command with the username and password for the PDB:

sqlplus username/password@PDB_NAME

If the connection is successful, you’ll see the SQL prompt connected to the PDB.


Step 5: Using EZConnect for Quick Connections

If you don’t want to bother with tnsnames.ora, you can use Oracle’s EZConnect method. This allows you to connect directly using a simplified connection string.

Here’s an example:

sqlplus username/password@//your_server_ip:1521/pdb_name

Replace:

  • your_server_ip with the server's IP address.

  • pdb_name with your PDB name.

This method is fast but requires you to remember the full connection string.


Common Errors and Troubleshooting Tips

  1. TNS Listener Not Running:

    • Ensure the listener service is active:

        lsnrctl status
      
    • If it’s not running, start it with:

        lsnrctl start
      
  2. PDB Not Found:

    • Verify the PDB name with:

        SELECT NAME FROM V$PDBS;
      
  3. Insufficient Privileges:

    • Make sure your user account has the correct privileges to access the PDB. For admin tasks, use the SYS user.
  4. Network Issues:

    • Ensure the server’s firewall isn’t blocking the Oracle listener port (usually 1521).

Summary

Connecting to a PDB on a Linux server involves understanding your Oracle environment, opening the PDB if necessary, configuring the TNS Names file, and using tools like SQL*Plus or EZConnect. While the process might feel technical at first, it becomes second nature once you’ve done it a couple of times.

By mastering these steps, you’ll have full control over your PDB connections, whether you’re managing a single database or working in a larger Oracle environment.


FAQs

How do I check if the PDB is open or closed?

Run this query after logging into SQL*Plus:

SELECT NAME, OPEN_MODE FROM V$PDBS;

If the status is READ WRITE, the PDB is open.

What is the default port for Oracle database connections?

The default port is 1521, used by the Oracle TNS Listener.

Can I connect to a PDB without modifying tnsnames.ora?

Yes, you can use EZConnect with this format:

sqlplus username/password@//server_ip:port/pdb_name

What should I do if I get a “TNS:listener does not currently know of service requested” error?

This means the PDB isn’t registered with the listener. Open the PDB using:

ALTER PLUGGABLE DATABASE pdb_name OPEN;

Then reload the listener:

lsnrctl reload

How can I automate connecting to a PDB?

You can create a shell script with the sqlplus command and your connection details. This saves time for repetitive tasks.