[Spring Boot + Kotlin] MariaDB 설치 및 연동하기

임경섭·2024년 6월 28일
1
post-thumbnail

리눅스 서버에 MariaDB를 설치하고, 설치된 MariaDB을 연동하는 방법에 대해 알아보겠다.

MariaDB 설치

// 설치
sudo apt-get update
sudo apt-get install mariadb-server

설치 과정중에 비밀번호 설정이 뜰 것이다.

Enter current password for root(enter for none):
// 서비스 시작 및 확인
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo systemctl status mariadb

sudo systemctl status mariadb를 확인했을때 별 다른 에러메시지가 없다면 정상적으로 실행된 것이다.

아래 여러 항목에 대해서 묻는데 원하는 취향에 따라 설정해주면 된다.

// 패스워드 입력
// 초기에 설정한 패스워드를 입력해주었다.
New password:
Re-enter new password:

// anonymous 계정을 삭제하겠습니까?
Remove anonymous users?

// 원격으로 root 로그인을 허용하지 않습니까?
Disallow root login remotely?

// test database를 삭제하겠습니까?
Remove test database and access to it?

// 지금 권한 테이블을 다시 로드하시겠습니까?
Reload privilege tables now?

아래 명령어로 mariaDB에 접속이 가능하다.

sudo mysql -u root -p

MariaDB 연동하기

  1. 먼저 build.gradle.kts 파일을 설정해준다.
plugins {
    id("org.springframework.boot") version "3.3.1"
    id("io.spring.dependency-management") version "1.1.5"
    kotlin("plugin.jpa") version "1.9.24"
    kotlin("jvm") version "1.9.24"
    kotlin("plugin.spring") version "1.9.24"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("io.github.cdimascio:dotenv-kotlin:6.2.2")
    implementation("org.mariadb.jdbc:mariadb-java-client")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
    runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll("-Xjsr305=strict")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}
  1. 데이터베이스 설정
    application.yml이나 application.properties를 작성한다.
// application.properties

spring.application.name=kotlin_crud
spring.datasource.url=jdbc:mariadb://localhost/crud
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDB106Dialect

ddl-auto는 DDL(Data Definition Language)을 자동으로 작성하여 테이블을 생성하거나 수정해주는 속성이다.

show-sql은 콘솔로그로 sql문을 확인할 수 있다.

format_sql은 sql문을 포맷팅해서 보기 편하게 만들어준다.

서버의 데이터베이스를 사용한다면, localhost에 서버 주소를 입력하면 된다.
localhost뒤에는 사용할 데이터베이스 이름을 넣어주면 된다.
서버 주소, 데이터베이스 유저 이름, 패스워드는 노출될 수도 있기에 환경 변수로 사용하였다.

이제 애플리케이션을 실행하면 MariaDB와 연동된 Spring Boot를 사용할 수 있다.

profile
즐겁게 코딩 ૮₍ •̀ᴥ•́ ₎ა

1개의 댓글

comment-user-thumbnail
2024년 7월 2일

오 이제는 백엔드도 하시는군요!

답글 달기