shell常见使用技巧

1、正则表达式判断(bash shell)

if [[ $PARTITION_NAME =~ '^P_((20[0-9]{4})|MAX)$' ]] ; then
echo 'yes'
else
echo 'no'
fi

重点:=~[[]]

2、shell编程读取文件中的行,并循环处理

cat PARTITION_NAME.cnf | while read P_LINE
do
PARTITION_NAME=`echo $P_LINE|awk  '{print $1}'`
echo $PARTITION_NAME
:
do

重点:while read

3.kill每个关键字进程

ps -ef|grep xifenfei|grep -v grep|awk '{print $2}'|xargs kill -9

重点:awk和xargs使用

4.删除几天前文件

find ./ -type f -name "*.txt" -mtime +1|xargs rm -rf
find ./ -type f -name "*.log" -mtime +1 -exec rm -fr {} \;

重点:find/mtime/xargs(exec )

MYSQL修改密码

方法一
(适用于管理员或者有全局权限的用户重设其它用户的密码)
进入命令行模式
mysql -u root mysql
mysql> UPDATE user SET password=PASSWORD(“new password”) WHERE user=’name’;
mysql> FLUSH PRIVILEGES;
mysql> QUIT
方法二
(应用同上,只是方法不同)
mysql -u root mysql
mysql> SET PASSWORD FOR name=PASSWORD(‘new password’);
mysql> QUIT
方法三
mysqladmin -u root “old password” “new password”

ORA-38760: 此数据库实例无法启用闪回数据库

SQL> startup
ORACLE 例程已经启动。
Total System Global Area 368263168 bytes
Fixed Size 1374668 bytes
Variable Size 276825652 bytes
Database Buffers 83886080 bytes
Redo Buffers 6176768 bytes
数据库装载完毕。
ORA-38760: 此数据库实例无法启用闪回数据库
SQL> alter database flashback off;
数据库已更改。
SQL> alter database open;
数据库已更改。
SQL> alter database flashback on;
数据库已更改。
说明:如果出现以下错误,处理如下
SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-38760: This database instance failed to turn on flashback database
SQL> select name from v$restore_point;
If so, drop them:
SQL> drop restore point ;
SQL> alter database flashback off;
.

ORA-01207/ORA-00338恢复

1、模拟环境
拷贝出来控制文件,然后对数据库进行一些操作,然后关闭数据库,使用拷贝出来的控制文件替换原控制文件,启动数据库
出现类此错误:
ORA-00338: 日志 2 (用于线程 1) 比控制文件更新
ORA-00312: 联机日志 2 线程 1: ‘E:\ORACLE\ORADATA\XFF\REDO02’
或者
ORA-01122: 数据库文件 1 验证失败
ORA-01110: 数据文件 1: ‘E:\ORACLE\ORADATA\XFF\SYSTEM01.DBF’
ORA-01207: 文件比控制文件更新 – 旧的控制文件
2、处理思路
1)创建控制文件
1.1)alter database backup controlfile to trace;
1.2)生成控制文件脚本

STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "XFF" NORESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 584
LOGFILE
  GROUP 1 'E:\ORACLE\ORADATA\XFF\REDO01'  SIZE 10M BLOCKSIZE 512,
  GROUP 2 'E:\ORACLE\ORADATA\XFF\REDO02'  SIZE 10M BLOCKSIZE 512,
  GROUP 3 'E:\ORACLE\ORADATA\XFF\REDO03'  SIZE 10M BLOCKSIZE 512
DATAFILE
  'E:\ORACLE\ORADATA\XFF\SYSTEM01.DBF',
  'E:\ORACLE\ORADATA\XFF\SYSAUX01.DBF',
  'E:\ORACLE\ORADATA\XFF\UNDOTBS01.DBF',
  'E:\ORACLE\ORADATA\XFF\USERS01.DBF',
  'E:\ORACLE\ORADATA\XFF\EXAMPLE01.DBF',
  'E:\ORACLE\ORADATA\XFF\O_ORACLE.DBF',
  'E:\ORACLE\ORADATA\XFF\XIFENFEI01.DBF',
  'E:\ORACLE\ORADATA\XFF\P_TEST01.DBF',
  'E:\ORACLE\ORADATA\XFF\SYS_MG01.DBF',
  'E:\ORACLE\ORADATA\XFF\P101.DBF',
  'E:\ORACLE\ORADATA\XFF\P201.DBF',
  'E:\ORACLE\ORADATA\XFF\P301.DBF',
  'E:\ORACLE\ORADATA\XFF\OGG01.DBF',
  'E:\ORACLE\ORADATA\XFF\SPOT01.DBF'
CHARACTER SET ZHS16GBK
;

1.3)关闭数据库
shutdown abort;
1.4)删除现在的控制文件
1.5)执行生成控制文件脚本
2)恢复数据库
recover database;
3)打开数据库
alter database open;
4)添加临时文件
alter tablespace temp add tempfile
‘E:\ORACLE\ORADATA\XFF\TEMP01.DBF’ size 20m reuse;
说明:可以使用备份的控制文件进行恢复
1:startup mount;
2:recover database using backup controlfile until cancel;
然后根据情况,指定archive log和redo file。
3:alter database open resetlogs;

latch: cache buffers chains

当一个数据块读入sga区,相应的buffer header会被放置到hash列表上,我们称其这hash chains,chain在中文的意为链条或串的意思,表达就是关连性.如果一个进程想访问或修改hash chain上的block,它首先要获得”cache buffers chains” latch。
原因一:低效率的SQL语句(主要体现在逻辑读过高)
cache buffers chains latch很大程度与逻辑读有关,所以要观注v$sql中BUFFER_GETS/EXECUTIONS大的语句。
同时每一个逻辑读需要一个latch get 操作及一个cpu操作,这样的sql也会很耗cpu资源。
原因二:热块(访问过于频繁)
找出热点块方法一:

--找出p1raw
select p1,p1raw from v$session_wait where event='latch: cache buffers chains';
--找到对象
SELECT /*+ RULE */
 E.OWNER || '.' || E.SEGMENT_NAME SEGMENT_NAME,
 E.PARTITION_NAME,
 E.EXTENT_ID EXTENT#,
 X.DBABLK - E.BLOCK_ID + 1 BLOCK#,
 X.TCH,
 L.CHILD#
  FROM SYS.V$LATCH_CHILDREN L, SYS.X$BH X, SYS.DBA_EXTENTS E
 WHERE X.HLADDR = '00000002576EE018'--p1raw
   AND E.FILE_ID = X.FILE#
   AND X.HLADDR = L.ADDR
   AND X.DBABLK BETWEEN E.BLOCK_ID AND E.BLOCK_ID + E.BLOCKS - 1
 ORDER BY X.TCH DESC;

找出热点块方法二:

--直接找出热点块
SELECT OBJECT_NAME, SUBOBJECT_NAME
  FROM DBA_OBJECTS
 WHERE DATA_OBJECT_ID IN
       (SELECT DATA_OBJECT_ID
          FROM (SELECT OBJ DATA_OBJECT_ID, FILE#, DBABLK, CLASS, STATE, TCH
                  FROM X$BH
                 WHERE HLADDR IN (SELECT ADDR
                                    FROM (SELECT ADDR
                                            FROM V$LATCH_CHILDREN
                                           ORDER BY (GETS + MISSES + SLEEPS) DESC)
                                   WHERE ROWNUM < 10)
                 ORDER BY TCH DESC)
         WHERE ROWNUM < 10);

Oracle 9I Data Guard配置

1、修改参数文件
primary:
*.standby_archive_dest=’/opt/ora9/oradata/txzldb/stdarch/’
*.log_archive_dest_2=’service=txzldb.Standby optional reopen=60′ –配置tns
*.utl_file_dir=’/opt/ora9′
*.remote_archive_enable=TRUE
*.standby_file_management=AUTO
*.fal_server=’txzldb.Standby’
*.fal_client=’txzldb.Primary’
*.log_archive_dest_state_2=defer
standby:
*.standby_archive_dest=’/opt/ora9/oradata/txzldb/stdarch/’
*.log_archive_dest_2=’service=txzldb.Primary optional reopen=60′ –配置tns
*.utl_file_dir=’/opt/ora9′
*.remote_archive_enable=TRUE
*.standby_file_management=AUTO
*.fal_server=’txzldb.Primary’
*.fal_client=’txzldb.Standby’
*.log_archive_dest_state_2=defer
2、备份主库
backup database format=’/home/oracle/full_backup_%U_%s.bak’;
sql “alter system archive log current”;
–backup filesperset 10 archivelog all format=’/home/oracle/archivlog_%U_%s.bak’;(可选)
alter database create standby controlfile as ‘/home/oracle/control01.ctl’;
3、拷贝密码文件控制文件
scp /opt/oracle/product/9.2.0/dbs/orapwora9i
192.168.3.2:/opt/oracle/product/9.2.0/dbs/
scp control01.ctl
192.168.3.2:/opt/oracle/oradata/ora9i/control01.ctl
4、恢复备机
拷贝备份至备机与主机相同的目录
sqlplus “/ as sysdba”
  startup nomount pfile=”
  alter database mount standby database;
  exit
  rman target / nocatalog
  restore database
–restore archivelog all;(可选)
–recover automatic standby database;(可选)
5、启动恢复状态
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
create spfile from pfile=”;
alter system set log_archive_dest_state_2=enable scope=both;
6、添加临时表空间
recover managed standby database cancel;
alter database open read only;
alter tablespace temp add tempfile ‘/opt/oracle/oradata/ora9i/temp01.dbf’
SIZE 412M AUTOEXTEND ON NEXT 640K MAXSIZE UNLIMITED;
shutdown immediate;
startup nomount pfile=”
alter database mount standby database;
recover managed standby database disconnect from session;

Oracle监听设置密码

LSNRCTL> change_password
Old password:
New password:
Reenter new password:
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ecp-uc-db1)(PORT=1521)))
Password changed for LISTENER
The command completed successfully
LSNRCTL> set password
Password:
The command completed successfully
LSNRCTL> save_config
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ecp-uc-db1)(PORT=1521)))
Saved LISTENER configuration parameters.
Listener Parameter File /opt/oracle/product/10.2.0/db_1/network/admin/listener.ora
Old Parameter File /opt/oracle/product/10.2.0/db_1/network/admin/listener.bak
The command completed successfully
[oracle@ecp-uc-db1 admin]$ cat listener.ora
#—-ADDED BY TNSLSNR 10-JUN-2011 18:13:24—
PASSWORDS_LISTENER = 6D7AA003392C436A
#——————————————–
note:10g数据库上需要上添加(重启监听)
LOCAL_OS_AUTHENTICATION_LISTENER = OFF
1、添加LOCAL_OS_AUTHENTICATION_LISTENER = OFF之前
Security ON: Password or Local OS Authentication
2、添加LOCAL_OS_AUTHENTICATION_LISTENER = OFF之后
Security ON: Password
LSNRCTL> status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ecp-uc-db1)(PORT=1521)))
TNS-01169: The listener has not recognized the password
LSNRCTL> stop
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ecp-uc-db1)(PORT=1521)))
TNS-01169: The listener has not recognized the password
LSNRCTL> set password 123456
The command completed successfully
LSNRCTL> status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ecp-uc-db1)(PORT=1521)))
STATUS of the LISTENER
————————
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.4.0 – Production
Start Date 10-JUN-2011 18:15:49
Uptime 0 days 0 hr. 1 min. 16 sec
Trace Level off
Security ON: Password
SNMP OFF
Listener Parameter File /opt/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File /opt/oracle/product/10.2.0/db_1/network/log/listener.log
Listening Endpoints Summary…
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ECP-UC-DB1)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary…
Service “PLSExtProc” has 1 instance(s).
Instance “PLSExtProc”, status UNKNOWN, has 1 handler(s) for this service…
Service “ecp” has 1 instance(s).
Instance “ecp”, status READY, has 1 handler(s) for this service…
Service “ecpXDB” has 1 instance(s).
Instance “ecp”, status READY, has 1 handler(s) for this service…
Service “ecp_XPT” has 1 instance(s).
Instance “ecp”, status READY, has 1 handler(s) for this service…
The command completed successfully

Oracle 调度程序作业–修改执行时间

--DBMS_SCHEDULER 运行信息
select job_name,state,enabled,to_char(last_start_date,'yyyy-mm-dd hh24:mi:ss'), schedule_name
from dba_scheduler_jobs;
--DBMS_SCHEDULER运行成功与否信息
SELECT log_id, job_name, status,TO_CHAR(ACTUAL_START_DATE,'yyyy-mm-dd HH24:MI:ss') start_date,
           TO_CHAR (log_date, 'yyyy-mm-dd HH24:MI:ss') log_date
      FROM dba_scheduler_job_run_details
     WHERE job_name = 'GATHER_STATS_JOB'
     order by 4 DESC;
--查询执行时间情况
select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2
 where t1.window_name=t2.window_name and t2.window_group_name='MAINTENANCE_WINDOW_GROUP';
--修改执行时间
begin
dbms_scheduler.set_attribute('WEEKEND_WINDOW','REPEAT_INTERVAL','freq=daily;byday=SAT;byhour=0;byminute=0;bysecond=0');
dbms_scheduler.set_attribute('WEEKEND_WINDOW','DURATION','+002 00:00:00');
end;

ORA-01410: invalid ROWID 分析

1、跟踪错误表
alter session set max_dump_file_size=unlimited;
alter session set db_file_multiblock_read_count=1;
alter session set events ‘10200 trace name context forever, level 1’;
执行下面语句
SELECT COUNT(*) FROM QXTDEV1.TAB_SMS_MT_DOWN
报错
ORA-01410: invalid ROWID
alter session set events ‘10200 trace name context off’;
2、查看跟踪文件尾部
Consistent read started for block 0 : 00400652
env: (scn: 0x0abf.1ee04657 xid: 0x0000.000.00000000 uba: 0x00000000.0000.00 statement num=0 parent xid: xid: 0x0000.000.00000000 scn: 0x0000.00000000 0sch: scn: 0x0000.00000000)
Consistent read finished for block 0 : 400652
Consistent read finished for block 0 : 400652
通过这个文件看到文件读到400652的块的时候终止了,错误应该在这块上
3、分析400652块内容
–转换为10进制
SELECT to_number(‘400652′,’xxxxxxxx’) FROM dual;
–求文件号和块号
SELECT DBMS_UTILITY.DATA_BLOCK_ADDRESS_FILE(4195922),
DBMS_UTILITY.DATA_BLOCK_ADDRESS_BLOCK(4195922)
FROM DUAL;
4、dump文件号为1,块号为1618的内容
alter system dump datafile 1 block 1618;
Block header dump: 0x00400652
Object id on Block? Y
seg/obj: 0xde csc: 0xabf.1e400c0c itc: 2 flg: O typ: 1 – DATA
fsl: 0 fnx: 0x0 ver: 0x01
得到object_id为de,转化为十进制
SELECT to_number(‘de’,’xx’) FROM dual;
查询all_objects表
SELECT * FROM All_Objects WHERE object_id=222;
发现这个数据库(绝对块号400652,相对:文件号1,块号1618)指向的表为dual
5、得出结论
我们的QXTDEV1.TAB_SMS_MT_DOWN表中的数据块指向了sys.dual表,从而出现了ORA-01410错误

undo损坏恢复–有事务

startup nomount;
create pfile=’/tmp/pfile’ from spfile;
修改pfile
*._allow_resetlogs_corruption=true
*._corrupted_rollback_segments=(_SYSSMU1$,_SYSSMU2$,_SYSSMU3$,_SYSSMU4$,
_SYSSMU5$,_SYSSMU6$,_SYSSMU7$,_SYSSMU8$,_SYSSMU9$,_SYSSMU10$)
startup pfile=’/tmp/pfile’ mount;
–undo数据文件不用离线也可以打开的时候(2),不行采用下面的方法
alter database datafile 2 offline drop;
alter database open;
CREATE UNDO TABLESPACE UNDOTBS01
DATAFILE ‘/opt/oracle/oradata/xifenfei/UNDOTBS01.dbf’ SIZE 50M;
select segment_name,status from dba_rollback_segs;–查看是否有回滚段被创建,处于offline状态
shutdown immediate;
修改pfile:
*.undo_tablespace=’UNDOTBS01′
startup pfile=’/tmp/pfile’
–alter tablespace undotbs1 offline normal;(2)–使数据文件先离线,如果开始已经离线,这里不需要处理
select segment_name,status from dba_rollback_segs;
drop rollback segment “_SYSSMUx$”;
drop tablespace UNDOTBS02 including contents and datafiles;
select * from v$recover_file;–验证是否还有文件需要恢复
shutdown immediate
修改参数文件
#*._allow_resetlogs_corruption=true
#*._corrupted_rollback_segments=(_SYSSMU1$,_SYSSMU2$,_SYSSMU3$,_SYSSMU4$,_SYSSMU5$,
_SYSSMU6$,_SYSSMU7$,_SYSSMU8$,_SYSSMU9$,_SYSSMU10$)
startup pfile=’/tmp/pfile’;
create spfile from pfile=’/tmp/pfile’;