Skip to content

Commit 4db63c9

Browse files
committed
feat: 디비에 데이터베이스가 없을시 자동 생성
1 parent e22c2a8 commit 4db63c9

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

.github/workflows/gradle.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ jobs:
8484
script: |
8585
cd /home/${{ secrets.SERVER_USER }}
8686
87+
# 데이터베이스 체크 및 생성
88+
echo "데이터베이스 SWYP8 확인 및 생성 중..."
89+
90+
# 데이터베이스 체크 스크립트 생성
91+
cat > check_db.sh << 'EOF'
92+
#!/bin/bash
93+
94+
# MySQL 접속 정보
95+
MYSQL_USER="root"
96+
MYSQL_PASSWORD="rootroot"
97+
MYSQL_HOST="localhost"
98+
DB_NAME="SWYP8"
99+
100+
# 데이터베이스 존재 여부 확인
101+
DB_EXISTS=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES LIKE '$DB_NAME';" 2>/dev/null | grep -c "$DB_NAME")
102+
103+
if [ "$DB_EXISTS" -eq 0 ]; then
104+
echo "데이터베이스 $DB_NAME이 존재하지 않습니다. 생성합니다..."
105+
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
106+
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$MYSQL_USER'@'%';"
107+
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH PRIVILEGES;"
108+
echo "데이터베이스 $DB_NAME이 성공적으로 생성되었습니다."
109+
else
110+
echo "데이터베이스 $DB_NAME이 이미 존재합니다."
111+
fi
112+
EOF
113+
114+
chmod +x check_db.sh
115+
sudo bash ./check_db.sh
116+
87117
# Java에 443 포트 바인딩 권한 부여
88118
sudo setcap 'cap_net_bind_service=+ep' /usr/lib/jvm/java-17-openjdk-amd64/bin/java
89119
@@ -233,6 +263,7 @@ jobs:
233263
--spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com \\
234264
--spring.mail.properties.mail.smtp.ssl.protocols=TLSv1.2 \\
235265
--spring.mail.properties.mail.debug=true \\
266+
--spring.datasource.url=jdbc:mysql://localhost:3306/SWYP8?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8&createDatabaseIfNotExist=true \\
236267
--cors.allowed-origins=https://locationcheckgo.netlify.app,http://localhost:8080,https://willgo6.duckdns.org,http://158.180.87.205:8080
237268
238269
Restart=always

Location-based-target-authentication/src/main/java/com/swyp/global/config/DatabaseInitializer.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.swyp.global.config;
22

33
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
46
import java.sql.Statement;
57

68
import javax.sql.DataSource;
@@ -20,9 +22,52 @@ public DatabaseInitializer(DataSource dataSource) {
2022

2123
@PostConstruct
2224
public void init() {
25+
// 데이터베이스 존재 확인 및 생성
26+
try {
27+
// 기본 MySQL 서버에 연결 (데이터베이스 지정 없이)
28+
String url = "jdbc:mysql://158.180.87.205:3306?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
29+
String username = "root";
30+
String password = "rootroot";
31+
32+
// MySQL 드라이버 로드
33+
try {
34+
Class.forName("com.mysql.cj.jdbc.Driver");
35+
} catch (ClassNotFoundException e) {
36+
System.err.println("MySQL JDBC 드라이버를 찾을 수 없습니다: " + e.getMessage());
37+
}
38+
39+
// MySQL 서버에 직접 연결
40+
try (Connection rootConnection = java.sql.DriverManager.getConnection(url, username, password);
41+
Statement rootStatement = rootConnection.createStatement()) {
42+
43+
// 데이터베이스 존재 여부 확인
44+
boolean dbExists = false;
45+
try (ResultSet rs = rootStatement.executeQuery("SHOW DATABASES LIKE 'SWYP8'")) {
46+
dbExists = rs.next();
47+
}
48+
49+
// 데이터베이스가 없으면 생성
50+
if (!dbExists) {
51+
System.out.println("데이터베이스 SWYP8이 존재하지 않습니다. 새로 생성합니다...");
52+
rootStatement.executeUpdate("CREATE DATABASE SWYP8 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
53+
rootStatement.executeUpdate("GRANT ALL PRIVILEGES ON SWYP8.* TO 'root'@'%'");
54+
rootStatement.executeUpdate("FLUSH PRIVILEGES");
55+
System.out.println("데이터베이스 SWYP8이 성공적으로 생성되었습니다.");
56+
} else {
57+
System.out.println("데이터베이스 SWYP8이 이미 존재합니다.");
58+
}
59+
} catch (SQLException e) {
60+
System.err.println("데이터베이스 생성 중 오류: " + e.getMessage());
61+
e.printStackTrace();
62+
}
63+
} catch (Exception e) {
64+
System.err.println("데이터베이스 초기화 중 예외 발생: " + e.getMessage());
65+
e.printStackTrace();
66+
}
67+
68+
// 기존 테이블 컬럼 수정 작업
2369
try (Connection connection = dataSource.getConnection();
2470
Statement statement = connection.createStatement()) {
25-
2671

2772
String[] sqls = {
2873
// 컬럼 수정

Location-based-target-authentication/src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ security.require-ssl=true
2525

2626

2727
# Database Configuration
28-
spring.datasource.url=jdbc:mysql://158.180.87.205:3306/SWYP8?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
28+
spring.datasource.url=jdbc:mysql://158.180.87.205:3306/SWYP8?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8&createDatabaseIfNotExist=true
2929
spring.datasource.username=root
3030
spring.datasource.password=rootroot
3131
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
32+
spring.datasource.initialization-mode=always
3233

3334
# JPA Configuration
3435
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)