kotlin - TestContainers 和 Mysql : Access denied for user '' @'172.17.0.1' (using password: NO)

标签 kotlin testcontainers kotlin-exposed kotest

我编写了一个简单的存储库来测试 Kotlin Exposed with TestContainers。我使用的数据库是mysql。
这是我的代码:

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()
      }
}
 
代码在 beforeSpec 处失败
Access denied for user ''@'172.17.0.1' (using password: NO)
也许我错过了一些东西,任何帮助将不胜感激
我使用的库:
  • kotest
  • kotest-extension-testcontainers
  • 测试容器
  • testcontainers-mysql
  • 最佳答案

    我终于找到了我的问题的答案。原因是我忘记将用户名和密码传递给与 Exposed 库的连接。

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

    关于kotlin - TestContainers 和 Mysql : Access denied for user '' @'172.17.0.1' (using password: NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63957420/

    相关文章:

    spring - 如何使用Spring Boot和Spock运行测试容器

    google-cloud-sql - 如何通过Expedia连接到Google Cloud SQL

    kotlin - 如何在 Kotlin Exposed 中获取插入的行?

    android - 为什么最大长度过滤器不起作用?安卓

    kotlin - 我如何从挂起功能中使用当前的 CoroutineScope `CoroutineScope.async`?

    java - 使用 testcontainers 使用自定义端口运行 ES docker 镜像

    spring-boot - KafkaContainer - 如何在 start() 之后在 Spring Boot 中读取 kafka 容器端口作为属性/如何在启动之前配置 Kafka 端口

    android - 错误 : Parceler: Unable to find read/write generator for type io. Realm .RealmList

    java - Spring mongodb查询包含嵌套数组的嵌套数组

    kotlin - 如何使用Exposed和Kotlin在db中设置当前日期