Search code examples
kotlintestcontainerskotlin-exposedkotest

TestContainers and Mysql : Access denied for user ''@'172.17.0.1' (using password: NO)


I wrote a simple repository to test Kotlin Exposed with TestContainers. The database that I use is mysql.

Here is my code :

class StudentsRepositoryTest: ShouldSpec({
     val container = getMysqlContainer()
     val mysqlJdbcUrl = container.jdbcUrl

     beforeSpec {
        Database.connect(mysqlJdbcUrl, "com.mysql.cj.jdbc.Driver")
        transaction {
            SchemaUtils.create(Students)
        }
     }

     ... // some tests


     private fun getMysqlContainer(): MySQLContainer<Nothing> {
       return MySQLContainer<Nothing>("mysql:5.7").apply {
          withUsername("root")
          withPassword("")
          withEnv("MYSQL_ROOT_PASSWORD", "%")
          withDatabaseName("test")
          start()
      }
}
 

The code fails at the beforeSpec

Access denied for user ''@'172.17.0.1' (using password: NO)

Maybe I am missing something, any help would be appreciated

The libraries that I used :

  • kotest
  • kotest-extension-testcontainers
  • testcontainers
  • testcontainers-mysql

Solution

  • I finally find the answer to my question. The cause is that I forgot to pass the username and password to the connection with Exposed library.

    val mysqlJdbcUrl = container.jdbcUrl
    val username = container.username
    val password = container.password
    Database.connect(mysqlJdbcUrl, "com.mysql.cj.jdbc.Driver", username, password)