oracle运维(持续更新)
oracle运维
简单命令使用
这个简单了解就可以,我是从网上和书本收集而来。(我自己没怎么用过)
进入SQL*Plus
$sqlplus 用户名/密码
退出SQL*Plus
SQL>exit
在sqlplus下得到帮助信息
列出全部SQL 命令和SQL*Plus 命令
SQL>help
列出某个特定的命令的信息
SQL>help 命令名
显示表结构命令 DESCRIBE
SQL>DESC 表名
SQL*Plus 中的编辑命令
显示SQL缓冲区命令
SQL>L
修改SQL 命令
首先要将待改正行变为当前行
SQL>n
用CHANGE 命令修改内容
SQL>c/旧/新
重新确认是否已正常
SQL>L
使用INPUT 命令可以在SQL 缓冲区中增加一行或多行
SQL>i
SQL>输入内容
调用外部系统编辑器
SQL>edit 文件名
可以使用DEFINE命令设置系统变量EDITOR 来改变文本编辑器的类型,在login.sql文件中定义如下一行
DEFINE_EDITOR=vi
运行命令文件
SQL>START test
SQL>@test
关于侦听
1、新建/修改/删除侦听
以oracle 用户登录,运行netca,会跳出图形配置界面。
2、打开侦听
lsnrctl start
3、查看侦听
Lsnrctl status
4、关闭侦听
lsnrctl stop
ORACLE的启动和关闭
在单机环境下
要想启动或关闭ORACLE系统必须首先切换到ORACLE用户下,如下
su -oracle
启动oracle 数据库命令
$sqlplus /nolog
已连接到空闲例程
SQL> conn / as sysdba
ORACLE 例程已经启动
SQL>startup
关闭oracle 数据库命令:
$sqlplus /nolog
连接
SQL> conn / as sysdba
关闭数据库
SQL> shutdown immediate
数据库管理员日常工作
检查alterSID.log
这个日志文件位于参数BACKGROUND_DUMP_DEST 指定的目录,可能通过以下
命令来查看。
SQL> SHOW PARAMETER background_dump_dest
在出现大故障前,数据库有可能会报一些警告或错误信息,应该充分重视这
些信息,未雨绸缪,避免更大错误的发生。
检查alterSID.log 的什么内容。
👊 检查数据库是否出现过宕机(可能在晚间重启而维护人员不知道)
👊 Oracle 出错信息,通过$grep ORA- alterSID.log 查找
👊 产品有关的问题:ORA-00600/ORA-07445 等错误
👊 相应的TRACE 文件
环境确认
数据库实例是否正常工作
SQL > select status from v$instance;
数据库监听器是否正常工作
- $ lsnrctl status
是否存在故障表空间
- SQL> select tablespace_name,status from dba_tablespace;
控制文件、日志文件是否正常
SQL> select * from v$controlfile;
SQL> select * from v$log;
SQL> select * from v$logfile;
性能监测
👊 每天按业务峰值情况,对数据库性能数据进行定时采集
👊每天检查数据库的主要性能指标
👊每天检查最消耗资源的SQL 语句变化情况。
👊每天检查是否有足够的资源
👊检查所有表空间的剩余情况
👊识别出一些异常的增长
👊检查CPU、内存、网络等是否异常
数据库日常操作SQL
查看表空间物理文件的名称及大小
select tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) filesize
from dba_data_files
order by tablespace_name;
查看表空间使用情况
select a.tablespace_name "表空间名称",
100 - round((nvl(b.bytes_free, 0) / a.bytes_alloc) * 100, 2) "
占用率(%)",
round(a.bytes_alloc / 1024 / 1024, 2) "容量(M)",
round(nvl(b.bytes_free, 0) / 1024 / 1024, 2) "空闲(M)",
round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024, 2)
"使用(M)",
to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') "采样时间"
from (select f.tablespace_name,
sum(f.bytes) bytes_alloc,
sum(decode(f.autoextensible, 'YES', f.maxbytes, 'NO',
f.bytes)) maxbytes from dba_data_files f
group by tablespace_name) a,
(select f.tablespace_name, sum(f.bytes) bytes_free
from dba_free_space f
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name
order by 2 desc;
查看表空间的碎片程度
select tablespace_name, count(tablespace_name)
from dba_free_space
group by tablespace_name
having count(tablespace_name) > 10;
alter tablespace HS_USER_DATA coalesce;
alter table name deallocate unused;
碎片程度
select tablespace_name, count(tablespace_name)
from dba_free_space
group by tablespace_name
having count(tablespace_name) > 10;
alter tablespace name coalesce;
alter table name deallocate unused;
create or replace view ts_blocks_v as
select tablespace_name, block_id, bytes, blocks, segment_name
from dba_free_space
union all
select tablespace_name, block_id, bytes, blocks, segment_name
from dba_extents;
select * from ts_blocks_v;
select tablespace_name, sum(bytes), max(bytes), count(block_id)
from dba_free_space
group by tablespace_name;
👊 查看碎片程度高的表
SELECT segment_name table_name, COUNT(*) extents
FROM dba_segments
WHERE owner NOT IN ('SYS', 'SYSTEM')
GROUP BY segment_name
HAVING COUNT(*) = (SELECT MAX(COUNT(*))
FROM dba_segments
GROUP BY segment_name);
查看回滚段名称及大小
select segment_name,
tablespace_name,
r.status,
(initial_extent / 1024) InitialExtent,
(next_extent / 1024) NextExtent,
max_extents,
v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name;
查看控制文件
select name from v$controlfile;
查看日志文件
select member from v$logfile;
查看表空间的使用情况
select sum(bytes) / (1024 * 1024) as free_space, tablespace_name
from dba_free_space
group by tablespace_name;
SELECT A.TABLESPACE_NAME,
A.BYTES TOTAL,
B.BYTES USED,
C.BYTES FREE,
(B.BYTES * 100) / A.BYTES "% USED",
(C.BYTES * 100) / A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A, SYS.SM$TS_USED B, SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME
AND A.TABLESPACE_NAME = C.TABLESPACE_NAME;
查看数据库对象
select owner, object_type, status, count(*) count#
from all_objects
group by owner, object_type, status;
查看数据库的版本
Select version
FROM Product_component_version
Where SUBSTR(PRODUCT, 1, 6) = 'Oracle';
查看Oracle字符集
select * from sys.props$ where name = 'NLS_CHARACTERSET';
在某个用户下找所有的索引
select user_indexes.table_name,
user_indexes.index_name,
uniqueness,
column_name
from user_ind_columns, user_indexes
where user_ind_columns.index_name = user_indexes.index_name
and user_ind_columns.table_name = user_indexes.table_name
order by user_indexes.table_type,
user_indexes.table_name,
user_indexes.index_name,
column_position;
表、索引的存储情况检查
select segment_name, sum(bytes), count(*) ext_quan
from dba_extents
where tablespace_name = '&tablespace_name'
and segment_type = 'TABLE'
group by tablespace_name, segment_name;
select segment_name, count(*)
from dba_extents
where segment_type = 'INDEX'
and owner = '&owner'
group by segment_name;
查看数据库的创建日期和归档方式
Select Created, Log_Mode, Log_Mode From V$Database;
显示所有数据库对象的类别和大小
select type,
count(name) num_instances,
sum(source_size) source_size,
sum(parsed_size) parsed_size,
sum(code_size) code_size,
sum(error_size) error_size,
sum(source_size) + sum(parsed_size) + sum(code_size) +
sum(error_size) size_required
from dba_object_size
group by type
order by 1;
RAC启动归档
步骤:
- 以SYSDBA 身份登陆2 个节点,执行
alter system set cluster_database=false scope =spfile sid=’*’;
设置归档路径
alter system set log_archive_start=true scope=spfile;
- 2 个节点
shutdown immediate
- 在一个节点上执行
startup mount
alter database archivelog;
shutdown immediate;
alter database open;
alter system set cluster_database=true scope =spfile sid=’*’;
shutdown immediate
4、分别启动2 个节点,修改完毕
Troubleshooting
监控事务的等待
select event,
sum(decode(wait_Time, 0, 0, 1)) "Prev",
sum(decode(wait_Time, 0, 1, 0)) "Curr",
count(*) "Totol"
from v$session_Wait
group by event
order by 4;
查看一些等待信息
select sid, event
from v$session_wait
where event not like 'SQL%'
and event not like '%ipc%';
查看是否存在下面等常见的等待事件:
👊buffer busy waits,
👊free buffer waits,
👊db file sequential read,
👊db file scattered read,
👊enqueue,latch free,
👊log file parallel write,
👊log file sync
查看等待情况
SELECT v$waitstat.class,
v$waitstat.count count,
SUM(v$sysstat.value) sum_value
FROM v$waitstat, v$sysstat
WHERE v$sysstat.name IN ('db block gets', 'consistent gets')
group by v$waitstat.class, v$waitstat.count;
回滚段查看
select rownum,
sys.dba_rollback_segs.segment_name Name,
v$rollstat.extents Extents,
v$rollstat.rssize Size_in_Bytes,
v$rollstat.xacts XActs,
v$rollstat.gets Gets,
v$rollstat.waits Waits,
v$rollstat.writes Writes,
sys.dba_rollback_segs.status status
from v$rollstat, sys.dba_rollback_segs, v$rollname
where v$rollname.name(+) = sys.dba_rollback_segs.segment_name
and v$rollstat.usn(+) = v$rollname.usn
order by rownum;
回滚段的争用情况
select name, waits, gets, waits / gets "Ratio"
from v$rollstat a, v$rollname b
where a.usn = b.usn;
监控表空间的 I/O 比例
select df.tablespace_name name,
df.file_name "file",
f.phyrds pyr,
f.phyblkrd pbr,
f.phywrts pyw,
f.phyblkwrt pbw
from v$filestat f, dba_data_files df
where f.file# = df.file_id
order by df.tablespace_name;
监控文件系统的I/O比例
select substr(a.file#, 1, 2) "#",
substr(a.name, 1, 30) "Name",
a.status,
a.bytes,
b.phyrds,
b.phywrts
from v$datafile a, v$filestat b
where a.file# = b.file#;
监控SGA 的命中率
select a.value + b.value "logical_reads",
c.value "phys_reads",
round(100 * ((a.value + b.value) - c.value) / (a.value + b.value))
"BUFFER HIT RATIO"
from v$sysstat a, v$sysstat b, v$sysstat c
where a.statistic# = 38 -- physical read total multi block requests
and b.statistic# = 39 -- physical read total bytes
and c.statistic# = 40; -- physical write total IO requests