1
votes

I'm trying to connect to MySQL in Playframework 2.4 (I created project play java).

I've installed mysql 5.7 in mac by brew and I created user and database.

And I added some configurations to application.conf and build.sbt in play.

Here's application.conf

    db.default.driver=com.mysql.jdbc.Driver
    db.default.url="jdbc:mysql://localhost/testplay"
    db.default.user="test"
    db.default.password= "test"

Here's buid.sbt.

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs,
 "mysql" % "mysql-connector-java" % "5.1.35"
)

I've done 'sbt' and 'activator run' in the project folder.

However when I connected to http://localhost:9000/ , it returned "configuration error cannot connect to database[db]".

the application log summury is

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [db]]


Caused by: play.api.Configuration$$anon$1: Configuration error[dataSource or dataSourceClassName or jdbcUrl is required.]
    at play.api.Configuration$.configError(Configuration.scala:154)
    at play.api.PlayConfig.reportError(Configuration.scala:996)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:70)
    at play.api.db.PooledDatabase.createDataSource(Databases.scala:199)
    at play.api.db.DefaultDatabase.dataSource$lzycompute(Databases.scala:123)
    at play.api.db.DefaultDatabase.dataSource(Databases.scala:121)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:44)
    ... 66 common frames omitted


Caused by: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
    at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:784)
    at play.api.db.HikariCPConfig.toHikariConfig(HikariCPModule.scala:141)
    at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:57)
    at play.api.db.HikariCPConnectionPool$$anonfun$1.apply(HikariCPModule.scala:54)
    at scala.util.Try$.apply(Try.scala:192)
    at play.api.db.HikariCPConnectionPool.create(HikariCPModule.scala:54

Would you tell me how I can solve this problem?

3
I don't mean to patronize but MySQL is running right? What version of Play, and I assume you are using Play-Java?jacks
Thanks for you commets! Play version is 2.4 and yes ,I 'm using Play-Java.I thought MySQL is running .. (I checked it by ps aux | grep mysqld ) But, there may be some problem in MySQL.user3119018
your config and dependencies look Ok so I would first rule out that MySQL is ok, and your testplay schema and credentials are correct.jacks
I see.Username and password are correct ,and privileges on testplay is also ok.And,I tried 127.0.0.1 instead of localhost but i didn't work..user3119018
I think SQL+ command line client ships with MySql - you could try connecting to your schema via that client to check its not your Play app.jacks

3 Answers

2
votes

The correct key to specify the user name in Play 2.4 and newer is username, not user. You can get more info in the JDBC Settings - Reference section.

db.default.username="test"

With 2.3 and probably older versions, the key you used (user) was the correct one.

1
votes

From the stacktrace, it seems like the configuration parameter is no longer just url but `jdbcUrl. Try this:

...
db.default.jdbcUrl="jdbc:mysql://localhost/testplay"
// you may also need to change user to username
db.default.username=test
...
1
votes

I think that the proper connection url to mySql should contain user name and password, in spite they are defined separately - at least this is what I am doing. In your case this is something like this:

db.default.driver=com.mysql.jdbc.Driver
db.default.user="test"
db.default.password= "test"   
db.default.url="mysql://test:test@localhost/testplay"

I prefer to use parametric url:

db.default.driver=com.mysql.jdbc.Driver
db.default.user="test"
db.default.password= "test"   
db.host="localhost"
db.schema="testplay"
db.default.url="mysql://"${db.default.username}":"${db.default.password}"@"${dbhost}"/"${dbschema}

This probably looks to your more sophisticated, that the first url, but this prevents bugs and provides better flexibility. In this way you can easily substitute the property value without breaking by mistake the url format.

You should probably also take care to set the db.default.jndiName property - I don't know, id it is relevant for jdbc. I am using JPA/hibernate and it is needed - I use the value defined in persistence.xml.