Practical Autonomous DB with OCI CLI — Start, Stop & Scale

Data Voyage by Jigar Doshi
5 min readMar 20, 2023

--

Start, Stop , Restart and Scale Autonomous DB (ADB) using simple OCI CLI. Automate routine tasks with few simple OCI commands

In the previous post, I discussed on how to create primary and cross region standby ADB pair. The second article in series discussed about generating wallets and connection strings. In this post, I will describe how to perform routine tasks such as start, stop , restart and scaling operations using OCI CLI.

Task 1 — Start & Stop ADB

We will need compartment and DB OCID for all the operations. Set the required variables as first step.

The code block below saves compartment and ADB OCID in variable.

# This assumes unique compartment name in tenancy. 
# Adjust for duplicate compartments
# Replace <INSERT_COMPARTMENT_NAME> with actual compartment name
export compname=<INSERT_COMPARTMENT_NAME>
export Compocid=`oci iam compartment list --compartment-id-in-subtree true --all |jq -r ".data[] | select(.name == \"${compname}\") | .id"`
# Replace <INSERT_DB_NAME> with actual DB name.
export DBname=<INSERT_DB_NAME>
# Save DB OCID in variable based on name(case sensitive)
export ATPocid=`oci db autonomous-database list --compartment-id $Compocid --query "data[?contains(\"display-name\",'$DBname')]"|jq -r ".[].id"`

Once you have the variables configured, Starting ,Stopping or restarting ADB requires one single command. No need to login to console, spend precious clicks navigating to DB page and performing operations.

Start ADB — To start ADB execute following command

export WorkReqOCID=`oci db autonomous-database start --autonomous-database-id $ATPocid |jq -r '."opc-work-request-id"'`
while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "Startup is $status"
echo "Ctrl-C to exit"
sleep 10
done
ADB startup in progress

Stop ADB — To stop ADB execute following command

export WorkReqOCID=`oci db autonomous-database stop --autonomous-database-id $ATPocid |jq -r '."opc-work-request-id"'`
while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "DB stop is $status"
echo "Ctrl-C to exit"
sleep 10
done
ADB stop in progress

Restart ADB — To restart the ADB execute following commands

export WorkReqOCID=`oci db autonomous-database restart --autonomous-database-id $ATPocid |jq -r '."opc-work-request-id"'`
while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "DB stop is $status"
echo "Ctrl-C to exit"
sleep 10
done
DB restart in progress

Task 2 — Scaling ADB

Whether it’s for saving cloud costs or to improve performance, scaling ADB CPU is routine task.

We can easily scale up and down using OCI CLI. Execute this code block for scaling ADB.

# Replace <INSERT_COMPARTMENT_NAME> with actual compartment name
export compname=<INSERT_COMPARTMENT_NAME>
export Compocid=`oci iam compartment list --compartment-id-in-subtree true --all |jq -r ".data[] | select(.name == \"${compname}\") | .id"`
# Replace <INSERT_DB_NAME> with actual DB name.
export DBname=<INSERT_DB_NAME>
# Save DB OCID in variable based on name(case sensitive)
export ATPocid=`oci db autonomous-database list --compartment-id $Compocid --query "data[?contains(\"display-name\",'$DBname')]"|jq -r ".[].id"`
export WorkReqOCID=`oci db autonomous-database update --compute-count 2 --autonomous-database-id $ATPocid|jq -r '."opc-work-request-id"'`
while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "Scaling request $status"
echo "Ctrl-C to exit"
sleep 10
done
Scaling in progress

Note — For ADB with ECPU model the compute-count parameter must be in multiples of 2 and different from original ECPU value.

Following error is reported if same or invalid CPU count is provided in scaling operation.

ServiceError:
{
"client_version": "Oracle-PythonSDK/2.93.1, Oracle-PythonCLI/3.23.2",
"code": "InvalidParameter",
"logging_tips": "Please run the OCI CLI command using --debug flag to find more debug information.",
"message": "Operation failed. An Autonomous Database ECPU count must be multiple of 2."

Task 3— Delete ADB

Delete is not something you do often. But in today’s cloud world, where resources are treated like cattle instead of pets, you may find yourself doing this often.

Note — Delete operation uses “force” flag. In absence of force flag, the command will prompt you for confirmation.

oci db autonomous-database delete --autonomous-database-id  $ATPocid 
Are you sure you want to delete this resource? [y/N]: y

If you want to capture the workrequest ID as shown in code block below “force” flag is required. You can skip if executing interactively.

# This assumes unique compartment name in tenancy. 
# Adjust for duplicate compartments
# Replace <INSERT_COMPARTMENT_NAME> with actual compartment name
export compname=<INSERT_COMPARTMENT_NAME>
export Compocid=`oci iam compartment list --compartment-id-in-subtree true --all |jq -r ".data[] | select(.name == \"${compname}\") | .id"`
# Replace <INSERT_DB_NAME> with actual DB name.
export DBname=<INSERT_DB_NAME>
# Save DB OCID in variable based on name(case sensitive)
export ATPocid=`oci db autonomous-database list --compartment-id $Compocid --query "data[?contains(\"display-name\",'$DBname')]"|jq -r ".[].id"`
# Delete DB.
# Caution this will delete ADB without warning
export WorkReqOCID=`oci db autonomous-database delete --autonomous-database-id $ATPocid --force |jq -r '."opc-work-request-id"'`
while true
do
export status=`oci work-requests work-request get --work-request-id $WorkReqOCID|jq -r ".data.status"`
echo "DB Deletion is $status"
echo "Ctrl-C to exit"
sleep 10
done

Note — Deletion of Standby fails if primary DB is in stopped state. Start the primary ADB before deleting the standby DB.

For updated code examples bookmark the repo

Leave your feedback and criticism in comments. Like what you see or something you would like to see, say it via comment. Thank you for reading.

--

--

Data Voyage by Jigar Doshi

Master Principal Cloud Architect @ Oracle Singapore. Data enthusiast. Sharing my adventures in world of data