Docker를 활용해 MySQL을 생성하고 Docker ps 명령어로 정상적으로 실행되는 걸 확인했다면 이제 Spring과 연결을 할 수 있습니다.
하지만 저는 연결하는 과정에서 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)라는 에러를 직면했습니다. 이 에러는 비밀번호를 틀렸을 경우에도 나올 수 있습니다.
하지만 저는 아래의 명령어로 생성했고 비밀번호는 1234였습니다.
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 --name mysql mysql
이 과정에서 yml파일의 경우 아래와 같이 등록이 되어 있었기에 비밀번호와 url 모두 맞는 것을 알 수 있습니다.
spring:
jpa:
hibernate:
ddl-auto: create
show-sql: true
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/stock_example
username: root
password: 1234
# JPA 쿼리가 어떻게 나가는지
logging:
level:
org:
hibernate:
SQL: DEBUG
type:
descriptor:
sql:
BasicBinder: TRACE
이 내용을 해결하는 법은 어렵지 않습니다.
1.
저 아래 같은 경우에는 터미널에서 관리자 계정으로 접속후 사용하였습니다.
터미널에서
docker exec -it mysql bash
mysql -u root -p
비밀번호에 입력
use {사용할 데이터베이스 명};
위의 과정을 거친 후에 서버를 가동시키면 잘 작동하는 것을 알 수 있습니다.
2.
Mac 기준이며 아래와 같이 3306Port가 먼저 점유하고 있는지 확인합니다.
확인을 했다면 sudo kill {port id}로 삭제를 하면 되는데 저 같은 경우에는 아래와 같이 kill 명령어로 삭제를 했음에도 계속 mysqld가 살아나는 것을 볼 수 있었습니다. 그렇기 때문에 Docker Container가 이미 작동 중인걸 확인하고 Spring을 실행시에도 Docker에 깔린 mysql port를 인식을 하지 못하고 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 에러가 발생하게 됩니다.
그렇다면 왜 이런 일이 발생하게 될까요? 아래의 마지막 줄 Error를 보게 되면 exposing port TCP 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306: bind: address already in use 이미 mysql port 번호인 3306이 사용하고 있다는 말이 나옵니다.
그 말은 brew에서 즉 Local에서 동작중인 MySQL이 죽지 않고 돌고 있다는 것을 의미합니다. 아래와 같은 오류도 볼 수 있죠
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
그렇다면 간단하게 말하자면 Local에서 돌고 있는 MySQL을 멈추면 kill명령어로 삭제할 수 있다는 소리가 됩니다.
아래와 같이 먼저 MySQL을 멈추고 kill 명령어로 삭제한 후 한 번 더 3306 port를 확인하게 되면? 아래와 같이 정상적으로 삭제하고 docker도 작동시킬 수 있게 됩니다.
그렇다면 성공적으로 docker가 실행했고 docker에서 띄운 MySQL이 인식이 된다면 성공적으로 해결했다고 볼 수 있습니다.
성공적으로 아래와 같이 작동하는 것을 알 수 있습니다. 주의할 점은 만약 docker에 설치한 MySQL을 사용하지 않고 Local에 있는 MySQL을 다시 사용하고 싶다면 Local에 중지시켜둔 MySQL을 다시 실행시켜야 yml이나 properties에 적어둔 url이 인식될 수 있습니다.
/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home/bin/java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dmanagement.endpoints.jmx.exposure.include=* -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49535:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/igwangmin/IdeaProjects/Mikor/out/production/classes:/Users/igwangmin/IdeaProjects/Mikor/out/production/resources:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.projectlombok/lombok/1.18.24/13a394eed5c4f9efb2a6d956e2086f1d81e857d9/lombok-1.18.24.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-data-jdbc/2.7.1/2916d532662c696b3d4960e8c17b7263afa3a004/spring-boot-starter-data-jdbc-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-data-jpa/2.7.1/39e98264c195a06c8c29a63c24b6ce295ca10795/spring-boot-starter-data-jpa-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-web/2.7.1/29f47f503f9955b1a9746870aeaebdba448416d/spring-boot-starter-web-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-jdbc/2.7.1/bba8a72a8042cec0b7961f11b0b3332724f5845e/spring-boot-starter-jdbc-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-jdbc/2.4.1/60e1954e464b4864b6dc31d95e49470c50775176/spring-data-jdbc-2.4.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-aop/2.7.1/e7c78497e3528ae3037c723259b4b0944994f03/spring-boot-starter-aop-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/jakarta.transaction/jakarta.transaction-api/1.3.3/c4179d48720a1e87202115fbed6089bdc4195405/jakarta.transaction-api-1.3.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/jakarta.persistence/jakarta.persistence-api/2.2.3/8f6ea5daedc614f07a3654a455660145286f024e/jakarta.persistence-api-2.2.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.hibernate/hibernate-core/5.6.9.Final/8ec2c7b13de2fbcb19feddfb3a30932bb6a8228a/hibernate-core-5.6.9.Final.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-jpa/2.7.1/9cbefcab47d47a64afeca24b5fbe7328dd08ef9b/spring-data-jpa-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.3.21/9e8271b86c5be0388c2c34f2c5c3d070af06cd2/spring-aspects-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-json/2.7.1/711889df8474d7f0271b1e25cd75a9249e0a4621/spring-boot-starter-json-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter/2.7.1/48f7e04459ccc16d3532bfc486c1b6d629e6e0fc/spring-boot-starter-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-tomcat/2.7.1/c99fe94b685f1707907afb84ecb998ac13271ead/spring-boot-starter-tomcat-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-webmvc/5.3.21/a62db425cc29c48e138846e706ca37acb138ca13/spring-webmvc-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-web/5.3.21/317aadd37f70ba34ff93d068343e3110b5dcf2f/spring-web-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.zaxxer/HikariCP/4.0.3/107cbdf0db6780a065f895ae9d8fbf3bb0e1c21f/HikariCP-4.0.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jdbc/5.3.21/68f9b9a9b1541bdb40a7187f5eb96e3af6c27b40/spring-jdbc-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-relational/2.4.1/6ada9d62a794a7d64b9ab14f2feb2030861a231/spring-data-relational-2.4.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-commons/2.7.1/ed6cd9f72f7712ea1a2c56c3affdba3c4f3948a8/spring-data-commons-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-tx/5.3.21/13f4f564024d2f85502c151942307c3ca851a4f7/spring-tx-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-context/5.3.21/fe371c85f02b8c6690fc3b3d0950ef4f965db0cd/spring-context-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-beans/5.3.21/e3eae7e6d211381642a0b7507a5215e3ac1b32e1/spring-beans-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.3.21/1b0c9be6b972e4c615f175c70fc32e80557e68e8/spring-core-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.36/6c62681a2f655b49963a5983b8b0950a6120ae14/slf4j-api-1.7.36.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aop/5.3.21/58ec4ff7a0ce30a1e2612f04ad0fb13ea806705/spring-aop-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjweaver/1.9.7/158f5c255cd3e4408e795b79f7c3fbae9b53b7ca/aspectjweaver-1.9.7.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.hibernate.common/hibernate-commons-annotations/5.1.2.Final/e59ffdbc6ad09eeb33507b39ffcf287679a498c8/hibernate-commons-annotations-5.1.2.Final.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.4.3.Final/c4bd7e12a745c0e7f6cf98c45cdcdf482fd827ea/jboss-logging-3.4.3.Final.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy/1.12.11/ebeebf10ea48f997da583cdb6ce861eee279a507/byte-buddy-1.12.11.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/antlr/antlr/2.7.7/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.jboss/jandex/2.4.2.Final/1e1c385990b258ff1a24c801e84aebbacf70eb39/jandex-2.4.2.Final.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml/classmate/1.5.1/3fe0bed568c62df5e89f4f174c101eab25345b6c/classmate-1.5.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.glassfish.jaxb/jaxb-runtime/2.3.6/1e6cd0e5d9f9919c8c8824fb4d310b09a978a60e/jaxb-runtime-2.3.6.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-orm/5.3.21/388e8c494b667df092817bd1bf8e588643bcd253/spring-orm-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.13.3/ad2f4c61aeb9e2a8bb5e4a3ed782cfddec52d972/jackson-datatype-jsr310-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.module/jackson-module-parameter-names/2.13.3/f71c4ecc1a403787c963f68bc619b78ce1d2687b/jackson-module-parameter-names-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.datatype/jackson-datatype-jdk8/2.13.3/d4884595d5aab5babdb00ddbd693b8fd36b5ec3c/jackson-datatype-jdk8-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.13.3/56deb9ea2c93a7a556b3afbedd616d342963464e/jackson-databind-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.1/923ad789b004e8cc17d67853b1e4d3db11946f0/spring-boot-autoconfigure-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.7.1/8e49b8e7e9ea470a7772f489532264732ab206a2/spring-boot-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-starter-logging/2.7.1/461cf82dc10505f47d3ce2146bd01721177cde4a/spring-boot-starter-logging-2.7.1.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/jakarta.annotation/jakarta.annotation-api/1.3.5/59eb84ee0d616332ff44aba065f3888cf002cd2d/jakarta.annotation-api-1.3.5.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.yaml/snakeyaml/1.30/8fde7fe2586328ac3c68db92045e1c8759125000/snakeyaml-1.30.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.apache.tomcat.embed/tomcat-embed-websocket/9.0.64/2a5e4f1f04830f2bfd01108ddc59a451c4baef34/tomcat-embed-websocket-9.0.64.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.apache.tomcat.embed/tomcat-embed-core/9.0.64/2d91a06d1b93ba13a2cca9e9ea7c143a64037351/tomcat-embed-core-9.0.64.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.apache.tomcat.embed/tomcat-embed-el/9.0.64/227363669235feab54519102af723a54d1a7850e/tomcat-embed-el-9.0.64.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-expression/5.3.21/ca8c5822fc528066ec717f1e74160a1575c43192/spring-expression-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.springframework/spring-jcl/5.3.21/b41a2888c0e708f9fd12cf9cc0c29cebbcab2e5e/spring-jcl-5.3.21.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/jakarta.xml.bind/jakarta.xml.bind-api/2.3.3/48e3b9cfc10752fba3521d6511f4165bea951801/jakarta.xml.bind-api-2.3.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.glassfish.jaxb/txw2/2.3.6/45db7b69a8f1ec2c21eb7d4fc0ee729f53c1addc/txw2-2.3.6.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.sun.istack/istack-commons-runtime/3.0.12/cbbe1a62b0cc6c85972e99d52aaee350153dc530/istack-commons-runtime-3.0.12.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.13.3/7198b3aac15285a49e218e08441c5f70af00fc51/jackson-annotations-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.13.3/a27014716e4421684416e5fa83d896ddb87002da/jackson-core-2.13.3.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.11/4741689214e9d1e8408b206506cbe76d1c6a7d60/logback-classic-1.2.11.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-to-slf4j/2.17.2/17dd0fae2747d9a28c67bc9534108823d2376b46/log4j-to-slf4j-2.17.2.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.slf4j/jul-to-slf4j/1.7.36/ed46d81cef9c412a88caef405b58f93a678ff2ca/jul-to-slf4j-1.7.36.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.2.11/a01230df5ca5c34540cdaa3ad5efb012f1f1f792/logback-core-1.2.11.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-api/2.17.2/f42d6afa111b4dec5d2aea0fe2197240749a4ea6/log4j-api-2.17.2.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/mysql/mysql-connector-java/8.0.29/16bfffda393ac4fe56f0985f1f035b37d3fc48f/mysql-connector-java-8.0.29.jar:/Users/igwangmin/.gradle/caches/modules-2/files-2.1/com.sun.activation/jakarta.activation/1.2.2/74548703f9851017ce2f556066659438019e7eb5/jakarta.activation-1.2.2.jar com.shop.mikor.MikorApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2024-04-25 21:53:10.378 INFO 11304 --- [ main] com.shop.mikor.MikorApplication : Starting MikorApplication using Java 11.0.22 on igwangmin-ui-MacBookPro.local with PID 11304 (/Users/igwangmin/IdeaProjects/Mikor/out/production/classes started by igwangmin in /Users/igwangmin/IdeaProjects/Mikor)
```
```
```
```
2024-04-25 21:53:11.460 INFO 11304 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2024-04-25 21:53:11.464 INFO 11304 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-04-25 21:53:11.680 WARN 11304 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2024-04-25 21:53:11.858 INFO 11304 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-04-25 21:53:11.864 INFO 11304 --- [ main] com.shop.mikor.MikorApplication : Started MikorApplication in 1.646 seconds (JVM running for 1.948)
다만 저는 제가 해결한 방법만을 알려드린 점 알아주시고 저와 같은 오류에도 다른 방법으로 해결 될 수 있다는 점 알아주시길 바라겠습니다.
Docker로 이미지를 생성하는 부분은 아래의 링크를 확인시 사용할 수 있습니다.
https://numchar.tistory.com/56
Mac에서 도커 적용하기
mac에서 도커를 사용하기 위해서는 여러 가지 방법이 있지만 이 방법은 homebrew를 이용한 방법임을 알아주시길 바랍니다. homebrew 설치 법은 아래의 사이트에 들어가서 다운로드 방법이 잘 나와 있
numchar.tistory.com
https://numchar.tistory.com/57
Docker Port가 겹친다면? (docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:3306 -> 0.0
Docker를 활용해 image를 생성하던 와중 아래와 같이 Port가 충돌하는 상황이 발생했다. igwangmin@igwangmin-ui-MacBookPro ~ % docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 --name mysql mysql Unable to find image 'mysql:lates
numchar.tistory.com
참고 자료
https://superuser.com/questions/1695533/mysql-process-wont-die-on-mac
mysql process won't die on mac
I have a problem in the setup of mysql using homebrew and so I decided to uninstall and start with a clean slate. Following the answer here I try to kill the process by Getting the process id thro...
superuser.com
'Spring' 카테고리의 다른 글
레이스 컨디션이란? (0) | 2024.04.18 |
---|---|
Spring Security Request method 'GET' not supported 에러 (0) | 2023.01.27 |
@NoArgsConstructor(access = AccessLevel.PROTECTED) (0) | 2023.01.26 |
Spring boot Mysql 삽질 (0) | 2023.01.18 |
Spring boot Mysql dbeaver연동 삽질 (0) | 2023.01.17 |