温馨提示×

怎样在Ubuntu上配置Oracle Data Guard

小樊
56
2025-09-23 11:24:03
栏目: 云计算

Configuring Oracle Data Guard on Ubuntu: A Step-by-Step Guide
Oracle Data Guard is a robust high-availability solution that maintains one or more standby databases to protect against data loss, disasters, and corruption. Below is a structured guide to setting it up on Ubuntu, covering prerequisites, physical standby creation, and essential configurations.


1. Prerequisites

Before starting, ensure the following requirements are met:

  • Environment Consistency: Primary and standby servers should have identical Ubuntu versions (e.g., 22.04/24.04), Oracle Database versions (e.g., 19C/21C), hardware configurations (CPU, memory), and storage setups.
  • Network Stability: A low-latency, high-bandwidth network connects the primary and standby servers. Configure static IP addresses and open required ports (default: 1521 for SQL*Net, 443 for Oracle Enterprise Manager).
  • TNS Configuration: Create tnsnames.ora entries on both servers to resolve database aliases. For example:
    PRIMARY_DB =
      (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = primary-server-ip)(PORT = 1521))
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = primary_db)
        )
      )
    STANDBY_DB =
      (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = standby-server-ip)(PORT = 1521))
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = standby_db)
        )
      )
    
    Place this file in $ORACLE_HOME/network/admin on both servers.
  • Archive Log Mode: The primary database must run in ARCHIVELOG mode to enable redo log archiving (a Data Guard requirement). Verify with:
    SELECT log_mode FROM v$database;
    
    If not in ARCHIVELOG mode, enable it:
    SHUTDOWN IMMEDIATE;
    STARTUP MOUNT;
    ALTER DATABASE ARCHIVELOG;
    ALTER DATABASE OPEN;
    
  • Storage Space: Ensure sufficient disk space for redo logs, standby redo logs, and backup files on both servers.

2. Prepare the Primary Database

2.1 Enable Forced Logging

Forced logging ensures all changes (including direct-path inserts) are logged for Data Guard replication. Run:

ALTER DATABASE FORCE LOGGING;

Verify with:

SELECT force_logging FROM v$database;

(Expected result: YES).

2.2 Create a Standby Redo Log Group

Standby redo logs store redo data received from the primary database. The number of groups should equal the number of online redo log groups plus one. Check existing redo logs:

SELECT group#, bytes, members FROM v$log;

Create standby redo logs (example for 3 groups of 500MB each):

ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/primary/standby_redo04.log') SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/primary/standby_redo05.log') SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/primary/standby_redo06.log') SIZE 500M;

Confirm creation:

SELECT group#, type, bytes FROM v$standby_log;
```.

#### **2.3 Back Up the Primary Database**  
Use RMAN (Recovery Manager) to create a full backup of the primary database. This backup will be used to create the standby database. Example:  
```bash
rman target /
RUN {
  BACKUP AS BACKUPSET DATABASE PLUS ARCHIVELOG;
  BACKUP CURRENT CONTROLFILE FOR STANDBY;
}

Copy the backup files (e.g., datafiles, control files, archived logs) to the standby server using scp or a shared storage.


3. Create the Physical Standby Database

A physical standby database is an exact block-for-block copy of the primary database, applying redo logs in real-time.

3.1 Copy Backup Files to Standby Server

Transfer the primary database backup (from Step 2.3) to the standby server’s Oracle data directory (e.g., /u01/app/oracle/oradata/standby). Use scp for secure transfer:

scp -r /u01/app/oracle/backup/* standby-server:/u01/app/oracle/oradata/standby/
```.

#### **3.2 Restore the Standby Database**  
On the standby server, start RMAN and restore the database from the primary backup:  
```bash
rman target /
RUN {
  RESTORE DATABASE;
  RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
}

The RECOVER MANAGED STANDBY command applies redo logs to keep the standby synchronized.

3.3 Configure Initialization Parameters

Modify the standby database’s spfile (or pfile) to enable Data Guard. Add/modify these parameters in $ORACLE_HOME/dbs/initstandby.ora:

db_unique_name=standby_db
log_archive_config='DG_CONFIG=(primary_db,standby_db)'
log_archive_dest_1='LOCATION=/u01/app/oracle/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby_db'
log_archive_dest_2='SERVICE=primary_db ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary_db'
fal_server=primary_db
standby_file_management=auto

Restart the standby database to apply changes:

SHUTDOWN IMMEDIATE;
STARTUP NOMOUNT;
```.

#### **3.4 Start the Managed Recovery Process (MRP)**  
On the standby server, connect to the standby instance and start MRP to apply redo logs:  
```sql
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

Check MRP status with:

SELECT process, status, sequence# FROM v$managed_standby;

(Expected: Processes like MRP0 running, indicating redo log application).


4. Configure Data Guard Broker (Optional but Recommended)

The Data Guard Broker simplifies management via the dgmgrl command-line tool.

4.1 Enable Broker on Both Servers

Add these parameters to both primary and standby spfiles:

dg_broker_start=true

Restart the databases to apply changes.

4.2 Create a Data Guard Configuration

Connect to the primary database using dgmgrl (run as the Oracle user):

dgmgrl sys@primary_db

Enter the password when prompted. Then, create the configuration:

CREATE CONFIGURATION my_dg_config AS
  PRIMARY DATABASE IS "primary_db"
  CONNECT IDENTIFIER IS primary_db;

Add the standby database:

ADD DATABASE "standby_db" AS
  CONNECT IDENTIFIER IS standby_db;

Enable the configuration:

ENABLE CONFIGURATION;

Verify the setup:

SHOW CONFIGURATION;
```.


---

### **5. Validate the Setup**  
Ensure Data Guard is working correctly:  
- **Check Redo Apply Status**: On the standby server, query `v$managed_standby` to confirm MRP is running.  
- **Test Log Transport**: Insert data into a table on the primary database and verify it appears on the standby after a few seconds.  
- **Monitor Performance**: Use `v$dataguard_stats` to check latency and synchronization status.


---

### **6. Maintenance and Troubleshooting**  
- **Switchovers/Failovers**: Use `dgmgrl` to perform planned switchovers (for maintenance) or failovers (for disasters). Example:  
  ```sql
  SWITCHOVER TO standby_db;
  • Backup Standby: Regularly back up the standby database to avoid data loss.
  • Monitor Alerts: Set up Oracle Enterprise Manager or custom scripts to alert on MRP failures, redo transport issues, or high latency.

By following these steps, you can configure Oracle Data Guard on Ubuntu to achieve high availability and disaster recovery for your Oracle Database. Always refer to the latest Oracle documentation for version-specific updates.

0