728x90
문제
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.'
MongoDB 서버에 인증 정보를 사용하여 로그인 시도가 실패했음을 의미한다. 오류 메시지는 "Authentication failed."로, 인증이 실패했다는 것을 나타낸다.
해결
application.yml 파일에서 접속 URI를 바꾸어 주었다.
변경 전
spring:
application:
name: chatting-service
data:
mongodb:
username: root
password: 1234
host: 35.192.--.---
port: 27017
database: chat_app
authentication-database: admin
변경 후
spring:
application:
name: chatting-service
data:
mongodb:
uri: mongodb://root:1234@35.192.--.---:27017/chat_app?authSource=admin
MongoDB 설정을 spring.data.mongodb.uri를 사용하여 URI 형태로 지정하는 것이 일반적이라고 한다.. 이렇게 하면 연결 문자열을 직접 관리할 수 있다.