一、Oracle命令总结
1.查看、编辑环境变量
cat /etc/profile vi /etc/profile
2.环境变量生效
source /etc/profile
3.修改目录文件的拥有者和用户组
chown oracle filename
chgrp oinstall filename
chown -R tongfang ./*
chgrp -R tongfang ./*
4.查看dmp文件字符集
Oracle中IMP导入数据时提示字符集不一致解决
https://www.linuxidc.com/Linux/2018-01/150043.htm
cat '/root/db-1219.dmp' |od -x|head -1|awk '{print $2 $3}'|cut -c 3-6
0345对应的是ZHS16GBK,如果是0369,则对应字符集为AL32UTF8
SQL> select nls_charset_name(to_number('0354','xxxx')) from dual;
5.查看服务器端字符集
SQL > select * from V$NLS_PARAMETERS;
6.修改数据库字符集为:ZHS16GBK
$sqlplus /nolog
SQL>conn / as sysdba
若此时数据库服务器已启动,则先执行 SHUTDOWN IMMEDIATE 命
令关闭数据库服务器,然后执行以下命令:
SQL>shutdown immediate
SQL>STARTUP MOUNT
SQL>ALTER SYSTEM ENABLE RESTRICTED SESSION;
SQL>ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
SQL>ALTER SYSTEM SET AQ_TM_PROCESSES=0;
SQL>ALTER DATABASE OPEN;
SQL>ALTER DATABASE CHARACTER SET ZHS16GBK
若出现超集上面语句无法修改,需使用INTERNAL_USE可以跳过超集的检查:
SQL>ALTER DATABASE CHARACTER SET INTERNAL_USE ZHS16GBK;
ERROR at line 1:
ORA-12721: operation cannot execute when other sessions are active
若报以上错误则:select sid,serial#,username,machine from v$session;查看有哪些SESSION,然后
alter system kill session 'sid'serial#' immediate;然后
alter system kill session '158,7' immediate;如这样
SQL>ALTER DATABASE CHARACTER SET INTERNAL_USE ZHS16GBK;
SQL>SHUTDOWN IMMEDIATE
SQL>STARTUP
7. 删除表空间和用户
drop tablespace DW_TRAFFIC_TBS including contents and datafiles;
drop user DW_TRAFFIC cascade;
drop tablespace TOPEP including contents and datafiles;
8.创建表空间与用户
Create tablespace DW_TRAFFIC_TBS datafile '/u01/app/oracle/oradata/orcl/DW_TRAFFIC_TBS.dbf' size 500m autoextend on;
Create tablespace TOPEP datafile '/home/hongdian/tools/oracle11g/oradata/TOPEP.dbf' size 500m autoextend on;
create user DW_TRAFFIC identified by DW_TRAFFIC default tablespace DW_TRAFFIC_TBS temporary tablespace temp;
9.给用户授权
grant connect to DW_TRAFFIC;
grant resource to DW_TRAFFIC;
grant dba to DW_TRAFFIC;
10. 创建逻辑目录
--创建还原目录(单引号里面的内容是导入的目录,与前面创建的目录相同)
create or replace directory DATA_PUMP_DIR as '/u01/app/oracle/oradata/orcl';
create or replace directory DATA as '/home/hongdian/tools/oracle11g/oradata';
11.给目标用户授权
grant read,write on directory DATA_PUMP_DIR to DW_TRAFFIC;
grant read,write on directory DATA to TOPEP;
12.导出备份文件
通过数据泵expdp、impdp方式备份与还原(导出与导入)Oracle数据库
https://blog.csdn.net/qq_26230421/article/details/79382013
expdp DW_TRAFFIC/DW_TRAFFIC DIRECTORY=DATA_PUMP_DIR SCHEMAS=DW_TRAFFIC dumpfile=DW_TRAFFIC20200116.dump
--导出dmp文件路径:/home/hongdian/tools/oracle11g/admin/orcl/dpdump/
/home/hongdian/tools/oracle11g/oradata
exp system/oracle@orcl file=/hongdian/db-back/db-1219.dmp full=y ignore=y
13.导入备份文件
cp DW_TRAFFIC20200116.dump /u01/app/oracle/oradata/orcl
impdp DW_TRAFFIC/DW_TRAFFIC@orcl directory=DATA_PUMP_DIR dumpfile= DW_TRAFFIC20200116.dump
imp DW_TRAFFIC/DW_TRAFFIC@orcl file=/root/DW_TRAFFIC_1224.dmp full=y ignore=y
cp TOPEP20200114_15.dump /home/hongdian/tools/oracle11g/oradata/
impdp DW_TRAFFIC/DW_TRAFFIC@orcl directory=DATA dumpfile= TOPEP20200114_15.dump
14.有很多种方法可以查出oracle server端的字符集,
比较直观的查询方法是以下这种:SQL>select userenv(‘language’) from dual;
结果类似如下:AMERICAN _ AMERICA. ZHS16GBK
15.查询oracle client端的字符集
在unix平台下,就是环境变量NLS_LANG。
$echo $NLS_LANG
AMERICAN_AMERICA.ZHS16GBK
如果检查的结果发现server端与client端字符集不一致,请统一修改为同server端相同的字符集。
16.查看ORACLE版本
select * from v$version;
17. 查询所有用户
select * from dba_users;
18.查看所有用户所在表空间
select username,default_tablespace from dba_users;
19.查询所有表空间路径
select * from dba_data_files;
20.查询表空间的名称及大小(原样复制,不要改)
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size
FROM dba_tablespaces t, dba_data_files d
WHERE t.tablespace_name = d.tablespace_name
GROUP BY t.tablespace_name;
21.查看表空间物理文件的名称及大小(原样复制,不要改)
SELECT tablespace_name,file_id,file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files ORDER BY tablespace_name;
22.创建临时表空间
create temporary tablespace TRAFFIC_ONE_TEMP tempfile '/home/lukz/oracle11g/oradata/orcl/TRAFFIC_ONE_TEMP.dbf' size 50M autoextend ON next 10M maxsize 100M;
23.分配表空间和临时表空间
alter user DW_TRAFFIC_01 default tablespace TRAFFIC_ONE temporary tablespace TRAFFIC_ONE_TEMP;
24.给用户分配权限
grant create session,create table,create view,create sequence,unlimited tablespace to DW_TRAFFIC_01;
25. 表空间重命名
alter tablespace GPSPACE rename to GPMGT_DATA;
Tablespace altered
26.查询表格字段和注释
SELECT
A.COLUMN_NAME,
A.DATA_TYPE,
B.comments
FROM
user_tab_columns A INNER JOIN user_col_comments B ON A.TABLE_NAME = B.table_name
AND A.COLUMN_NAME = B.column_name
WHERE
A.TABLE_NAME = 'SYS_USER'
ORDER BY
A.column_id
27.ORA-01940 无法删除当前已连接的用户之解决方案
在执行drop user的时候,提示报错信息:ORA-01940: cannot drop a user that is currently connected
SQL> drop user ecity ;
ERROR at line 1:
ORA-01940: cannot drop a user that is currently connected
造成这个问题的原因是很明显的,有用户在连接,不允许drop掉该user。
解决方案:
首先查询一下数据中有没有用户在使用
select username,sid,serial#,paddr from v$session where username='ECITY';
USERNAME SID SERIAL# PADDR
------------------------------ ---------- -------------------------------------------------
ECITY 634 7 00000000C028D198
SQL> select PROGRAM from v$process where addr='00000000C028D198';
PROGRAM
----------------------------------------------------------------------------------------------------------
Oracle@oradb01 (DW00)
其次杀掉系统中的这个进程
SQL> alter system kill session '634,7';
System altered.
然后执行删除操作,即可完成
SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;
SQL> drop user ecity CASCADE;
User dropped.
28.sqlplus连接远程数据库
简易连接,不用进行网络配置,其实就是tnsname.ora文件,但只支持oracle10G以上。命令:sqlplus 用户名/密码@ip地址[:端口]/service_name [as sysdba]示例:sqlplussys/pwd@ip:1521/test as sysdba 备注:使用默认1521端口时可省略输入
29.重启Oracle数据库
1.先执行sqlplus /nolog
2. 再执行 conn sys / as sysdba(口令是你登录数据库时输入的密码)
3. 再执行 shutdown immediate
4.最后执行startup
30.centos7.4防火墙
1.查看防火墙状态sudo systemctl status firewalld
2.关闭防火墙sudo systemctl stop firewalld
3.打开防火墙sudo systemctl start firewalld