Spring Boot Mongodb

Environment

MongoDB : Version 4.0 (from brew)

Prepare MongoDB

Let’s start to use MongoDB
For Mac user, brew is easiest way to install mongodb.

brew install mongodb

Start with data directory(before it, please create empty directory named “mongodata”) blank is fine.

mongod --dbpath ./mongodata

Spring Boot Dependencies

Add following dependency to build.gradle

compile("org.springframework.boot:spring-boot-starter-data-mongodb")

Create User and Database Settings

Next is advanced topic. By default, library uses root user. It’s not good for security.
Create new user for this database

mongo
> use crm
> db.createUser({user: "guser", pwd:"password", roles:[{role:"readWrite", db:'crm'}]})

Create crm database and new user(name: guser, password: password) This has readWrite only for crm database.

Mongo side was done.
Next is setting for Spring Boot side.
Mongo setting should be in application.yml src/main/resources/application.yml

spring:
  data:
    mongodb:
      uri: mongodb://guser:password@localhost:27017/crm
未分類
スポンサーリンク
Professional Programmer2

コメント