如何在Ubuntu上监控Oracle数据库资源使用情况
在Ubuntu上监控Oracle数据库前,需完成以下基础配置:
sudo apt update
sudo apt install libaio1
wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linux.x64-21.9.0.0.0dbru.zip
unzip instantclient-basiclite-linux.x64-21.9.0.0.0dbru.zip
export LD_LIBRARY_PATH=$PWD/instantclient_21_9 # 临时生效,建议写入~/.bashrc永久生效
sqlplus工具连接数据库。SELECT_CATALOG_ROLE角色的账号(如SYSDBA),用于查询性能视图。使用SQL*Plus登录数据库(sysdba权限),执行以下命令检查实例是否运行:
SELECT status FROM v$instance;
-- 返回"OPEN"表示实例正常运行
统计当前数据库会话数量,识别并发连接是否异常:
SELECT COUNT(*) FROM v$session;
查询执行次数多或耗时长的SQL,定位性能瓶颈:
SELECT sql_text, executions, elapsed_time/1000000 AS elapsed_sec
FROM v$sql
WHERE executions > 100 -- 筛选执行次数较多的SQL
ORDER BY elapsed_sec DESC FETCH FIRST 10 ROWS ONLY;
查看会话等待事件,识别资源瓶颈(如I/O、锁等待):
SELECT event, wait_class, COUNT(*) AS wait_count
FROM v$session_wait
WHERE wait_class != 'Idle'
GROUP BY event, wait_class
ORDER BY wait_count DESC;
监控表空间占用情况,避免空间耗尽导致数据库故障:
SELECT tablespace_name,
ROUND((used_space * 8192)/1024/1024, 2) AS used_mb,
ROUND((tablespace_size * 8192)/1024/1024, 2) AS total_mb,
ROUND(used_percent, 2) AS used_percent
FROM dba_tablespace_usage_metrics;
使用系统工具监控Oracle进程的CPU、内存占用:
top -u oracle # 查看Oracle用户进程的资源占用
vmstat 1 5 # 监控虚拟内存、进程、I/O等统计信息(1秒采样,共5次)
iostat -x 1 5 # 监控磁盘I/O性能(需安装sysstat包)
分析数据库错误和警告信息:
tail -f $ORACLE_BASE/diag/rdbms/<db_name>/<instance_name>/trace/alert_<instance_name>.log # 实时查看alert日志
less $ORACLE_HOME/network/log/listener.log # 查看监听器日志
sudo apt install golang
git clone https://github.com/iamseth/oracledb_exporter.git
cd oracledb_exporter
go build
config.yml(添加Oracle连接信息):datasource:
host: "localhost"
port: 1521
service_name: "orcl"
username: "monitor_user"
password: "monitor_password"
./oracledb_exporter -config.config.yml
prometheus.yml):scrape_configs:
- job_name: 'oracle'
static_configs:
- targets: ['localhost:9161'] # Oracle Exporter默认端口
Oracle Database Monitoring模板)。sudo apt install zabbix-server-pgsql zabbix-frontend-php zabbix-apache-conf zabbix-agent
Zabbix Oracle Monitoring Template),配置Oracle连接参数(用户名、密码、SID)。@?/rdbms/admin/awrrpt.sql -- 交互式脚本,选择时间段和报告类型
SELECT * FROM TABLE(DBMS_WORKLOAD_REPOSITORY.awr_report_html(
l_dbid => (SELECT dbid FROM v$database),
l_inst_num => (SELECT instance_number FROM v$instance),
l_bid => (SELECT min(snap_id) FROM dba_hist_snapshot WHERE begin_interval_time > SYSDATE-1),
l_eid => (SELECT max(snap_id) FROM dba_hist_snapshot)
));
db file sequential read表示I/O瓶颈)、锁等待、表空间碎片。