Spring Boot 프로젝트에서 Docker Compose로 MySQL을 띄우고 연결하려 했는데, 아래 에러가 계속 발생했다.
Caused by: java.sql.SQLSyntaxErrorException: Access denied for user 'dev'@'localhost' to database 'ai_english'
Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
권한 부여(GRANT ALL PRIVILEGES)도 해보고, IP를 172.18.0.2로 직접 지정도 해봤지만 근본적으로 해결되지 않았다.
docker-compose up -d
를 실행했을 때 아래 에러가 나왔다.
Error response from daemon: ports are not available: exposing port TCP 127.0.0.1:3306 -> 127.0.0.1:0: listen tcp4 127.0.0.1:3306: bind: address already in use
Mac에 MySQL이 직접 설치되어 있었고, 이미 3306 포트를 점유하고 있었다.
Docker MySQL도 3306 포트를 사용하려 했기 때문에 충돌이 발생했고, Docker 컨테이너가 제대로 뜨지 않았던 것이다.
brew services list | grep mysql
# mysql started ...
brew services stop mysql
docker-compose up -d
Docker로 MySQL을 관리할 것이라면 로컬 MySQL은 꺼두는 것이 깔끔하다.
docker-compose.yml에서 포트를 변경한다.
mysql:
ports:
- "3307:3306"
application.yml도 맞춰서 변경한다.
spring:
datasource:
url: jdbc:mysql://localhost:3307/ai_english
나는 방법1, 로컬 MySQL을 중지해서 해결했다.
Access denied, Communications link failure 에러가 권한 문제처럼 보여도 실제로는 컨테이너 자체가 제대로 안 뜬 것일 수 있다.docker ps로 컨테이너 상태부터 확인하는 것이 중요하다.당연하게 MySQL을 설치, 실행한 것이 문제가 될 줄은 몰랐다.
# 컨테이너 상태 확인 습관 들이기
docker ps
docker-compose up -d
brew 로 로컬 MySQL 설치되어 있던 상태