Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up writing a groovy script that tests connection to MySQL. The code follows. Place it in <strong>scripts/TestMysql.groovy</strong> and run e.g. as </p> <pre><code>grails test test-mysql localhost 3306 test-db grails-user grails-password </code></pre> <p>The first "test" is to run the script in the test environment, in case there are any changes in DataSource.groovy. </p> <p>If you can connect to MySQL with this script, at the very least you will know the correct settings for your DataSource.groovy. In case of success it also prints the relevant <code>dataSource {}</code> section to be inserted into <code>DataSource.groovy</code>.</p> <pre><code>/* Testing grails/GORM connection to mysql * Call this script "scripts/TestMysql.groovy" * Usage: "grails test-mysql" or, to run in another environment, e.g. "grails test test-mysql" * (This is in case you have changed DataSource.groovy and nothing is working.) * * Up to five parameters: * grails test test-mysql &lt;host&gt; &lt;port&gt; &lt;database&gt; &lt;user&gt; &lt;password&gt; * e.g. grails test test-mysql localhost 3306 test root rootpassword. * The database name can also be empty. */ import groovy.sql.Sql includeTargets &lt;&lt; grailsScript("_GrailsInit") &lt;&lt; grailsScript("_GrailsArgParsing") target(main: "The description of the script goes here!") { def list=argsMap['params'] def host=list[0] ? list[0] : 'localhost' def port=list[1] ? list[1] : '3306' def db=list[2] ? list[2] : '' // can leave empty def user=list[3] ? list[3] : 'grails' def pswd=list[4] ? list[4] : 'grails' println "Connecting to " + host + ":" + port println "Database:" + db println "User: " + user println "Password: " + pswd def jdbc_string='jdbc:mysql://' + host + ':' + port + '/' + db def sql try { sql = Sql.newInstance(jdbc_string, user, pswd, "com.mysql.jdbc.Driver") } catch (com.mysql.jdbc.exceptions.jdbc4.CommunicationsException e) { println "ERROR! Cannot connect to " + host + ":" + port println "Check host, port, your open ports and other firewall settings; try to connect with some other program" println "" println e return } catch (com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException e) { println "MySQL ERROR, perhaps wrong database name!" println "" println e return } catch (java.sql.SQLException e) { println "MySQL ERROR, perhaps wrong login/password!" println "" println e return } println "SUCCESS! Connected to MySQL" def query = "SHOW DATABASES" println "Executing query " + query + "..." sql.eachRow(query) { println it } println "OK, Done!" println """ dataSource { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:mysql://${host}:${port}/${db}" username="${user}" password="${pswd}" } """ } setDefaultTarget(main) </code></pre> <p><strong>Update</strong>: indeed, once this script was working, it was easy to make Grails work as well. I just had to do <code>grails clean</code>, delete all <code>*.class</code> files that survived, restore the original <code>DataSource.groovy</code>, and then <code>grails compile --refresh-dependencies</code>. Then change <code>DataSource.groovy</code>, and it works like a charm.</p> <p>Here is my working <code>DataSource.groovy</code>:</p> <pre><code>dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" username = "sa" password = "" } hibernate { cache.use_second_level_cache = true cache.use_query_cache = false cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' } // environment specific settings environments { development { dataSource { dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:mysql://localhost:8890/test" username="grails" password="grails" } } test { dataSource { dbCreate = "update" url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000" } } production { dataSource { dbCreate = "update" url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000" pooled = true properties { maxActive = -1 minEvictableIdleTimeMillis=1800000 timeBetweenEvictionRunsMillis=1800000 numTestsPerEvictionRun=3 testOnBorrow=true testWhileIdle=true testOnReturn=true validationQuery="SELECT 1" } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload