ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Error] EmbeddedDatabase 사용 시 java.lang.RuntimeException: mysql start failed with error 해결
    Backend/Spring 2022. 8. 5. 15:15

    Error

    JUnit을 사용한 스프링 부트 테스트 시에 EmbeddedDatabase를 사용해 mysql을 연결하려고 했다.

        @Configuration
        @ComponentScan(
            basePackages= {"org.prgrms.kdt.customer"}
        )
        static class Config{
            @Bean
            public DataSource dataSource() {
                var dataSource = DataSourceBuilder.create()
                        .url("jdbc:mysql://localhost:2215/test-order_mgmt")
                        .username("test")
                        .password("0000")
                        .type(HikariDataSource.class)
                        .build();
                return dataSource;
            }
    
            @Bean
            public JdbcTemplate jdbcTemplate(DataSource dataSource) {
                return new JdbcTemplate(dataSource);
            }
        }
        Customer newCustomer;
    
        EmbeddedMysql embeddedMysql;
    
        @BeforeAll
        void setup() {
            newCustomer = new Customer(UUID.randomUUID(), "test-user", "test-user@gmail.com",
                    LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS));
            var mysqlConfig = aMysqldConfig(Version.v8_0_11)
                    .withCharset(UTF8)
                    .withPort(2215)
                    .withUser("test", "0000")
                    .withTimeZone("Asia/Seoul")
                    .build();
            embeddedMysql = anEmbeddedMysql(mysqlConfig)
                    .addSchema("test-order_mgmt", classPathScript("schema.sql"))
                    .start();
        }

     

    이때 다음과 같은 에러가 나면서 mysql start에 실패했다.

    java.io.IOException: java.lang.RuntimeException: mysql start failed with error: [ERROR] [MY-010338] [Server] Can't find error-message file 'C:\Users\MIRAE\CNU_LectureMaterial\share\errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
    2022-08-05T05:56:59.996507Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Users\MIRAE\CNU_LectureMaterial\data\DESKTO

     

     

    Solution

    기존에 내가 사용했던 mysql 버전은 v8_0_11로 이는 윈도우에서 잘 작동하지 않는 경우가 있다고 한다. 따라서 버전을 v5_7_10 으로 낮춰주었더니 연결에 성공했다.

    하지만 이때 주의할 점은 v8_0_11 해당 버전에서 사용가능한 sql 문법인 UUID_TO_BIN()이 v5_7_10 버전에서는 작동하지 않는다. 따라서 v5_7_10 버전을 사용할 때에는 다음과 같은 변경이 필요하다.

     

    v8_0_11

    @Override
        public Optional<Customer> findById(UUID customerId) {
            try {
                return Optional.ofNullable(
                        jdbcTemplate.queryForObject(
                                "select * from customers where customer_id = UUID_TO_BIN(?)",
                                customerRowMapper,
                                customerId.toString().getBytes())
                );
            } catch (EmptyResultDataAccessException e){
                logger.error("Got empty result ", e);
                return Optional.empty();
            }

     

    v5_7_10

        @Override
        public Optional<Customer> findById(UUID customerId) {
            try {
                return Optional.ofNullable(
                        jdbcTemplate.queryForObject(
                                "select * from customers where customer_id = UNHEX(REPLACE(?, '-',''))",
                                customerRowMapper,
                                customerId.toString())
                );
            } catch (EmptyResultDataAccessException e){
                logger.error("Got empty result ", e);
                return Optional.empty();
            }
        }

     

    댓글

Designed by Tistory.