CentOS Oracle 报 Environment variable ORACLE_UNQNAME not defined 异常的解决方法

CentOS Oracle 报 Environment variable ORACLE_UNQNAME not defined 错误的解决办法

    今天在 CentOS 上安装完 Oracle 后,通过命令 “emctl start dbconsole” 启动管理控制台时,报了 “Environment variable ORACLE_UNQNAME not defined” 的错误。问题原因是因为 Oracle 总是获取不到正确的 IP 地址(安装 Oracle 时获取的 IP 地址就是错误的),导致 em 出现问题,只需重新配置 emca 即可。折腾了好久,最后终于把问题给解决了。

环境描述

操作系统: CentOS 6.5
IP:         192.168.132.41
域:         mycompany.com.cn
主机名: hemw-pc
Oracle:11gR2,安装目录为 /opt/oracle/11g

问题分析

    刚开始按照网上的解决方案使用命令“emca -config dbcontrol db -repos create” 重新创建 em,输入完对应的信息时,到最后确认时获取到的 IP 地址总是不正确(Local hostname 应该是 192.168.132.41 才对):

-----------------------------------------------------------------

You have specified the following settings

Database ORACLE_HOME ................ /opt/oracle/11g

Local hostname ................ 192.168.64.61
Listener ORACLE_HOME ................ /opt/oracle/11g
Listener port number ................ 1521
Database SID ................ orcl
Email address for notifications ............... 
Outgoing Mail (SMTP) server for notifications ............... 

-----------------------------------------------------------------
因为获取到的 hostname 值不正确,导致一直无法创建成功,提示如下错误

Could not complete the configuration. Refer to the log file at /opt/oracle/cfgtoollogs/emca/orcl/emca_2015_03_10_17_19_30.log for more details.

解决方案

    经过各种尝试和折腾后,最终还是把问题给搞定了,步骤如下:

1. 停止监听器

$ lsnrctl stop

2. 停止 oracle 数据库

$ sqlplus "sys / as sysdba"

SQL*Plus: Release 11.2.0.1.0 Production on Tue Mar 10 17:16:45 2015

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

Enter password: 

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

SQL> shutdown

3. 修改 $ORACLE_HOME/network/admin/listener.ora,将文件内容中 HOST 的值改为正确的 IP(192.168.132.41),如:

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.132.41)(PORT = 1521))
    )
  )
4. 修改 $ORACLE_HOME/network/admin/tnsnames.ora,将文件内容中 HOST 的值改为正确的 IP(192.168.132.41),如:
ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.132.41)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
5. 修改 /etc/sysconfig/network,将文件内容改为:
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=hemw-pc

因为修改完这个文件后需要重启操作系统才会生效,所以在不重启操作系统的情况下需要同时执行一下:# hostname hemw-pc

6. 修改 /etc/hosts,在文件的最后新增如下内容:

192.168.132.41 hemw-pc hemw-pc.mycompany.com.cn

7. 启动数据库

SQL> startup

8. 启动监听

$ lsnrctl start
9. 删除早期DBCONSOLE创建的用户:
sql>drop role MGMT_USER;
sql>drop user MGMT_VIEW cascade;
sql>drop user sysman cascade;
10. 删除早期DBCONSOLE创建的对象:
sql>drop PUBLIC SYNONYM MGMT_TARGET_BLACKOUTS;
sql>drop public synonym SETEMVIEWUSERCONTEXT;
11. 重新创建DBCONSOLE:(如果是在windows下,要先到注册表删除DBCONSOLE的服务,重启电脑)
$emca -config dbcontrol db -repos create
    如果提示创建失败,输出类似 “Could not complete the configuration. Refer to the log file at /opt/oracle/cfgtoollogs/emca/orcl/emca_*.log for more details.” 的错误信息,请查看对应的日志文件, 日志文件的最后一行内容如果是 “ORA-00955: name is already used by an existing object”,如:
CREATE PUBLIC SYNONYM MGMT_AVAILABILITY FOR MGMT_AVAILABILITY
                      *
ERROR at line 1:
ORA-00955: name is already used by an existing object
则需要执行一次重建的命令:
$emca -config dbcontrol db -repos recreate
    在执行上面命令的过程中需要根据提示输入对应的数据库信息及 SYS、DBSNMP 和 SYSMAN 的登录密码,最后输入 Y 进行确认,如:
STARTED EMCA at Mar 10, 2015 10:03:17 PM
EM Configuration Assistant, Version 11.2.0.0.2 Production
Copyright (c) 2003, 2005, Oracle.  All rights reserved.

Enter the following information:
Database SID: orcl
Listener port number: 1521
Listener ORACLE_HOME [ /opt/oracle/11g ]: 
Password for SYS user:  
Password for DBSNMP user:  
Password for SYSMAN user:  
Email address for notifications (optional): 
Outgoing Mail (SMTP) server for notifications (optional):
12. 检查 em 的状态
$ emctl status dbconsole

Oracle Enterprise Manager 11g Database Control Release 11.2.0.1.0 
Copyright (c) 1996, 2009 Oracle Corporation.  All rights reserved.
https://hemw-pc:1158/em/console/aboutApplication
Oracle Enterprise Manager 11g is running. 
------------------------------------------------------------------
Logs are generated in directory /opt/oracle/11g/hemw-pc_orcl/sysman/log
至此,问题已经解决完毕。

附:emca 常用命令

创建一个 em 资料库:emca -repos create 
重建一个 em 资料库:emca -repos recreate 
删除一个 em 资料库:emca -repos drop
 
配置数据库的 Database Control:emca -config dbcontrol db 
删除数据库的 Database Control配置:emca -deconfig dbcontrol db
 
重新配置db control的端口,默认端口在1158
emca -reconfig ports
emca -reconfig ports -dbcontrol_http_port 1160
emca -reconfig ports -agent_port 3940
 
先设置 ORACLE_SID 环境变量后,启动 em console服务:emctl start dbconsole
先设置 ORACLE_SID 环境变量后,停止em console服务:emctl stop dbconsole
先设置 ORACLE_SID 环境变量后,查看 em console服务的状态:emctl status dbconsole
  
配置 dbconsole 的步骤
emca -repos create
emca -config dbcontrol db
emctl start dbconsole
 
重新配置 dbconsole 的步骤
emca -repos drop
emca -repos create
emca -config dbcontrol db

emctl start dbconsole


参考资料:
http://soulful.blog.51cto.com/468033/449934
http://czhwin.iteye.com/blog/1847366