如何通过typesafe配置包配置slick 3.2连接到mysql?

时间:2017-03-24 11:15:57

标签: mysql scala slick

我尝试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属性,但应该添加什么值?

============ UPDATE ======
配置属性在括号内:

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

3 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我奋斗了几天,发现配置可行。

  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.