【问题标题】:How to config slick 3.2 connect to mysql by typesafe config package?如何配置 slick 3.2 通过类型安全配置包连接到 mysql?
【发布时间】:2017-08-17 07:26:55
【问题描述】:

我尝试了official document for postgresql 并将org.postgresql.ds.PGSimpleDataSource 更改为slick.driver.MySQLDriver$

dataSourceClass = "slick.driver.MySQLDriver$"
properties = {
  databaseName = "mydb"
  user = "user"
  password = "password"
}
numThreads = 10

scala 代码:

val config = DatabaseConfig.forConfig[JdbcProfile]("slick.mysql.local")

输出错误:

Exception in thread "main" com.typesafe.config.ConfigException$Missing:  
No configuration setting found for key 'slick.mysql.local.profile'

似乎它需要一个profile 属性,但应该添加什么值?

============更新=========
配置属性在括号内:

slick.mysql.local {
    dataSourceClass = "slick.driver.MySQLDriver$"
    properties = {
      databaseName = "mydb"
      user = "user"
      password = "password"
    }
    numThreads = 10
}

【问题讨论】:

    标签: mysql scala slick


    【解决方案1】:

    如果您仔细阅读文档,他们所说的是:

    • 使用您的配置在某些application.conf 中定义mydb
    • 使用val db = Database.forConfig("mydb") 引用该配置

    所以我只能假设您丢失了 slick.mysql.local 密钥。

    【讨论】:

    • 嗨,我更改了 slick config 路径的根目录。完整的配置请参阅有问题的更新,谢谢。另外,还有一个疑惑,config没有指向mysql的主机和端口,能用吗?
    • 尝试在dataSourceClass 之后添加一个url 键,类似于jdbc:mysql://localhost:3306/test?user=root&password=password
    【解决方案2】:

    我还发现配置数据库连接器很困难。这是我的配置:

    mysql {
      dataSourceClass = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
      properties = {
        url = "jdbc:mysql://127.0.0.1:3306/dbName"
        databaseName = "dbName"  <= maybe not necessaray as also in the url
        user = "root"
        password = ""
      }
      numThreads = 10
    }
    

    感谢https://groups.google.com/forum/#!topic/scalaquery/WbR6yHzBj_8

    【讨论】:

      【解决方案3】:

      我挣扎了几天,发现配置可以工作。

        driver = "slick.driver.MySQLDriver$"
        db {
          url = "jdbc:mysql://127.0.0.1:3306/develop_tdsystem?user=user&password=password"
          driver = com.mysql.jdbc.Driver
          maxThreads = 10
        }
      

      请注意有一些警告:

      Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
      [warn] c.z.h.u.DriverDataSource - Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
      

      【讨论】:

        猜你喜欢
        • 2012-12-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        • 1970-01-01
        • 2020-05-03
        • 2020-05-13
        • 2018-09-26
        • 1970-01-01
        相关资源
        最近更新 更多