지난 Apache/NginX-Tomcat와 Tomcat연동 포스팅에 이어 작성한다.

https://img.nuschool.co.kr/wp-admin/post.php?post=220648&action=edit
https://img.nuschool.co.kr/wp-admin/post.php?post=220665&action=edit

Apache와 연동이 되어있어도 좋고, NginX와 연동이 되어있어도 좋다. 여튼 WAS인 Tomcat과 DB가 연동되면 웹에 나타나게 하면 되는 것이다.

1. MariaDB 설치

1. 마리아 DB 홈페이지에서 버전을 선택해 다운로드하자.

마리아 DB 설치 버전

2. repository 설정

– 홈페이지에 있는대로 repo 설정을 넣는다.

cd /etc/yum.repos.d
vi MariaDB.repo
[mariadb]
name = MariaDB
# rpm.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# baseurl = https://rpm.mariadb.org/10.6/rhel/$releasever/$basearch
baseurl = https://tw1.mirror.blendbyte.net/mariadb/yum/10.6/rhel/$releasever/$basearch
module_hotfixes = 1
# gpgkey = https://rpm.mariadb.org/RPM-GPG-KEY-MariaDB
gpgkey = https://tw1.mirror.blendbyte.net/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1


yum clean all
yum repolist all

3. yum 설치

repo 설정 가이드 밑에 나온 명령을 입력하여 MairaDB를 yum 설치한다.

4. MariaDB 기본 설정(DB 기동 중 진행해야 함! mariadb-secure-installation)

mariadb-secure-installation       #명령실행

(중략)

Enter current password for root (enter for none):(엔터)
OK, successfully used password, moving on...

(중략)

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!

(중략)

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

(중략)

Remove anonymous users? [Y/n] y
 ... Success!

(중략)

Disallow root login remotely? [Y/n] y
 ... Success!

(중략)

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

(중략)

Reload privilege tables now? [Y/n] y
 ... Success!

(중략)

Mariadb 접속

mysql -u root -p
mariadb -u root -p <<< 이것도 가능


MariaDB [(none)]> status  <<<<< 상태 확인
--------------
mariadb  Ver 15.1 Distrib 10.6.15-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          15
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.6.15-MariaDB MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8mb3
Conn.  characterset:    utf8mb3
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 57 min 13 sec

Threads: 1  Questions: 30  Slow queries: 0  Opens: 21  Open tables: 14  Queries per second avg: 0.008

5. Tomcat과의 연동 진행

1. 테스트 DB 생성 + 테스트 Table 생성하기

# Test용 DB를 생성한다.
mysql -u root -p
Enter password: (암호 입력)

# DB 생성하기
MariaDB [(none)]> use mysql ;
MariaDB [mysql]> create database test1;

# 생성한 DB에 대한 권한 설정하기
MariaDB [mysql]> grant all privileges on *.* to 'root'@'%' identified by '(당신이 설정한 root의 패스워드)';
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

# 생성한 DB로 이동
MariaDB [mysql]> use test1;
Database changed

# Table 생성하기
MariaDB [test1]> create table table1 (id varchar(20) primary key, pw varchar(20));
Query OK, 0 rows affected (0.031 sec)

# Table에 data 입력
MariaDB [test1]> INSERT INTO table1 (id, pw) VALUES ('admin', 'admin123');
Query OK, 1 row affected (0.001 sec)

# Table 내용 조회
MariaDB [test1]> SELECT * FROM table1;
+-------+----------+
| id    | pw       |
+-------+----------+
| admin | admin123 |
+-------+----------+

2. Tomcat과 MariaDB 연동작업

MariaDB Connector를 Tomcat의 lib 디렉토리 안에 넣어주면 된다.

1) 아래 홈페이지 접속한다.

https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector

2) MariaDB Connector 파일(jar)을 서버로 다운로드 한다.

 Java 설치 버전

이렇게 하면 밑에 사진처럼 다운로드해야 할 소스 링크가 뜰 것이다.

3) MariaDB Connector 파일(jar)을 다운로드 한다.

# jar 파일 복사
cp -arp mariadb-java-client-2.7.10.jar  /home/tomcat1/apache-tomcat-10.1.16/lib
cp -arp mariadb-java-client-2.7.10.jar  /home/tomcat2/apache-tomcat-10.1.16/lib

3. DB연동 확인

1) Tomcat 설치 경로 내 webapps/ROOT/index.jsp 파일 내용을 아래와 같이 수정하자.

<%-- index.jsp 내용 --%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
 
<%
Connection conn = null;
String url = "jdbc:mariadb://localhost:3306/(생성한 DB명을 여기 입력)";  //우리는 앞에서 test1이라는 DB를 생성했다
String id = "root";							// 접속을 위한 계정의 ID
String pw = "(root 패스워드 여기 입력)";	    // 접속을 위한 root 계정의 암호
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(url, id, pw);
out.println("<h1>MariaDB DB 연결 성공</h1>");
%>
 
</body>
 
</html>

연동 화면

Leave a Reply

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다