AWS

AWS : 3 Tier [DB ์ƒ์„ฑ]

NENGIN 2024. 11. 26. 22:04

 

์ด์ „ ๊ธ€์—์„œ ์ด์–ด์ง‘๋‹ˆ๋‹ค

 

AWS : 3 Tier [WAS & NLB ์ƒ์„ฑ]

์ด์ „ ๊ธ€์—์„œ ์ด์–ด์ง‘๋‹ˆ๋‹ค AWS : 3 Tier [WEB & ALB ์ƒ์„ฑ]์ด์ „ ๊ธ€์—์„œ ์ด์–ด์ง‘๋‹ˆ๋‹ค AWS : 3 Tier [์ดˆ๊ธฐ ์„ค์ • ๋ฐ ์ž‘์—…]์ฐธ์กฐ๋งํฌ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” AWS ๊ณต๋ถ€ - 3 Tier ๊ตฌ์ถ• 1ํŽธ - ๊ตฌ์ถ• ๊ณ„ํš & VPC & Bastion HostAWS 3 Tier ๊ตฌ

nenjin.tistory.com

 

 

์ธ์Šคํ„ด์Šค ์‚ฌ์–‘

Server Spec OS Volume Public IP Application Version
Bastion t2.micro Amazon Linux 8 GB O X X
WEB01 t2.small Amazon Linux 20 GB X Apache 2.4.X
WEB02 t2.small Amazon Linux 20 GB X Apache 2.4.X
WAS01 t2.small Amazon Linux 30 GB X Tomcat 9.0.97
WAS02 t2.small Amazon Linux 30 GB X Tomcat 9.0.97
DB t2.medium Amazon Linux 30 GB X MySQL 8.X

 

 

 

12. DB

12-1 ๋ณด์•ˆ๊ทธ๋ฃน ์ƒ์„ฑ

DB ์ „์šฉ ๋ณด์•ˆ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•œ๋‹ค

์ธ๋ฐ”์šด๋“œ๋Š” WAS ์„œ๋ฒ„์˜ 3306๋ฒˆ ํฌํŠธ์™€ Bastion ์„œ๋ฒ„์˜ 22๋ฒˆ ํฌํŠธ๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค

์•„์›ƒ๋ฐ”์šด๋“œ ๋ชจ๋“  ๋Œ€์ƒ๊ณผ ํŠธ๋ž˜ํ”ฝ์„ ์—ด์–ด๋‘” ์ด์œ ๋Š” DB๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ค์น˜๊ณผ์ • ํ›„ ์‚ญ์ œํ•  ์˜ˆ์ •์ด๋‹ค 

 

Bastion ๋ณด์•ˆ๊ทธ๋ฃน ์•„์›ƒ๋ฐ”์šด๋“œ๋กœ DB ๋ณด์•ˆ๊ทธ๋ฃน์˜ 22๋ฒˆ ํฌํŠธ๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค

 

WAS ๋ณด์•ˆ๊ทธ๋ฃน ์•„์›ƒ๋ฐ”์šด๋“œ๋กœ DB ๋ณด์•ˆ๊ทธ๋ฃน์˜ 3306๋ฒˆ ํฌํŠธ๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค

 

12-2 ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

์‚ฌ์–‘ํ‘œ์— ๋งž๊ฒŒ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

 

 

13. MySQL

13-1 MySQL ์†Œ์Šค ์„ค์น˜

- root ์ง„์ž…
# sudo su -

- ๊ทธ๋ฃน๊ณผ ์‚ฌ์šฉ์ž ์ƒ์„ฑ
# groupadd mysql
# useradd -g mysql -s /bin/bash -m mysql
- ํ•„์š” ํŒจํ‚ค์ง€ ์„ค์น˜
# yum install -y --allowerasing gcc gcc-c++ openssl curl autoconf ncurses-devel bison zlib curl bzip2-devel rpcgen libtirpc libtirpc-devel cmake

allowerasing : ์ข…์†์„ฑ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ๊ธฐ์กด ์„ค์น˜๋œ ํŒจํ‚ค์ง€๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์ข…์†์„ฑ์ด ํ•ด๊ฒฐ๋œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๋Š” ์˜ต์…˜

- mysql ์†Œ์ŠคํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.23.tar.gz

- ์••์ถ•ํŒŒ์ผ ํ•ด์ œ ํ›„ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋™
# tar xvf mysql-boost-5.7.23.tar.gz
# cd mysql-5.7.23
- cmake์„ ์‚ฌ์šฉํ•˜์—ฌ MAKEFILE ์ƒ์„ฑ
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 -DMYSQL_DATADIR=/usr/local/mysql57/data -DMYSQL_UNIX_ADDR=/usr/local/mysql57/mysql.sock -DSYSCONFDIR=/usr/local/mysql57 -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_ARCHIVE_STORAGE_ENGINE=1 \-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \-DDOWNLOAD_BOOST=0 -DWITH_BOOST=./boost
- ์œ„์—์„œ ์ƒ์„ฑํ•œ MAKEFILE ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜์—ฌ ๋นŒ๋“œ(์ปดํŒŒ์ผ) & ๋นŒ๋“œ๋œ ์‹คํ–‰ ํŒŒ์ผ์„ ์‹œ์Šคํ…œ ๊ฒฝ๋กœ์— ์„ค์น˜
# make && make install

 

13-2 MySQL ์„ค์ •

- my.cnf ์ƒ์„ฑ
# sudo vi /usr/local/mysql57/my.cnf

[mysqld]
port=3306
# basedir=/usr/local/mysql57
# datadir=/usr/local/mysql57/data
# pid-file=/usr/local/mysql57/mysqld.pid
# log_error=/usr/local/mysql57/mysql_error.log
# lc-messages-dir=/usr/local/mysql57/share

init_connect=SET collation_connection = utf8_general_ci
init_connect=SET NAMES utf8
character-set-server=utf8
collation-server=utf8_general_ci
# table_cache=1024
max_connections=2048
max_user_connections=500
max_connect_errors=10000
wait_timeout=300
query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 5M
slow_query_log
long_query_time=3
max_allowed_packet=16M
sort_buffer_size = 2M
# skip-innodb
skip-name-resolve
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[mysql]
default-character-set=utf8

[client]
default-character-set=utf8

my.cnf ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•œ๋‹ค

my.cnf ํŒŒ์ผ์€ MySQL ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์˜ ์„ค์ •์„ ์ €์žฅํ•˜๋Š” ๊ตฌ์„ฑ ํŒŒ์ผ์ด๋‹ค

MySQL์€ ์ด ํŒŒ์ผ์„ ์ฝ์–ด์„œ ์„œ๋ฒ„ ๋ฐ ํด๋ผ์ด์–ธํŠธ์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ์ œ์–ดํ•œ๋‹ค

- ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์„ ๋ณ€๊ฒฝ
# chown -R mysql:mysql /usr/local/mysql57

/usr/local/mysql57 ๋””๋ ‰ํ„ฐ๋ฆฌ์™€ ๊ทธ ํ•˜์œ„์— ์žˆ๋Š” ๋ชจ๋“  ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์†Œ์œ ์ž์™€ ๊ทธ๋ฃน์„ mysql๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค

- ๋””๋ ‰ํ„ฐ๋ฆฌ ์ด๋™
# cd /usr/local/mysql57

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์น˜ํ•˜์—ฌ ์ดˆ๊ธฐํ™”
# bin/mysql_install_db --no-defaults --user=mysql --datadir=/usr/local/mysql57/data --basedir=/usr/local/mysql57 -v

/usr/local/mysql57์— ์„ค์น˜๋œ MySQL ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ(/usr/local/mysql57/data)๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค

- ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ถ”๊ฐ€
# vi ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/mysql57/bin
export PATH

MySQL ๋ช…๋ น์–ด๋ฅผ ์–ด๋””์„œ๋“  ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—…์ด๋‹ค

 

13-3 MySQL ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์‹คํ–‰

 

- ํŒŒ์ผ ๋ณต์‚ฌ
# cp /usr/local/mysql57/support-files/mysql.server /etc/init.d/mysqld57
- ์„œ๋น„์Šค ํŒŒ์ผ ์ˆ˜์ •
# sudo vi /etc/init.d/mysqld57

basedir=/usr/local/mysql57
datadir=/usr/local/mysql57/data

์„œ๋น„์Šค ํŒŒ์ผ์—์„œ  MySql ์„ค์น˜ ๋””๋ ‰ํ† ๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•œ๋‹ค

- ์„œ๋น„์ŠคํŒŒ์ผ ๋“ฑ๋ก
# chkconfig --add mysqld57
# chmod +x /etc/init.d/mysqld57
# chkconfig --level 345 mysqld57 on

chkconfig๋Š” ์‹œ์Šคํ…œ ๋ถ€ํŒ… ์‹œ mysqld57 ์„œ๋น„์Šค๋ฅผ ์ž๋™์œผ๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ๋“ฑ๋กํ•˜๋Š” ๋ช…๋ น์–ด์ด๋‹ค

์„œ๋น„์ŠคํŒŒ์ผ์— ์‹คํ–‰๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ณ  ๋Ÿฐ๋ ˆ๋ฒจ3/4/5 ๋ถ€ํŒ…์‹œ ์ž๋™์‹คํ–‰์„ ์„ค์ •ํ•œ๋‹ค

# service mysqld57 start

 

13-4 MySQL ์ ‘์† ๋ฐ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ

- ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ
# cat /root/.mysql_secret
Password set for user 'root@localhost' at 2024-11-25 11:45:20
(oAq6jyW-qAc

MySql 5.7 ๋ถ€ํ„ฐ๋Š” root ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ฐœ๊ธ‰ํ•ด์ค€๋‹ค

- ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ root ์ ‘์†
# bin/mysql -u root -p

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23-log

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
- ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ
# ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd';

- ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ ์šฉ
# flush privileges;
- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ™•์ธ
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

- ํฌํŠธ๋ฒˆํ˜ธ ํ™•์ธ
mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
- ์œ ์ € ์ƒ์„ฑ
mysql> CREATE USER tomcatuser@10.0.118.76 identified by 'P@ssw0rd';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER tomcatuser@10.0.118.85 identified by 'P@ssw0rd';
Query OK, 0 rows affected (0.00 sec)

- ์ƒ์„ฑํ•œ ์œ ์ €์— ๋Œ€ํ•˜์—ฌ SELECT ๊ถŒํ•œ ๋ถ€์—ฌ
mysql> grant select on *.* to tomcatuser@10.0.118.76 identified by 'P@ssw0rd';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant select on *.* to tomcatuser@10.0.118.85 identified by 'P@ssw0rd';
Query OK, 0 rows affected, 1 warning (0.00 sec)

- ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ ์šฉ
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

WAS ์„œ๋ฒ„์—์„œ ์ ‘์†ํ•  tomcat ์œ ์ € ๊ณ„์ •์„ ๋งŒ๋“ ๋‹ค

๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ”์— ๋Œ€ํ•˜์—ฌ SELECT ๊ถŒํ•œ๋งŒ ๋ถ€์—ฌํ•œ๋‹ค

- mysql ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ ํƒ
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

- user ํ…Œ์ด๋ธ”์—์„œ ์ƒ์„ฑํ•œ ์œ ์ € ํ™•์ธ
mysql> select user,host from user;
+---------------+-------------+
| user          | host        |
+---------------+-------------+
| tomcatuser    | 10.0.118.76 |
| tomcatuser    | 10.0.118.85 |
| mysql.session | localhost   |
| mysql.sys     | localhost   |
| root          | localhost   |
+---------------+-------------+
5 rows in set (0.00 sec)

 

13-5 MySQL ์›๊ฒฉ์ ‘์† ์„ค์ •

# netstat -ntlp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      3407/mysqld

Mysql server 3306ํฌํŠธ์— ๋Œ€ํ•ด ipv6๋งŒ ์—ด๋ ค์žˆ๋Š” ์ƒํƒœ์ด๋‹ค

- ์„ค์ •ํŒŒ์ผ ์ˆ˜์ •
# vi /usr/local/mysql57/my.cnf
..
bind-address = 0.0.0.0
..

- ์„œ๋น„์Šค ์žฌ์‹œ์ž‘
# service mysqld57 restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

- ํฌํŠธ ํ™•์ธ
# netstat -ntlp | grep mysqld
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4338/mysqld

ipv4 3306ํฌํŠธ๊ฐ€ ์—ด๋ฆฐ ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค

 

13-6 MySQL Table ์ƒ์„ฑ

- mysql ์ ‘์†
# mysql -u root -p

- ์ƒˆ DB ์ƒ์„ฑ
mysql> create database TOMCATDB;

- ์ƒ์„ฑํ•œ DB ์„ ํƒ
mysql> use TOMCATDB;
- TABLE ์ƒ์„ฑ
mysql> CREATE TABLE TESTTABLE( id Int(11) NOT NULL AUTO_INCREMENT, name VARCHAR(20) NOT NULL, location VARCHAR(20) NOT NULL, CONSTRAINT TESTTABLE_PK PRIMARY KEY(id) );

TESTTABLE ํ…Œ์ด๋ธ”์— id, name, location ์ปฌ๋Ÿผ์„ ์ƒ์„ฑํ•œ๋‹ค

์ œ์•ฝ ์กฐ๊ฑด์˜ ์ด๋ฆ„์€ TESTTABLE_TK์ด๋ฉฐ, PRIMARY KEY๋ฅผ id ์ปฌ๋Ÿผ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ์ œ์•ฝ ์กฐ๊ฑด์ด๋‹ค

AUTO INCREMENT : ์ž๋™์œผ๋กœ ๊ฐ’์ด 1์”ฉ ์ฆ๊ฐ€

NOT NULL : ๊ฐ’์„ ๋น„์›Œ๋‘˜ ์ˆ˜ ์—†๋‹ค

PRIMARY KEY : ํ•ด๋‹น ์—ด์ด ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ, ์ค‘๋ณต๋œ ๊ฐ’์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋„๋ก ๋ณด์žฅํ•œ๋‹ค

- ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ” ํ™•์ธ
mysql> desc TESTTABLE;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | NO   |     | NULL    |                |
| location | varchar(20) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
- ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€
mysql> insert into TESTTABLE(name,location) values('suah','noryangjin');

mysql> insert into TESTTABLE(name,location) values('dani','yeonsugu');

mysql> insert into TESTTABLE(name,location) values('mungi','sinlim');

- ํ…Œ์ด๋ธ” ํ™•์ธ
mysql> select * from TESTTABLE;
+----+-------+------------+
| id | name  | location   |
+----+-------+------------+
|  1 | suah  | noryangjin |
|  2 | dani  | yeonsugu   |
|  3 | mungi | sinlim     |
+----+-------+------------+
3 rows in set (0.00 sec)

 

 

14. WAS-DB

14-1 MySQL Connector ์„ค์น˜

WAS ์„œ๋ฒ„์—์„œ MySQL Connector๋ฅผ ๋‹ค์šดํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ž„์‹œ๋กœ ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์œผ๋กœ ๋‚˜๊ฐ€๋Š” ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•œ๋‹ค

 

14-2 MySQL Connector ์••์ถ•ํŒŒ์ผ ์„ค์น˜ ๋ฐ ํ•ด์ œ

- root ๊ณ„์ • ์ ‘์†
$ sudo su -

- ์„ค์น˜ ๋ฐ ์••์ถ•ํ•ด์ œ
# wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.23.tar.gz
# tar xvfz mysql-connector-java-8.0.23.tar.gz

- jar ํŒŒ์ผ ํ™•์ธ
# cd mysql-connector-java-8.0.23/
# ls
CHANGES   INFO_SRC  README     mysql-connector-java-8.0.23.jar
INFO_BIN  LICENSE   build.xml  src

JAR ( Java Archive ) : ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž๋ฐ” Class ํŒŒ์ผ๊ณผ ๊ด€๋ จ ๋ฆฌ์†Œ์Šค ๋“ฑ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๋ชจ์•„์„œ

์ž๋ฐ” ํ”Œ๋žซํผ์— ์‘์šฉ ์†Œํ”„ํŠธ์›จ์–ด๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•œ ์†Œํ”„ํŠธ์›จ์–ด ํŒจํ‚ค์ง€ ํŒŒ์ผ ํฌ๋งท์ด๋‹ค

- jar ํŒŒ์ผ ๋ณต์‚ฌ
# cp -a mysql-connector-java-8.0.23.jar /usr/lib/jvm/jre/lib/ext/
# cp -a mysql-connector-java-8.0.23.jar /usr/local/tomcat9.0/lib/

 

14-3 Tomcat ์„ค์ •

# vi /usr/local/tomcat9.0/conf/context.xml

<Context>
...
	<Resource name="jdbc/test"
        	auth="Container"
        	type="javax.sql.DataSource"
        	username="tomcatuser"		//MySQL ์ ‘์† ๊ณ„์ •
        	password="P@ssw0rd"		//MySQL ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ
        	driverClassName="com.mysql.jdbc.Driver"
        	url="jdbc:mysql://10.0.118.104:3306/TOMCATDB" //MySQL ์„œ๋ฒ„์ฃผ์†Œ์™€ DB๋ช…
        	maxActive="15"
        	maxIdle="3"/>
...
</Context>

<Context>๋ž€ ์•ˆ์— ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

context.xml์€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ž์›์„ ๋ช…์‹œํ•ด์ฃผ๋Š” ํŒŒ์ผ์ด๋‹ค

์œ„ ๊ณผ์ •์—์„œ MySQL tomcat ๊ณ„์ •์„ ์ƒ์„ฑํ–ˆ๋˜ ์ •๋ณด ๊ทธ๋Œ€๋กœ ์ž…๋ ฅํ•œ๋‹ค

(username, password, url ํ•ญ๋ชฉ)

# vi /usr/local/tomcat9.0/conf/web.xml

<web-app>
 ...
 <resource-ref>
                <res-ref-name>jdbc/test</res-ref-name>
                <res-type>javax.sql.DataSource</res-type>
                <res-auth>Container</res-auth>
 </resource-ref>
 ...
</web-app>

<web-app>๋ž€ ์•ˆ์— ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค

web.xml์€ Tomcat์˜ ์‹คํ–‰ ํ™˜๊ฒฝ ์ •๋ณด๊ฐ€ ์žˆ๋Š” ํ™˜๊ฒฝ์„ค์ • ํŒŒ์ผ๋กœ

Tomcat์˜ ๋ชจ๋“  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ธฐ๋ณธ ์„ค์ •์„ ์ •์˜ํ•˜๋Š” ํŒŒ์ผ์ด๋‹ค

- ์„œ๋น„์Šค ์žฌ์‹œ์ž‘
# systemctl restart tomcat

 

14-4 Web Page ์ž‘์„ฑ

# vi /usr/local/tomcat9.0/webapps/ROOT/index.jsp

<%@ page import = "java.sql.*" %>
<%
  try{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://10.0.118.104:3306/TOMCATDB";		//DB์„œ๋ฒ„ ip, DB๋ช… ์ž…๋ ฅ
        Connection conn = DriverManager.getConnection(url, "tomcatuser", "P@ssw0rd");	//MySQL ๊ณ„์ •๊ณผ PW

        out.println("SHOW NAME AND LOCATION - WEB 1");
        out.println("<BR>");
        out.println("-------------------------------");
        out.println("<BR>");
        PreparedStatement ps=(PreparedStatement)conn.prepareStatement("SELECT * from TESTTABLE");
        ResultSet rs=ps.executeQuery();

        while(rs.next()){

                String name=rs.getString("name");
                String location=rs.getString("location");

                out.println("name : "+ name);
                out.println("<BR>");
                out.println("location : "+location);
                out.println("<BR>");
                out.println("-----------------------------");
                out.println("<BR>");

        }

  }
  catch(Exception e){
        out.print(e.toString());
  }

%>

MySql ์„œ๋ฒ„์— ์›๊ฒฉ์œผ๋กœ Select Query ๋ฌธ์„ ๋ณด๋‚ด์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ•œ๋‹ค

- ์„œ๋น„์Šค ์žฌ์‹œ์ž‘
# systemctl restart tomcat

 

14-5 ๋ณด์•ˆ๊ทธ๋ฃน ๊ทœ์น™ ์‚ญ์ œ

๋ชจ๋“  ๊ตฌ์ถ•์ด ๋๋‚ฌ์œผ๋ฏ€๋กœ, WAS ์„œ๋ฒ„์˜ ์•„์›ƒ๋ฐ”์šด๋“œ์—์„œ ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์œผ๋กœ ๋‚˜๊ฐ€๋Š” ์•„์›ƒ๋ฐ”์šด๋“œ ๊ทœ์น™์„ ์‚ญ์ œํ•œ๋‹ค

 

14-6 ์—ฐ๋™ ํ…Œ์ŠคํŠธ

 DB์— ๋„ฃ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž˜ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค

WAS-DB ์—ฐ๋™์— ์„ฑ๊ณตํ•˜์˜€๋‹ค

 

14-7 ์ถ”๊ฐ€ ํ…Œ์ŠคํŠธ

- ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
mysql> insert into TESTTABLE(name,location) value('nengin','incheon');

- ํ™•์ธ
mysql> select * from TESTTABLE;
+----+--------+------------+
| id | name   | location   |
+----+--------+------------+
|  1 | suah   | noryangjin |
|  2 | dani   | yeonsugu   |
|  3 | mungi  | sinlim     |
|  4 | nengin | incheon    |
+----+--------+------------+

๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•œ๋‹ค

๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค

 

 

 

 

WEB-WAS-DB ๋ชจ๋“  ์—ฐ๋™์„ ์™„๋ฃŒํ–ˆ๋‹ค!

๋‹ค์Œ์€ ๊ตฌ์ถ• ํ๋ฆ„๋„์™€ ๊ฒฐ๊ณผ ์ •๋ฆฌ์œผ๋กœ ์ด์–ด์ง‘๋‹ˆ๋‹ค