2
votes

I have been following the Typesafe online training for the Play Framework (I'm a complete beginner in terms of web design), and everything was working until the part where we are supposed to connect to a database.

When I reach the second video on lesson two this site: https://typesafe.com/how/online-training/play-java

I run into a problem, and this is the error message I get when I try refreshing my browser:

Cannot connect to database [default]
In /Users/Arash/playconf/conf/application.conf at line 43.
40 db.default.driver=com.mysql.jdbc.Driver
41
 # Here we add the database that we have created locally
43 db.default.url="jdbc:mysql://localhost:3306/playconf"
44
45 # We set this to root, we login as root with no password
46 db.default.user=root

This is the error message in my terminal (running play)

[error] c.j.b.h.AbstractConnectionHook - Failed to obtain initial connection Sleeping for 0ms and trying again. Attempts left: 0. Exception: null.Message:Unknown database 'playconf' [error] application -

! @6hmk31f56 - Internal server error, for (GET) [/] ->

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]] at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92) ~[play_2.10.jar:2.2.2] at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.10.jar:2.2.2] at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[play-jdbc_2.10.jar:2.2.2] at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:243) ~[play-jdbc_2.10.jar:2.2.2] at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na] at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'playconf' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0] at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.26.jar:na] at com.mysql.jdbc.Util.getInstance(Util.java:386) ~[mysql-connector-java-5.1.26.jar:na]

I have followed everything as precisely as I can, but I get this error, and I have been stuck here all day. Very frustrating...

Then I thought, oh, maybe I don't have mysql? So I installed mysql using homebrew, and tried again, but got the same error.

Here is my code that has to do with the database:

in the config file:

# We add our sql driver, which we have added in the build.sbt file
 # db.default.driver=org.h2.Driver # This is the original, commented out originally
 db.default.driver=com.mysql.jdbc.Driver

 # Here we add the database that we have created locally
 db.default.url="jdbc:mysql://localhost:3306/playconf"

 # We set this to root, we login as root with no password
 db.default.user=root

in the build file:

libraryDependencies ++= Seq(
    javaEbean,
    "mysql" % "mysql-connector-java" % "5.1.26",
    "org.webjars" %% "webjars-play" % "2.2.0",
    "org.webjars" % "bootstrap" % "2.3.1")

Ok, so it seemed I had to create the database, and now I have done so, but I can't access the database. I get ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'playconf'

3
Look at the last line, most of the issue is solved, but I can't access the database...Arash Saidi

3 Answers

9
votes

You need to create this playconf database in your MySQL server manually using for an example common DB GUI (it's NOT created automatically like ie. in H2)

After doing this kill the running app and run it again.

2
votes

Test connection to the database first :
It seems that the schema does not exists or your user doesn't have access to it !

Try this and post the output

 mysql -uroot -p <passwrd> -e 'use playconf;
0
votes

application.conf

db.default.driver = com.mysql.jdbc.Driver

db.default.url = "jdbc:mysql://127.0.0.1:3306/dbname"

db.default.user = root

db.default.password = 12345

db.default.host = localhost

No need to use double quotes for user,password,host and driver fields.

built.sbt

add mysql connector.

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