删除oracle 用户 时 报 “ORA-01940: cannot drop a user that is currently connected” 的解决方法

我们在删除oracle的用户时,如果经常遇到 ORA-01940 无法删除当前已连接的用户

如下:

SQL> drop user occdev cascade;
drop user occdev cascade
*
ERROR at line 1:
ORA-01940: cannot drop a user that is currently connected

造成这个问题的原因是很明显的,就是有用户在连接,oracle不允许删除掉该user。
那该如何解决呢?网上很多都说先查询一下数据中有没有用户在使用,命令

SQL> select username,sid,serial# from v$session;

然后再用命令杀掉系统中的这个进程

SQL> alter system kill session 'sid,serial#';

杀掉进程后再删除该用户

SQL> drop user occdev cascade;

可惜,很遗憾!我使用以上方法,几乎都是不成功的,特别是我的项目是微服务,几十个甚至上百个服务连接了这个用户(开发环境),所以靠杀掉进程,你会kill到你发疯。
故本人直接关闭数据库的监听,然后再删除改用户,具体操作如下:

[root@ts-db ~]# su - oracle
Last login: Fri May  8 10:10:00 CST 2020 on pts/0
-bash: ulimit: max user processes: cannot modify limit: Operation not permitted

oracle@ts-db ~]$ lsnrctl stop

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 08-MAY-2020 17:07:46

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ts-db)(PORT=1521)))
The command completed successfully
[oracle@ts-db ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri May 8 17:07:55 2020

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> shutdown immediate; 
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 1.0055E+10 bytes
Fixed Size		    2261888 bytes
Variable Size		 2315259008 bytes
Database Buffers	 7717519360 bytes
Redo Buffers		   19742720 bytes
Database mounted.
SQL> alter database open;

Database altered.

SQL> drop user occdev cascade;

User dropped.

SQL> 

虽然此操作有些粗暴,但简单还省事,并且没有什么后遗症(目前是未发现啊)。
至此,用户顺利删除,再 使用 命令把监听服务重启

[oracle@ts-db ~]$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 08-MAY-2020 17:29:35

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Starting /oracle/app/product/11.2.0/db_home/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /oracle/app/product/11.2.0/db_home/network/admin/listener.ora
Log messages written to /oracle/app/diag/tnslsnr/ts-db/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ts-db)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ts-db)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                08-MAY-2020 17:29:35
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /oracle/app/product/11.2.0/db_home/network/admin/listener.ora
Listener Log File         /oracle/app/diag/tnslsnr/ts-db/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ts-db)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
The listener supports no services
The command completed successfully
[oracle@ts-db ~]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 08-MAY-2020 17:29:54

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ts-db)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                08-MAY-2020 17:29:35
Uptime                    0 days 0 hr. 0 min. 19 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /oracle/app/product/11.2.0/db_home/network/admin/listener.ora
Listener Log File         /oracle/app/diag/tnslsnr/ts-db/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=ts-db)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@ts-db ~]$ 

你可能感兴趣的:(数据库)