Thursday, September 7, 2023

Syntax for switching over a container database using data guard broker

Here I am switching over a container database called cdb, to the standby server which is running a container database with unique name cdb_stby1.

First, view the configuration:
DGMGRL> show configuration

Configuration - DGConfig1

  Protection Mode: MaxPerformance
  Members:
  cdb       - Primary database
    cdb_stby1 - Physical standby database

Fast-Start Failover:  Disabled

Configuration Status:
SUCCESS   (status updated 84 seconds ago)
Perform the switchover:
DGMGRL> switchover to 'cdb_stby1';
Typical output from a successful switchover:
Performing switchover NOW, please wait...
Operation requires a connection to database "cdb_stby1"
Connecting ...
Connected to "cdb_stby1"
Connected as SYSDBA.
New primary database "cdb_stby1" is opening...
Operation requires start up of instance "cdb" on database "cdb"
Starting instance "cdb"...
Connected to an idle instance.
ORACLE instance started.
Connected to "cdb"
Database mounted.
Switchover succeeded, new primary is "cdb_stby1"
If I wish to switch back, make sure you log on to whichever server is running your primary database at the time.
(DO NOT use os authentication with dgmgrl / as sysdba, it will throw an error)
dgmgrl sys@cdb_stby1 as sysdba
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Thu Sep 7 15:27:27 2023
Version 19.20.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

Welcome to DGMGRL, type "help" for information.
Password:
Connected to "cdb_stby1"
Connected as SYSDBA.
DGMGRL> show configuration;

Configuration - DGConfig1

  Protection Mode: MaxPerformance
  Members:
  cdb_stby1 - Primary database
    cdb       - Physical standby database

Fast-Start Failover:  Disabled

Configuration Status:
SUCCESS   (status updated 46 seconds ago)

DGMGRL> switchover to cdb
Performing switchover NOW, please wait...
Operation requires a connection to database "cdb"
Connecting ...
Connected to "cdb"
Connected as SYSDBA.
New primary database "cdb" is opening...
Operation requires start up of instance "cdb" on database "cdb_stby1"
Starting instance "cdb"...
Connected to an idle instance.
ORACLE instance started.
Connected to "cdb_stby1"
Database mounted.
Switchover succeeded, new primary is "cdb"

Thursday, August 31, 2023

How to extract all datafile names without path using substr and instr functions

select trim(
            substr(file_name,
                (instr(file_name,'/', -1, 1) +1)
                )
               )  "file name"
from  dba_data_files;
Result:
file name
system01.dbf
sysaux01.dbf
undotbs01.dbf
users01.dbf
appl_data.dbf

More on how to display the current container in a multitenant database

My listener is listening for connections to a service called "myservice1", which runs out of the pluggable database "pdb1":
lsnrctl status
Service "pdb1" has 1 instance(s). <-- the default service for the pluggable database
  Instance "cdb", status READY, has 1 handler(s) for this service...

Service "myservice1" has 1 instance(s). <-- service defined by the DBA
  Instance "cdb", status READY, has 1 handler(s) for this service...
In the code snippet below, I am executing a script called disp_session.sql which will

  • connecting directly to a service served by the listener
  • switching schema within my session

    The following will display the username, current schema, and the service my connction is connecting to:
    alter session set current_schema=scott;
    set lines 200
    col service format a20
    col container format a20
    col username format a20
    col "container ID" format a20
    col schemaname format a20
    select sys_context ('userenv','SERVICE_NAME')   service,
           sys_context ('userenv','CON_NAME')       container,
           sys_context ('userenv','CON_ID')         "container ID",
           sys_context ('userenv','CURRENT_USER')   username,
           sys_context ('userenv','CURRENT_SCHEMA') schemaname
    from dual;
    
    oracle@server1.oric.no:[cdb]# sqlplus system@myservice1 @disp_session.sql
    
    Session altered.
    
    SERVICE              CONTAINER            container ID         USERNAME             SCHEMANAME
    -------------------- -------------------- -------------------- -------------------- --------------------
    myservice1           pdb1                 4                    SYSTEM               SCOTT
    
    Useful information when debugging client connections ;-)
  • Friday, August 25, 2023

    How to exchange a line in a text file with a different one, when using double quotation marks in the text itself?

    The following sed command will exchange the string
    initParams=
    
    with
    initParams=undo_tablespace=UNDOTBS1,sga_target=9GB,db_block_size=32768BYTES,control_files=\"\/u02\/oradata\/{DB_UNIQUE_NAME}\/control01.ctl\"\,\"\/recovery\/fra\/{DB_UNIQUE_NAME}\/control01.ctl\"),db_recovery_file_dest=\/recovery\/fra\/{DB_UNIQUE_NAME}
    
    in the file "myresponsefile.rsp":
    sed -i "s/initParams=/initParams=undo_tablespace=UNDOTBS1,sga_target=9GB,db_block_size=32768BYTES,control_files=\"\/u02\/oradata\/{DB_UNIQUE_NAME}\/control01.ctl\"\,\"\/recovery\/fra\/{DB_UNIQUE_NAME}\/control01.ctl\"),db_recovery_file_dest=\/u04\/recovery\/{DB_UNIQUE_NAME} /" myreponse_file.rsp
    
    Note that all double quotes (") and forward (/) slashes must be masked by the backslash charcater \ to be interpreted correctly.

    Tuesday, August 22, 2023

    Cause and solution to ORA-00058: DB_BLOCK_SIZE must be 8192 to mount this database (not 16384)

    You attempt to execute dbca silently, using a response file, as shown below:
    dbca -createDatabase -responsefile mydb01.rsp -silent
    
    But it errors out with the error:
    [WARNING] ORA-00058: DB_BLOCK_SIZE must be 8192 to mount this database (not 16384)
    and the instance is left running, with an unmounted database.

    Cause:

    This means you are using a pre-defined template for dbca that includes datafiles.

    When you do this, you cannot change the db_block_size. It is set to 8K, and carved in stone.

    Solution:

    Create your own, customized template and refer to that instead.

    Remember that templates ending with the extension .dbc contains database files, so they will be significantly faster to use, than custom made templates ending with the extension .dbt - the latter will have to go through the "CREATE DATABASE" statements from scratch.

    Templates that do not contain datafiles have the extension .dbt

    What you can do in order to have your cake and eat it, too, is to first create a database with a non-confirming db_block_size, then create a dbca template based on this new database, including its datafiles.

    I will see if I have time to write a blog post about how to do extactly this.

    Tuesday, August 15, 2023

    Simple PL/SQL script to alter parameter in database

    At my current workplace, we use emcli for mass-updates of database parameters in groups of databases.

    Below is a simple PL/SQL script that can be used to change parameter if needed. If the parameter is already set, nothing happens.

    In my case, it was the parameter "recyclebin" that was needed to be switched from OFF to ON for some databases, but it could be any parameter.

    Surely there are many ways to solve such a problem, this was how I solved it with very little effort :-)
    set serveroutput on
    set feedback off
    set echo off
    set verify off
    
    DECLARE
    
     v_db_name v$database.name%%TYPE;
     v_rb v$system_parameter.name%%TYPE;
    
    
    BEGIN
     execute immediate 'alter session set nls_language=''american''';
     select name
     into v_db_name
     from v$database;
    
     select value
     into v_rb
     from v$system_parameter
     where name = 'recyclebin';
     
    --dbms_output.put_line('v_db_name: ' || v_db_name);
    --dbms_output.put_line('v_rb:      ' || v_rb);
    
    CASE
       WHEN v_rb = 'ON' THEN
        dbms_output.put_line('Database ' || v_db_name || ': recyclebin already enabled.');
       ELSE
        dbms_output.put_line('Database ' || v_db_name || ': recyclebin is currently disabled. Turning it on now...');
        execute immediate 'alter system set recyclebin=ON scope=spfile';
         dbms_output.put_line('Database ' || v_db_name || ' now has recyclebin=ON in spfile. Database must be bounced in in order to enable the setting');
       END CASE;
    END;
    /
    exit
    
    When we execute the script through emcli, the syntax comes to:
    emcli execute_sql -sql="FILE" -iemcli execute_sql -sql="FILE" -input_file="FILE:/scripts/chk_and_alter_param.sql" -targets="TEST_DBS:composite"
    

    Friday, August 4, 2023

    How to trace the dbca tool

    These notes are taken directly from MOS, and jotted down here so I have it readily available for the future.

    To trace the DBCA, we need to perform the following.

    1. Do a copy of the original dbca file in $ORACLE_HOME/bin. For example:
    cp $ORACLE_HOME/bin/dbca $ORACLE_HOME/bin/dbca.ori
    
    2. vi the dbca file in the $ORACLE_HOME/bin directory.

    3. At the end of the file, look for the following line:
    # Run DBCA
    $JRE_DIR/bin/jre -DORACLE_HOME=$OH -DJDBC_PROTOCOL=thin -mx64m -classpath $CLASSPATH oracle.sysman.assistants.dbca.Dbca $ARGUMENTS
    
    4. Add the following just before the -classpath in the '$JRE_DIR' line:
    -DTRACING.ENABLED=true -DTRACING.LEVEL=2
    
    5. At the end of the dbca file, the string should now look like this:
    # Run DBCA
    $JRE_DIR/bin/jre -DORACLE_HOME=$OH -DJDBC_PROTOCOL=thin -mx64m -DTRACING.ENABLED=true -DTRACING.LEVEL=2 -classpath $CLASSPATH oracle.sysman.assistants.dbca.Dbca $ARGUMENTS
    
    6. To trace, run:
    [host]/u01/home/usupport> dbca > dbca.out >
    
    The output will be written to the dbca.out file.