When I worked on my computer at home, it worked well without any errors, but when I downloaded the project to my laptop through git, the error occurred as below.
2022-08-01 00:12:43.419 INFO 25004 --- [ main] c.p.r.mysqlcheck.MySqlConnectionTest : The following 1 profile is active: "config"
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
2
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles config are currently active).
I search on google and it saids that this error occur because of wrong data source information. But I checked data source several times but nothing's wrong.
So I think config profile file is not registered properly.
How can I solve this problem??
I'll show my code below.
application.properties
#Important information will be placed in here
spring.profiles.include=config
## I'm going to hide this file beacause it has personal information
#mybatis mapper location
mybatis.type-aliases-package=com.prac.react.model.dto
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
application-config.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kculter?serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=id
spring.datasource.password=1234
##db를 로컬 서버로 돌렸고 username과 pwd는 제 로컬에서 사용하는걸로 변경했습니다.
##db 서버 연결 테스트코드를 위한 변수를 만들었습니다.
mysqlusername=id
pwd=1234
Test Code
@SpringBootTest
public class MySqlConnectionTest {
private final String DRIVER = "com.mysql.jdbc.Driver"; //mysql 드라이버 생성 주소?
private final String URL = "jdbc:mysql://localhost:3306/kculter"; //mysql 주소
@Autowired
Mysql mysql;
Logger logger = LoggerFactory.getLogger(MySqlConnectionTest.class);
@Test
@DisplayName("MySql 연결 확인 테스트")
public void testConnection() throws Exception{
boolean flag = true;
logger.info(mysql.toString());
Class.forName(DRIVER); //위에서의 정보르들을 가지고 해당 driver를 JVM에 등록시키는것
try(Connection con = DriverManager.getConnection(URL,mysql.getUsername(),mysql.getPwd())){
logger.info(con.toString()); //콘솔창에서 연결정보
}catch(Exception e) {
logger.error("연결 실패");
flag = false;
assertTrue(flag);
}
}
}
Mysql.java
package com.prac.react.model.dto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Mysql {
@Value("${mysqlusername}")
private String username;
@Value("${pwd}")
private String pwd;
public Mysql() {}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "{" +
" username='" + getUsername() + "'" +
", pwd='" + getPwd() + "'" +
"}";
}
}