프로그램언어+/JAVA&JSP

CentOS 리눅스 (Oracle 구축연동)

logthink 2019. 2. 14. 15:08

 

썬구루 서버 인프라 [ Server Infra ]

Thursday, March 29, 2018

CentOS 6.6 리눅스 네트워크 ] 9.2 Oracle 11g Express 설치 및 연동




안녕하세요. 썬구루입니다. 이 게시물에서는 CentOS 리눅스 기반 Oracle 11g Express 데이터베이스 서버 설치 및 Tomcat과 연동하는 방법에 대해 알아보도록 하겠습니다.

Oracle 데이터베이스(Database)는 널리 이용되고 있는 상용 데이터베이스 시스템입니다. 우리나라에서도 기업과 관공서에 많이 설치되어 사용되고 있으며 Oracle 데이터베이스는 Express, Standard, Enterprise 버전이 있습니다.

이들 중 Express 버전은 라이선스 비용 없이 사용할 수 있습니다. 하지만 기능 제약과 데이터 용량에 대한 제한이 있습니다.

이 게시물에서 데이터베이스와 데이터베이스 시스템이 무엇인지에 대해 설명하지 않습니다. Oracle 11g Express 데이터베이스 시스템을 CentOS 6.6 리눅스에 설치하고 이전에 설치한 Apache Tomcat과 연동하는 방법에 대해 설명합니다. 그리고 테스트 용 JSP 파일을 사용하여 Apache Tomcat 과 Oracle Database 연동을 확인합니다.

서버 IP 주소

관려 서비스

서비스 포트(Port) 주소

192.168.80.21

Apache HTTPD

80

192.168.80.22

Apache Tomcat

8080

 192.168.80.32

Oracle Database 11g Express 

1521





리눅스 용 Oracle 11g Express 데이터베이스 설치 파일은 다음과 같은 웹사이트에서 내려받을 수 있습니다. 이를 위해 Oracle 웹 계정이 필요한데 만드는데 돈 들어가는 것은 없으니 걱정하지 말고 계정 만들고 파일을 내려받으시면 됩니다.

http://www.oracle.com/technetwork/database/database-technologies/express-edition/downloads/index.html

Oracle Database Express Edition 11g Release 2 for Linux x64 <= 다운로드

■ Oracle 11g Express Database

1). CentOS 6.6에 Oracle 11g Express 설치

DB02 가상머신에 Oracle 웹사이트에서 내려받은 oracle-xe-11.2.0-1.0.x86_64.rpm.zip 파일을 VMware Workstation의 드래그 앤 드롭 기능을 사용하여 /root 디렉터리에 복사합니다. 그리고 아래와 같은 명령을 사용하여 Oracle 11g Express를 설치합니다.

[root@db02 ~]# ls *.zip
oracle-xe-11.2.0-1.0.x86_64.rpm.zip
[root@db02 ~]#
[root@db02 ~]# unzip oracle-xe-11.2.0-1.0.x86_64.rpm.zip
Archive:  oracle-xe-11.2.0-1.0.x86_64.rpm.zip
   creating: Disk1/
   creating: Disk1/upgrade/
  inflating: Disk1/upgrade/gen_inst.sql
   creating: Disk1/response/
  inflating: Disk1/response/xe.rsp
  inflating: Disk1/oracle-xe-11.2.0-1.0.x86_64.rpm
[root@db02 ~]#
[root@db02 ~]# ls -F
anaconda-ks.cfg  Documents/   install.log.syslog                    Pictures/   Videos/
Desktop/         Downloads/   Music/                                Public/
Disk1/           install.log  oracle-xe-11.2.0-1.0.x86_64.rpm.zip*  Templates/
[root@db02 ~]#
[root@db02 ~]# cd Disk1/
[root@db02 Disk1]#
[root@db02 Disk1]# ls
oracle-xe-11.2.0-1.0.x86_64.rpm  response  upgrade
[root@db02 Disk1]#
[root@db02 Disk1]# rpm -ivh oracle-xe-11.2.0-1.0.x86_64.rpm
Preparing...                ########################################### [100%]
   1:oracle-xe              ########################################### [100%]
Executing post-install steps...
You must run '/etc/init.d/oracle-xe configure' as the root user to configure the database.
[root@db02 Disk1]# 


아래와 같은 명령을 실행하여 Oracle 11g Express 데이터베이스를 구성합니다.

[root@db02 Disk1]# vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.32 db02  <= 내용 추가

[root@db02 Disk1]#
[root@db02 Disk1]# /etc/init.d/oracle-xe configure

Oracle Database 11g Express Edition Configuration
-------------------------------------------------
This will configure on-boot properties of Oracle Database 11g Express
Edition.  The following questions will determine whether the database should
be starting upon system boot, the ports it will use, and the passwords that
will be used for database accounts.  Press <Enter> to accept the defaults.
Ctrl-C will abort.

Specify the HTTP port that will be used for Oracle Application Express [8080]: 엔터

Specify a port that will be used for the database listener [1521]: 엔터

Specify a password to be used for database accounts.  Note that the same
password will be used for SYS and SYSTEM.  Oracle recommends the use of
different passwords for each database account.  This can be done after
initial configuration: 암호 설정
Confirm the password: 암호 재입력

Do you want Oracle Database 11g Express Edition to be started on boot (y/n) [y]:

Starting Oracle Net Listener...Done
Configuring database...Done
Starting Oracle Database 11g Express Edition instance...Done
Installation completed successfully.
[root@db02 Disk1]#
[root@db02 Disk1]# ps -ef | grep oracle

oracle    2968     1  0 03:43 ?        00:00:00 /u01/app/oracle/product/11.2.0/xe/bin/tnslsnr LISTENER -inherit
oracle    3395     1  0 03:44 ?        00:00:00 xe_pmon_XE
oracle    3397     1  0 03:44 ?        00:00:00 xe_psp0_XE
oracle    3399     1  0 03:44 ?        00:00:00 xe_vktm_XE
oracle    3403     1  0 03:44 ?        00:00:00 xe_gen0_XE
oracle    3405     1  0 03:44 ?        00:00:00 xe_diag_XE
oracle    3407     1  0 03:44 ?        00:00:00 xe_dbrm_XE
oracle    3409     1  0 03:44 ?        00:00:00 xe_dia0_XE
oracle    3411     1  0 03:44 ?        00:00:00 xe_mman_XE
oracle    3413     1  0 03:44 ?        00:00:00 xe_dbw0_XE
oracle    3415     1  0 03:44 ?        00:00:00 xe_lgwr_XE
oracle    3417     1  0 03:44 ?        00:00:00 xe_ckpt_XE
oracle    3419     1  0 03:44 ?        00:00:00 xe_smon_XE
oracle    3421     1  0 03:44 ?        00:00:00 xe_reco_XE
oracle    3423     1  0 03:44 ?        00:00:00 xe_mmon_XE
oracle    3425     1  0 03:44 ?        00:00:00 xe_mmnl_XE
oracle    3427     1  0 03:44 ?        00:00:00 xe_d000_XE
oracle    3429     1  0 03:44 ?        00:00:00 xe_s000_XE
oracle    3431     1  0 03:44 ?        00:00:00 xe_s001_XE
oracle    3433     1  0 03:44 ?        00:00:00 xe_s002_XE
oracle    3435     1  0 03:44 ?        00:00:00 xe_s003_XE
oracle    3514     1  0 03:44 ?        00:00:00 xe_vkrm_XE
oracle    3516     1  0 03:44 ?        00:00:00 xe_qmnc_XE
oracle    3530     1  0 03:44 ?        00:00:00 xe_cjq0_XE
oracle    3568     1  0 03:45 ?        00:00:00 xe_q000_XE
oracle    3570     1  0 03:45 ?        00:00:00 xe_q001_XE
oracle    3592     1  0 03:50 ?        00:00:00 xe_j000_XE
oracle    3594     1  0 03:50 ?        00:00:00 xe_j001_XE
root      3606  2913  0 03:50 pts/0    00:00:00 grep oracle

[root@db02 Disk1]#


oracle 계정의 bash 쉘 초기화 파일인 .bash_profile에 아래 내용을 추가합니다.

[root@db02 Disk1]# vi ~oracle/.bash_profile

export ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe
export ORACLE_SID=XE
export NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
export PATH=$ORACLE_HOME/bin:$PATH

[root@db02 Disk1]#
[root@db02 Disk1]# chown oracle:dba  ~oracle/.bash_profile
[root@db02 Disk1]#


원활한 서비스 테스트를 위해 아래와 같이 iptables 방화벽을 내리고 SELinux를 permissive로 설정합니다.

[root@db02 Disk1]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@db02 Disk1]#
[root@db02 Disk1]# chkconfig iptables off
[root@db02 Disk1]#
[root@db02 Disk1]# setenforce permissive
[root@db02 Disk1]#
[root@db02 Disk1]# vi /etc/sysconfig/selinux

SELINUX=permissive

[root@db02 Disk1]#




2). Oracle 11g Express 데이터베이스 서비스 시작 종료

service oracle-xe [ start | stop ] 


3). 시스템 부팅 시 HTTPD 서비스 자동 시작

chkconfig oracle-xe [ on | off ]


4). Oracle 11g Express 데이터베이스에 테이블 생성

아래와 같은 명령을 실행하여 Oracle 11g Express 서비스를 확인합니다.

[root@db02 Disk1]# service oracle-xe status

LSNRCTL for Linux: Version 11.2.0.2.0 - Production on 25-JUL-2016 03:54:52

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.2.0 - Production
Start Date                25-JUL-2016 03:43:44
Uptime                    0 days 0 hr. 11 min. 8 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Default Service           XE
Listener Parameter File   /u01/app/oracle/product/11.2.0/xe/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/db02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC_FOR_XE)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db02)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db02)(PORT=8080))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
  Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "XE" has 1 instance(s).
  Instance "XE", status READY, has 1 handler(s) for this service...
Service "XEXDB" has 1 instance(s).
  Instance "XE", status READY, has 1 handler(s) for this service...
The command completed successfully
[root@db02 Disk1]#
[root@db02 Disk1]# chkconfig oracle-xe --list
oracle-xe       0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@db02 Disk1]#
[root@db02 Disk1]# cd ..
[root@db02 ~]# unalias rm
[root@db02 ~]#
[root@db02 ~]# rm -rf oracle-xe-11.2.0-1.0.x86_64.rpm.zip Disk1
[root@db02 ~]# 


아래와 같은 명령을 사용하여 oracle 계정으로 사용자 대치,  Oracle 11g Express 데이터베이스에 sysdba로 접속한 후 testuser 계정을 생성합니다. 암호는 1234입니다.

[root@db02 ~]# su - oracle
-bash-4.1$
-bash-4.1$ sqlplus "/as sysdba"
SQL*Plus: Release 11.2.0.2.0 Production on Mon Jul 25 04:01:14 2016
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> create user testuser identified by 1234;
User created.

SQL> grant connect, resource to testuser;
Grant succeeded.

SQL> commit;
Commit complete.

SQL> quit

Disconnected from Oracle Database 


아래와 같은 명령을 사용하여 생성한 testuser 계정으로 sqlplus를 사용하여 Oracle 11g Express에 접속합니다. 그리고 test 테이블을 생성한 후 테이블에 데이터를 입력합니다.

-bash-4.1$ sqlplus testuser

SQL*Plus: Release 11.2.0.2.0 Production on Mon Jul 25 04:08:35 2016

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

Enter password: 암호입력(1234)

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL>
SQL> create table test(name varchar(30));

Table created.

SQL> desc test;
 Name        Null?    Type
 ----------------------------------------- -------- ----------------------------
 NAME          VARCHAR2(30)

SQL>
SQL> insert into test values('sunguru');

1 row created.

SQL> insert into test values('Oracle 11g Express');

1 row created.

SQL>
SQL> select * from test;

NAME
------------------------------
sunguru
Oracle 11g Express

SQL> commit;

Commit complete.

SQL> quit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

-bash-4.1$ 




■ Oracle 11g Express Database와 Apache Tomcat 연동

DB02 가상머신에 있는 아래와 같은 파일들을 tar 명령으로 묶어 VMware Workstation의 드래그 앤 드롭 기능을 사용하여 호스트 운영체제(여러분들의 윈도우 운영체제)에 임시 복사한 후 이를 WAS01 가상머신에 복사하여 Apache Tomcat 라이브러리 디렉터리에 풀고 Tomcat을 재시작하면  상호 간 연동이 됩니다.

▶ DB01 가상머신에서 OJDBC 파일 tar 아카이브

[root@db02 ~]# cd /u01/app/oracle/product/11.2.0/xe/jdbc/lib
[root@db02 lib]#
[root@db02 lib]# ls ojdbc*
ojdbc5.jar  ojdbc6_g.jar  ojdbc6.jar
[root@db02 lib]#
[root@db02 lib]# tar -cvf ~/ojdbc.tar ./ojdbc*
./ojdbc5.jar
./ojdbc6_g.jar
./ojdbc6.jar
[root@db02 lib]# 


▶ DB01 가상머신에 있는 /root/ojdbc.tar 파일을 호스트 운영체제로 복사
▶ 호스트 운영체제에 있는 ojdbc.tar 파일을 WAS01 가상머신의 /root 디렉터리로 복사

[root@was01 ~]# ls *.tar
ojdbc.tar
[root@was01 ~]# tar -xvf ojdbc.tar
./ojdbc5.jar
./ojdbc6_g.jar
./ojdbc6.jar
[root@was01 ~]# mv ojdbc* /usr/share/tomcat6/lib/
[root@was01 ~]#
[root@was01 ~]# unalias rm
[root@was01 ~]# rm /usr/share/tomcat6/lib/ojdbc.tar
[root@was01 ~]#
[root@was01 ~]# service tomcat6 restart
Stopping tomcat6:                                          [  OK  ]
Starting tomcat6:                                          [  OK  ]
[root@was01 ~]# 


■ JSP 파일를 가지고 Oracle 11g Express Database 연결 테스트

이 게시물의 첨부 파일에 있는 oracle.jsp [ 다운로드 ] 파일을 WAS01 가상머신의 /root 디렉터리에 VMware Workstation 드래그 앤 드롭 기능을 사용하여 복사합니다. 그리고 아래와 같이 명령을 실행하여 내용을 수정한 후 Apache Tomcat 웹 소스 디렉터리로 이동시킵니다.

[root@was01 ~]# ls oracle.jsp
oracle.jsp
[root@was01 ~]# vi oracle.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Oracle Test Table</title>
</head>
<body>
<h1> Oracle Test_Table </h1>
<table border="1">
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {

 String JDBCINFO = "jdbc:oracle:thin:@192.168.80.32:1521:xe";
 String ORA_USER = "testuser";
 String USER_PASSWD = "1234";
 String SQL_QUERY =
  "select name from test";

 conn = DriverManager.getConnection(JDBCINFO, ORA_USER, USER_PASSWD);
 stmt = conn.createStatement();

 rs = stmt.executeQuery(SQL_QUERY);
 while ( rs.next() ) { 
%>  
  <tr><td><%= rs.getString("name") %> </td></tr>
<%
 }

}catch(SQLException ex) {
%>
 Error : <%= ex.getMessage() %>
<%
}finally {
 if (rs != null) try { rs.close();} catch(SQLException ex) {}
 if (stmt != null) try { stmt.close();} catch(SQLException ex) {}
 if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
%>
</table>
</body>
</html>

[root@was01 ~]#
[root@was01 ~]# mv oracle.jsp /usr/share/tomcat6/webapps/ROOT/
[root@was01 ~]# 


■ 웹브라우저를 사용하여 Apache Tomcat과 Oracle 11g Express 연동 확인

호스트 운영체제(여러분들의 윈도우 운영체제)에 있는 웹브라우저를 사용하여 웹서버로 접속 테스트를 합니다.

HTTPD 웹서버로 oracle.jsp 파일 서비스 요청을 하면 이를 Tomcat 웹 응용(WAS) 서버로 재요청하고 JSP 파일에 있는 DB 쿼리를 처리하기 위해 ojdbc를 사용하여 Oracle 11g Express 데이터베이스에 접속, 데이터베이스 내에 있는 테이블을 확인, 정보를 참조하여 역순으로 처리, 클라이언트(웹브라우저)로 응답합니다.




성공적으로 잘 되었네요. 여기까지 Oracle 11g Express 설치 및 연동에 대해 알아보았습니다.










DB연동 에러 참고 URL

1) https://acua.tistory.com/entry/The-Network-Adapter-could-not-establish-the-connection

2) https://sarc.io/index.php/oracledatabase/186-2014-06-10-01-33-05

3) ORACLE 11g r2 설치 : https://puttico.tistory.com/119

4) jsp page : https://cloudstudying.kr/lectures/73

5) open jdk + install tomcat 9 : https://hostpresto.com/community/tutorials/how-to-install-apache-tomcat-9-on-centos-7/

6) vscode 에서 SFTP 사용 : https://github.com/rangyu/TIL/blob/master/vscode/VSCode%EC%97%90%EC%84%9C-SFTP-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95.md

7) vscode에서 SSH : https://github.com/Soonmok/2018-AI-Study-Seminar/wiki/07.-%5BWindow%5D-VScode-%EC%97%90%EC%84%9C-SSH-%EC%97%B0%EA%B2%B0-%EB%B0%8F-%EC%9E%91%EC%97%85%ED%95%98%EA%B8%B0

8) tomcat 9 설치 : https://dion-ko.tistory.com/1