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 才对):

  1. —————————————————————–
  2. You have specified the following settings
  3. Database ORACLE_HOME ……………. /opt/oracle/11g
  4. Local hostname ……………. 192.168.64.61
  5. Listener ORACLE_HOME ……………. /opt/oracle/11g
  6. Listener port number ……………. 1521
  7. Database SID ……………. orcl
  8. Email address for notifications ……………
  9. Outgoing Mail (SMTP) server for notifications ……………
  10. —————————————————————–

因为获取到的 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 数据库

  1. $ sqlplus “sys / as sysdba”
  2. SQL*Plus: Release 11.2.0.1.0 Production on Tue Mar 10 17:16:45 2015
  3. Copyright (c) 1982, 2009, Oracle. All rights reserved.
  4. Enter password:
  5. Connected to:
  6. Oracle Database 11g Enterprise Edition Release 11.2.0.1.064bit Production
  7. With the Partitioning, OLAP, Data Mining and Real Application Testing options
  8. SQL> shutdown

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

  1. LISTENER =
  2. (DESCRIPTION_LIST =
  3. (DESCRIPTION =
  4. (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
  5. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.132.41)(PORT = 1521))
  6. )
  7. )

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

  1. ORCL =
  2. (DESCRIPTION =
  3. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.132.41)(PORT = 1521))
  4. (CONNECT_DATA =
  5. (SERVER = DEDICATED)
  6. (SERVICE_NAME = orcl)
  7. )
  8. )

5. 修改 /etc/sysconfig/network,将文件内容改为:

  1. NETWORKING=yes
  2. NETWORKING_IPV6=no
  3. 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创建的用户:

  1. sql>drop role MGMT_USER;
  2. sql>drop user MGMT_VIEW cascade;
  3. sql>drop user sysman cascade;

10. 删除早期DBCONSOLE创建的对象:

  1. sql>drop PUBLIC SYNONYM MGMT_TARGET_BLACKOUTS;
  2. 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”,如:

  1. CREATE PUBLIC SYNONYM MGMT_AVAILABILITY FOR MGMT_AVAILABILITY
  2. *
  3. ERROR at line 1:
  4. ORA-00955: name is already used by an existing object

则需要执行一次重建的命令:

$emca -config dbcontrol db -repos recreate

在执行上面命令的过程中需要根据提示输入对应的数据库信息及 SYS、DBSNMP 和 SYSMAN 的登录密码,最后输入 Y 进行确认,如:

  1. STARTED EMCA at Mar 10, 2015 10:03:17 PM
  2. EM Configuration Assistant, Version 11.2.0.0.2 Production
  3. Copyright (c) 2003, 2005, Oracle. All rights reserved.
  4. Enter the following information:
  5. Database SID: orcl
  6. Listener port number: 1521
  7. Listener ORACLE_HOME [ /opt/oracle/11g ]:
  8. Password for SYS user:
  9. Password for DBSNMP user:
  10. Password for SYSMAN user:
  11. Email address for notifications (optional):
  12. Outgoing Mail (SMTP) server for notifications (optional):

12. 检查 em 的状态

  1. $ emctl status dbconsole
  2. Oracle Enterprise Manager 11g Database Control Release 11.2.0.1.0
  3. Copyright (c) 1996, 2009 Oracle Corporation. All rights reserved.
  4. https://hemw-pc:1158/em/console/aboutApplication
  5. Oracle Enterprise Manager 11g is running.
  6. ——————————————————————
  7. 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

Leave a Reply