본문 바로가기
Project/3-Tier Architecture

[3-Tier] WEB↔ WAS ↔ DB 연동 확인 (MySQL Connector Java)

by ganyga 2024. 2. 23.

WAS

MySQL 설치

[root@was-a ~]# yum install https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

[root@was-a ~]# yum list mysql*

[root@was-a ~]# yum install mysql-community-server.x86_64

 

status 명령어로 mysqld service Inactive 확인

[root@was-a ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html

 

start, enable 하고 status로 mysqld service active 상태 확인

[root@was-a ~]# systemctl start mysqld
[root@was-a ~]# systemctl enable mysqld

[root@was-a ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-01-22 03:31:11 UTC; 32s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 13736 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─13736 /usr/sbin/mysqld

Jan 22 03:31:03 was-a systemd[1]: Starting MySQL Server...
Jan 22 03:31:11 was-a systemd[1]: Started MySQL Server.

 

MySQL 버전 확인

[root@was-a ~]# mysql --version
mysql  Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)

RDS 생성확인

  • DB database이름 : tomcatDB
  • 마스터 사용자 이름 : adminGa
  • 마스터 사용자 비밀번호 : test1234

 

RDS DB 접속 확인

mysql -u [마스터 사용자 이름] -h [엔드포인트] -p [마스터 사용자 비밀번호]

[root@was-a ~]# mysql -u adminGa -h ga-db.cbmgmikccorh.ap-northeast-2.rds.amazonaws.com -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 8.0.35 Source distribution

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

WAS ↔ RDS 연동 확인

mysql-connector-java 설치

WAS

[root@was-a lib]# pwd
/root/tomcat/lib

[root@was-a lib]# wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.30/mysql-connector-java-8.0.30.jar

--2024-01-22 04:07:02--  https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.30/mysql-connector-java-8.0.30.jar
Resolving repo1.maven.org (repo1.maven.org)... 151.101.40.209, 2a04:4e42:a::209
Connecting to repo1.maven.org (repo1.maven.org)|151.101.40.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2513563 (2.4M) [application/java-archive]
Saving to: ‘mysql-connector-java-8.0.30.jar’

100%[===========================================================>] 2,513,563   2.13MB/s   in 1.1s

2024-01-22 04:07:03 (2.13 MB/s) - ‘mysql-connector-java-8.0.30.jar’ saved [2513563/2513563]

[root@was-a lib]# ls
annotations-api.jar       jaspic-api.jar                   tomcat-i18n-ja.jar
catalina-ant.jar          jsp-api.jar                      tomcat-i18n-ko.jar
catalina-ha.jar           mysql-connector-java-8.0.30.jar  tomcat-i18n-pt-BR.jar
catalina.jar              servlet-api.jar                  tomcat-i18n-ru.jar
catalina-ssi.jar          tomcat-api.jar                   tomcat-i18n-zh-CN.jar
catalina-storeconfig.jar  tomcat-coyote.jar                tomcat-jdbc.jar
catalina-tribes.jar       tomcat-dbcp.jar                  tomcat-jni.jar
ecj-4.20.jar              tomcat-i18n-cs.jar               tomcat-util.jar
el-api.jar                tomcat-i18n-de.jar               tomcat-util-scan.jar
jasper-el.jar             tomcat-i18n-es.jar               tomcat-websocket.jar
jasper.jar                tomcat-i18n-fr.jar               websocket-api.jar

 

dbtest.jsp 파일 생성

tomcat/webapps/ROOT/dbtest.jsp

/root/tomcat/webapps/ROOT
[root@was-a ROOT]# vi dbtest.jsp

 

String Url = jdbc:mysql://RDS 엔드포인트/DB database이름

String Id = 마스터 사용자 이름

String Pass = 마스터 사용자 비밀번호

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<h1>DB</h2>
<%
        Connection conn=null;
        try{
        
                #DB 엔드포인트/DB database 이름, adminID, adminPW 입력
                String Url="jdbc:mysql://ga-db.cbmgmikccorh.ap-northeast-2.rds.amazonaws.com/tomcatDB";
                String Id="adminGa";
                String Pass="test1234";

                Class.forName("com.mysql.jdbc.Driver");
                conn=DriverManager.getConnection(Url,Id,Pass);
                out.println("was-db Connection Success!");
        }catch(Exception e) {
                e.printStackTrace();
}
%>

 

tomcat 재시작

[root@was-a ROOT]# cd /root/tomcat/bin/
[root@was-a bin]# ./shutdown.sh
Using CATALINA_BASE:   /root/tomcat
Using CATALINA_HOME:   /root/tomcat
Using CATALINA_TMPDIR: /root/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/tomcat/bin/bootstrap.jar:/root/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:

[root@was-a bin]# ./startup.sh
Using CATALINA_BASE:   /root/tomcat
Using CATALINA_HOME:   /root/tomcat
Using CATALINA_TMPDIR: /root/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /root/tomcat/bin/bootstrap.jar:/root/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

 

WAS sever에서 curl localhost:8080/dbtest.jsp로 확인하기

[root@was-a ROOT]# curl localhost:8080/dbtest.jsp


<h1>DB</h2>
was-db Connection Success!

WEB ↔ WAS ↔ DB 연동 확인

WEB server에서 curl localhost/dbtest.jsp가 되는지 확인

[root@web-a ~]# curl localhost/dbtest.jsp


<h1>DB</h2>
was-db Connection Success!

WEB server에서 curl localhost/test.jsp가 되는지 확인

[root@web-a ~]# curl localhost/test.jsp

<html>
    	<head><title>hello world</title></head>
    	<body>
            	tomcat test
            	now_time : Mon Jan 22 06:40:49 UTC 2024
    	</body>
</html>