【问题标题】:TestContainers and Mysql : Access denied for user ''@'172.17.0.1' (using password: NO)TestContainers 和 Mysql:用户“@'172.17.0.1”的访问被拒绝(使用密码:否)
【发布时间】:2021-01-05 11:47:30
【问题描述】:

我编写了一个简单的存储库来使用 TestContainers 测试 Kotlin Exposed。我使用的数据库是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

【问题讨论】:

    标签: kotlin testcontainers kotlin-exposed kotest


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2015-10-07
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多