╔══════════════════════════════════╗ ║ - 数据库启停 · 表空间管理 ║ - 用户权限 · 数据泵导入导出 ║ - 日常维护命令速查手册 ╚══════════════════════════════════╝
登录 SYS 用户 1 sqlplus sys/ aa123789 as sysdba;
一、Linux 启动 Oracle 启动监听,查看监听状态:
1 2 [oracle@localhost ~]$ lsnrctl start [oracle@localhost ~]$ lsnrctl status
启动数据库实例:
1 2 3 [oracle@localhost ~]$ sqlplus /nolog SQL> conn /as sysdba SQL> startup
二、Linux 关闭 Oracle 关闭数据库实例:
关闭监听:
1 [oracle@localhost ~]$ lsnrctl stop
三、会话管理 1 2 3 4 5 select * from v$session where username = 'NXECR' and status = 'ACTIVE' ;alter system kill session '1150, 9' ;
四、用户管理 1 2 drop user c##tien cascade;
五、表空间管理 查询表空间使用情况 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 SELECT df.tablespace_name TABLESPACE, df.total_space TOTAL_SPACE, fs.free_space FREE_SPACE, df.total_space_mb TOTAL_SPACE_MB, (df.total_space_mb - fs.free_space_mb) USED_SPACE_MB, fs.free_space_mb FREE_SPACE_MB, ROUND(((df.total_space - fs.free_space) / df.total_space) * 100 , 2 ) PCT_USED FROM (SELECT tablespace_name, SUM (bytes) TOTAL_SPACE, ROUND(SUM (bytes) / 1048576 ) TOTAL_SPACE_MB FROM dba_data_files GROUP BY tablespace_name) df, (SELECT tablespace_name, SUM (bytes) FREE_SPACE, ROUND(SUM (bytes) / 1048576 ) FREE_SPACE_MB FROM dba_free_space GROUP BY tablespace_name) fs WHERE df.tablespace_name = fs.tablespace_name(+ )ORDER BY ((df.total_space - fs.free_space) / df.total_space) desc ;
查询表空间名称及位置 1 2 select file_name, tablespace_name, bytes/ 1024 / 1024 , maxbytes/ 1024 / 1024 , autoextensiblefrom dba_data_files;
查询锁表情况 1 2 3 4 5 6 7 8 9 10 select b.owner TABLEOWNER, b.object_name TABLENAME, c.OSUSER LOCKBY, c.USERNAME LOGINID, c.sid SID, c.SERIAL# SERIAL from v$locked_object a, dba_objects b, v$session cwhere b.object_id = a.object_id and a.SESSION_ID = c.sid;
新建表空间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 create tablespace TIEN datafile 'C:\APP\MICROENGINEER\ORADATA\ORCL\TIAN01.DBF' size 5 M autoextend on ; create tablespace TIEN datafile '/orcl/app/oracle/oradata/orcl/tien01.dbf' size 5 M autoextend on ; create tablespace TIEN datafile '/usr/local/oracle/oradata/orcl/tien01.dbf' size 5 M autoextend on ; create tablespace TIEN datafile '/data/tien/tien02.dbf' size 5 M autoextend on ; create tablespace TAN datafile '/usr/local/oracle/oradata/orcl/tan01.dbf' size 5 M autoextend on ; create tablespace TAN datafile '/orcl/app/oracle/oradata/ORCL/datafile/tan01.dbf' size 5 M autoextend on ;
六、创建用户与授权 创建用户 1 2 3 4 5 6 7 8 9 10 11 12 13 14 create user tien identified by tien default tablespace TIEN; create user c#tien identified by xxxxxx default tablespace TIEN; create user ALS_CREDIT default tablespace ALS temporary tablespace ALS_TEMP profile DEFAULT identified by alscredit;
修改密码 1 2 alter user c##tien identified by c#tienxxxx;alter user c##tieninfo identified by c#tieninfoxxxx;
赋予权限 1 2 3 grant dba, resource to tien;grant dba, resource to c#tien;grant dba, resource, unlimited tablespace to c##tien;
七、EXPDP/IMPDP 数据泵 创建逻辑目录 1 2 3 4 5 6 7 8 9 10 [oracle@localhost ~ ]$ sqlplus / nolog SQL > conn / as sysdbacreate directory mydata as '/orcl/app/oracle/mydata' ;grant read, write on directory mydata to c##tien;grant dba, resource, unlimited tablespace to c##tien;
示例:
1 2 3 create directory tiendata as '/data/tienbak' ;grant read, write on directory tiendata to c##tien;grant dba, resource, unlimited tablespace to c##tien;
查看目录是否创建成功 1 select * from dba_directories;
EXPDP 导出 1 expdp c##tien/c#tien0918@tien schemas=c##tien dumpfile=2021tien.dmp directory=tiendata logfile=2021tien.log ;
导出场景 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 expdp scott/tiger@orcl schemas=scott dumpfile=expdp.dmp directory=dump_dir logfile=expdp.log; expdp scott/tiger@orcl tables=emp,dept dumpfile=expdp.dmp directory=dump_dir logfile=expdp.log; expdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp tables=emp query='where deptno=20' logfile=expdp.log; expdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=temp,example logfile=expdp.log; expdp scott/123@127.0.0.1/orcl directory=dump_dir dumpfile=ly.dmp full=y logfile=expdp.log; expdp JCPT/123@127.0.0.1/orcl directory=mydata schemas=jcpt dumpfile=ly.dmp logfile=expdp.log;
IMPDP 导入 步骤一:创建表空间 1 create tablespace data_test datafile 'e:\oracle\oradata\test\test.dbf' size 2000 M;
步骤二:创建用户并授权 1 2 3 4 5 6 7 create user study identified by study default tablespace data_test;grant read, write on directory mydata to study;grant dba, resource, unlimited tablespace to study;
步骤三:执行导入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 impdp scott/tiger@orcl directory=dump_dir dumpfile=expdp.dmp schemas=scott logfile=impdp.log; impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmp tables=scott.dept,scott.emp remap_schema=scott:system logfile=impdp.log table_exists_action=replace; impdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=example logfile=impdp.log; impdp system/manager@orcl directory=dump_dir dumpfile=full.dmp full=y logfile=impdp.log; impdp lyxt/lyxt123@127.0.0.1/orcl directory=mydata dumpfile=LY.DMP remap_schema=jcpt:lyxt logfile=ims20171122.log table_exists_action=replace; impdp system/manager@orcl directory=dump_dir dumpfile=expdp.dmp schemas=system table_exists_action=append logfile=impdp.log;
直接导入示例 1 impdp c##tien/tien@orcl directory=mydata dumpfile=tien20210207.dmp schemas=c##tien logfile=impdp.log;