Oracle common operation and maintenance commands

One, Oracle build and delete database commands

(1)oracle11g

Build database (generally it is customary to configure gdbname and sid name to be the same, sys password to be the same as system password to facilitate memory)

[oracledb@ ~]$ dbca -silent -createDatabase -templateName /u01/oracle/product/11.2.0/dbhome_1/assistants/dbca/templates/General_Purpose.dbc -gdbname GDBNAME -sid SIDNAME -characterSet AL32UTF8 -NATIONALCHARACTERSET UTF8 -sysPassword SYSPASSWORD -systemPassword SYSTEMPASSWORD -TOTALMEMORY 2048

Delete library method one:

[oracledb@ ~]$ dbca -silent -deleteDatabase -sourceDB  SIDNAME -sysDBAUserName sys -sysDBAPassword SYSPASSWORD

Delete library method two:

#步骤1:配置回应文件:
[oracledb@ ~]$ cat /u01/oracle/response/dbca.rsp
OPERATION_TYPE = "deleteDatabase"
SOURCEDB = "SIDNAME"
SYSDBAUSERNAME = "sys"
SYSDBAPASSWORD = "SYSPASSWORD"

#步骤2:执行回应文件删库:
[oracledb@ ~]$ dbca -silent -responseFile /u01/oracle/response/dbca.rsp

(2) Oracle12c build database

[oracledb@ ~]$ dbca -silent -createDatabase -templateName /u01/oracle/product/orahome/assistants/dbca/templates/General_Purpose.dbc  -gdbname GDBNAME -sid SIDNAME -characterSet AL32UTF8 -NATIONALCHARACTERSET UTF8 -sysPassword SYSPASSWORD -systemPassword SYSTEMPASSWORD -TOTALMEMORY 2048
#删库命令同上,注意运行删库命令之后,需要手动删除遗留的目录

(3) Deleting generally automatically deletes the library information in the following path or file

A:/u01/oracle/admin/SIDNAME
B:cat /etc/oratab
C:/u01/oracle/oradata/SIDNAME

The following paths need to be manually cleaned up

D:/u01/oracle/cfgtoollogs/dbca/SIDNAME
E:/u01/oracle/diag/rdbms/SIDNAME
F:/u01/oracle/product/11.2.0/dbhome_1/dbs/hc_SIDNAME.dat

2. Create the account password corresponding to the library

1. Switch SID

[oracledb@ ~]$ export ORACLE_SID=SIDNAME

2. Switch character set

#查看oracle数据库的字符集
SQL> select userenv('language') from dual;

#查看oracle数据库的编码
SQL> select * from nls_database_parameters where parameter ='NLS_CHARACTERSET';
[oracledb@ ~]$ set NLS_LANG=AMERICAN_AMERICA.AL32UTF8     #windows_os
[oracledb@ ~]$ export NLS_LANG=AMERICAN_AMERICA.AL32UTF8  #linux_os

3. Create user information corresponding to the library

SQL> create temporary tablespace SIDNAME_temp tempfile '/u01/oracle/oradata/SIDNAME/SIDNAME_temp.dbf 'size 64m autoextend on next 64m maxsize unlimited extent management local;

SQL> create tablespace SIDNAME_data logging datafile '/u01/oracle/oradata/SIDNAME/SIDNAME_data.dbf' size 64m autoextend on next 64m maxsize 2048m extent management local;

SQL> create user USERNAME identified by USERPASSWORD default tablespace SIDNAME_data temporary tablespace SIDNAME_temp;

SQL> grant connect,resource to USERNAME;
SQL> grant create view to USERNAME;
SQL> grant unlimited tablespace to USERNAME;

SQL> grant create public synonym to USERNAME;
SQL> grant drop public synonym to USERNAME;

SQL> create or replace directory dir_dump  as '/u01/oracle/backup';
SQL> grant read,write on directory dir_dump to USERNAME;

SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

-Modify the process and sessions parameter values ​​according to the instance environment (requires restarting the oracle database)

SQL> alter system set processes=1000 scope=spfile;  
SQL> alter system set sessions=1105 scope=spfile; 

Knowledge points:

  • oracle11g: The definition of sessions value should be greater than or set at 1.1processes+5. If it is less than 1.1processes+5, oracle will automatically set this parameter to 1.1processes+5 when it is started. d================================================================================================================================================================================================================================================== This is mainly considering the session initiated by the background process and about 10% of the recursive session.
  • oracle12c:1.1processes+22
#查询当前oracle的并发连接数:
SQL> select count(*) from v$session where status='ACTIVE';
#查看不同用户的连接数:
SQL> select username,count(username) from v$session where username is not null group by username;
#查看所有用户:
select * from all_users;
#当前的连接数
select count(*) from v$process;
#数据库允许的最大连接数
select value from v$parameter where name = 'processes';

Three, database restore and backup commands

View expdp export backup storage path:

sql> select * from dba_directories;

1. Backup and restore operations for the entire library

  • (1) Database backup (note that sometimes SIDNAME and SCHEMASNAME are inconsistent. Pay attention to the distinction when using. The general configuration is the same name, which is convenient for memory and operation and maintenance. The parallel parameter is added with a reasonable value according to the configuration of server memory and other conditions. )
#备份:
[oracledb@ ~]$  expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME dumpfile=SIDNAME`date +%Y%m%d`.dmp directory=dir_dump parallel=2
12
  • (2) Restore
    Case 1. The original database and the target database instance names are different (note that the data tablespace name of some environments is not SIDNAME_data, please check when using it)
#格式:
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DUMPFILE=XXXX.dmp  DIRECTORY=dir_dump remap_schema=源SCHEMASNAME:目标SCHEMASNAME remap_tablespace=源_data:目标_data

Case 2: The instance name of the source library and the target library are the same

#格式:
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DUMPFILE=XXXX.dmp  DIRECTORY=dir_dump EXCLUDE=STATISTICS 

Knowledge expansion: use EXCLUDE=STATISTICS to exclude statistics when restoring, you can use the following command to complete statistics

#命令如下:
SQL> exec dbms_stats.gather_schema_stats(ownname=>'SIDNAME',estimate_percent=>10,degree=>8,cascade=>true,granularity=>'ALL');

2. For single table backup and restore operations

  • (1), backup single table
格式:
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME dumpfile=tablenameXXXX.dmp DIRECTORY=dir_dump tables=TABLENAME
  • (2) Restore the single table
#格式:
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME dumpfile=tablenameXXXX.dmp DIRECTORY=dir_dump TABLES=TABLENAME TABLE_EXISTS_ACTION=REPLACE

Extension: table_exists_action parameter description

使用imp进行数据导入时,若表已经存在,要先drop掉表,再进行导入。
而使用impdp完成数据库导入时,若表已经存在,有四种的处理方式:
参数(1) skip:默认操作
参数(2) replace:先drop表,然后创建表,最后插入数据
参数(3) append:在原来数据的基础上增加数据
参数(4) truncate:先truncate,然后再插入数据
  • (3) Back up multiple tables
#格式:
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME dumpfile=tablenameXXXX.dmp DIRECTORY=dir_dump TABLES=源TABLENAME1,源TABLENAME2,.....
  • (4) Restore multiple tables
#格式:
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME dumpfile=tablenameXXXX.dmp DIRECTORY=dir_dump remap_table=源TABLENAME1:目标TABLENAME11 TABLE_EXISTS_ACTION=REPLACE
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME dumpfile=tablenameXXXX.dmp DIRECTORY=dir_dump remap_table=源TABLENAME2:目标TABLENAME22 TABLE_EXISTS_ACTION=REPLACE

3. Expand knowledge

  • 1) Extension 1:

Case 1. When exporting and restoring from a higher version to a lower version, for example, when restoring from 12 to 11, when performing the export in 12c, add the version number of the lower version version=11.1.0.2.0

#格式:
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME dumpfile=XXX.dmp DIRECTORY=dir_dump version=11.1.0.2.0

Case 2. When the lower version is restored to the higher version, the higher version is generally compatible with the lower version. At present, there is no problem with the restoration of 11 to 12 in the personal operation and maintenance work.

  • 2) Extension 2:

(1) Backup according to the specified size, such as 5G size per copy (parallel is multi-threaded processing, the number of threads should be less than the number of generated files, and the number of threads should be less than the number of cpu threads)

#备份格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX_%U.dmp logfile=expdpXXX.log filesize=5G parallel=16

(2) Restore multiple backup files:

#还原格式
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX_%U.dmp logfile=impdpXXX.log parallel=16

3) Extension 3

Export filter does not export a table:

#格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX.dmp  exclude=TABLE:\"IN \'TABLENAME\'\" 

4) Extension 4

Use the truncate parameter without changing the data structure when restoring different libraries:

#格式
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX.dmp remap_schema=源SCHEMASNAME:目标SCHEMASNAME remap_tablespace=源_data:目标_data TABLE_EXISTS_ACTION=truncate

5) Expansion 5

Keep the table and clear the table data when exporting backup (query parameter):

#格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX.dmp logfile=expdpXXX.log  query=TABLENAME1:'" where 1=2"',TABLENAME2:'" where 1=2"',........

6) Extension 6

Keep tables when exporting backups, clear table data, and filter two tables at the same time

#格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX.dmp logfile=expdpXXX.log  query=TABLENAME1:'" where 1=2"',TABLENAME2:'" where 1=2"',........

6) Extension 6

Keep tables when exporting backups, clear table data, and filter two tables at the same time

#格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump dumpfile=XXX.dmp logfile=expdpXXX.log  query=TABLENAME1:'" where 1=2"',TABLENAME2:'" where 1=2"' exclude=TABLE:\"IN \'TABLENAME1\'\'TABLENAME2\'\"

7) Extension 7

Only statistics the data of each table in the database, but does not export, the parameter estimate_only=y

#格式
[oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME DIRECTORY=dir_dump estimate_only=y

8) Expansion 8

Import a single table to a temporary table:

#格式(涉及统计与索引时添加参数EXCLUDE=STATISTICS EXCLUDE=INDEX)
—sql实现从这张表复制到另一张临时表

SQL> CREATE TABLE 目标TABLENAME  AS (SELECT * FROM 源TABLENAME);
—清空表中的数据

SQL> delete  from 目标TABLENAME;
[oracledb@ ~]$ impdp USERNAME/USERPASSWORD@SIDNAME DIRECTORY=dir_dump DUMPFILE=tablenameXXX.dmp remap_table=源TABLENAME:目标TABLENAME TABLE_EXISTS_ACTION=REPLACE EXCLUDE=STATISTICS EXCLUDE=INDEX
 [oracledb@ ~]$ expdp USERNAME/USERPASSWORD@SIDNAME schemas=SCHEMASNAME ESTIMATE_ONLY=y NOLOGFILE=y FULL=y

9) Extension 9

The content of individual sql scripts is very long. When executed with PLSQL Developer tool, it will always be stuck and there is no response. In addition to executing in the command window with PLSQL Developer tool, you can also use the shell terminal to execute

#格式
[oracle@localhost ~]$ export ORACLE_SID=SIDNAME
[oracle@localhost ~]$ sqlplus  / as sysdba
sql> conn USERNAME/USERPASSWORD@SIDNAME
sql> @/u01/oracle/backup/XXX.sql

10) Extension 10

How to terminate expdp and impdp tasks correctly, the steps are as follows:

步骤1、查看视图dba_datapump_jobs

select job_name,state from dba_datapump_jobs;
步骤2、正确停止expdp导出任务使用stop_job

expdp USERNAME/USERPASSWORD@SIDNAME attach=SYS_EXPORT_SCHEMA_02
步骤3、停止任务

Export> stop_job=immediate 
Are you sure you wish to stop this job ([yes]/no): yes  
步骤4、查看系统中的备份job状态

select owner_name,job_name ,state from dba_datapump_jobs;

Extension: The following commands are valid in interactive mode:

HELP: 总结交互命令。
KILL_JOB: 分离和删除作业。
PARALLEL: 更改当前作业的活动 worker 的数目。
PARALLEL=.2
START_JOB: 启动/恢复当前作业。
START_JOB=SKIP_CURRENT 在开始作业之前将跳过作业停止时执行的任意操作。
STATUS :在默认值(0)将显示可用时的新状态的情况下,要监视的频率(以秒计)作业状态。
STATUS[=interval]
STOP_JOB: 顺序关闭执行的作业并退出客户机。
STOP_JOB=IMMEDIATE 将立即关闭数据泵作业。

11) Extension 11

#查看DB中的NLS_CHARACTERSET的值
SQL> select * from v$nls_parameters  where parameter='NLS_CHARACTERSET'; 
SQL> select * from nls_database_parameters t where t.parameter='NLS_CHARACTERSET';

12) Extension 12

About exp, imp, expdp, impdp

(1)、exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。
(2)、expdp和impdp是服务端的工具程序,他们只能在oracle服务端使用,不能在客户端使用。
(3)、imp只适用于exp导出的文件,不适用于expdp导出文件;impdp只适用于expdp导出的文件,而不适用于exp导出文件。
(4)、对于10g以上的服务器,使用exp通常不能导出0行数据的空表,而此时必须使用expdp导出。

13) Expansion 13

When the oracle user password has the @ symbol, the connection between expdp and sqlplus and the backup file using rar compression

@echo off
rem ---- dmp backup directory, same as dump dir
set backup_dir=e:\app\oracle\backup
rem ---- today, day for dmp file remaining
set day=%date:~0,4%%date:~5,2%%date:~8,2%
set remain_day=7

rem --- delete files before 7 days
forfiles /p "%backup_dir%" /d -%remain_day%  /c "cmd /c del /f @path"

rem --- export oracle data to dmp file
expdp 用户名/\"xxx@yyy\"@orcl directory=dir_dump dumpfile=用户名_%day%.dmp logfile=用户名_%day%.log schemas=用户名 parallel=4 compression=ALL
rem --- sqlplus conn
sqlplus 用户名/"""xxx@yyy"""@orcl

rem ---- if compress the dumpfile and delete source dumpfile, unmark rem

set rar="C:\Program Files (x86)\WinRAR\WinRAR.exe"

%rar% a -df %backup_dir%\用户名_%day%.rar %backup_dir%\用户名_%day%.dmp %backup_dir%\用户名_%day%.log

Fourth, clean up (error during restore, clear user table space)

1. Delete the data table space:

#执行语句:
[oracledb@ ~]$ sqlplus / as sysdba
SQL> drop tablespace mepro_data including contents and datafiles cascade constraint;

2. Delete the temporary table space:

#执行语句:
SQL> drop tablespace mepro_temp including contents and datafiles cascade constraints;

3. Delete user:

#执行语句:
SQL> drop user srmhdld cascade;

4. The solution to report that the user is connecting and cannot be deleted

---方法1:重启并迅速执行drop user语句(个人推荐)
SQL> shutdown immediate;
SQL> startup

---方法2:删除正在连接的session(连接的session连接着很多时,清理耗时,没有方法1快速)
#查询用户会话
SQL> select username,serial#,sid,program,machine,status from v$session where username='USERNAME' AND STATUS='ACTIVE';;
SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

---删除相关用户会话
SQL> alter system kill session 'serial#, sid';

Guess you like

Origin blog.csdn.net/KH_FC/article/details/110858045