使用MySQL配置Grails 3.1.0

4

我正在努力使用本地MySQL数据库(仅在WAMP上)设置我的Grails 3.1.0应用程序,但我尝试了现有来源中的所有方法都没有成功。

以下解决方案都没有成功:

非解决方案1

grails-app/conf/application.yml:

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: sa
    password:

environments:
    development:
        dataSource:
            dbCreate: create-drop
            url: jdbc:mysql://liveip.com/liveDb

build.gradle:

runtime 'mysql:mysql-connector-java:5.1.36'

非解决方案2

相似的方法,但是复制这个Github起始项目中的application.yml和build.gradle文件。

非解决方案3

下载MySQL连接器jar包,并按照以下答案在build.gradle文件中引用:

dependencies {

    ...

    compile files('libs/a.jar')
}

在每次尝试后,我都运行了grails clean并在IntelliJ中重新构建。每种方法都会导致以下堆栈跟踪:
ERROR org.apache.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: Unable to load class: com.mysql.jdbc.Driver" from ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb;ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb
...
Caused by: java.lang.ClassNotFoundException: Unable to load class: com.mysql.jdbc.Driver" from ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb;ClassLoader:sun.misc.Launcher$AppClassLoader@736e9adb
    at org.apache.tomcat.jdbc.pool.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:56) ~[tomcat-jdbc-8.0.30.jar:na]
...
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_05]

1
仔细查看您所引用的帖子和您自己的配置。特别是在标题“sub content elements”周围的制表符/空格。这个配置文件对于spacing dataSource: pooled: true非常具体,应该是dataSource:[enter][tab]pooled:true。 - V H
@vahid 我在代码中使用了正确的格式,但是在复制到这个问题时没有正确缩进,所以不幸的是这不是问题的原因。感谢您指出!已编辑。 - Joshua S.
那么第一种方法出了什么问题呢?你的错误与找不到相关的jar有关,这与你的第三个解决方案有关。使用第一种方法最简单,我已经让它工作了。 - V H
尝试升级到3.1.4,我的正在运行这个版本。我没有特别测试过3.1.0。 - V H
1个回答

2
这是我的build.gradle文件:

build.gradle:

dependencies {
    runtime 'mysql:mysql-connector-java:5.1.20'

}

现在是application.yml文件:
dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username: username
        password: opendoor_policy
    nextdbsource:
        pooled: true
        jmxExport: true
        driverClassName: com.mysql.jdbc.Driver
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        username: username
        password: opendoor_policy
        url: jdbc:mysql://localhost:3306/nextdbsource?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
        dbCreate: update

environments:
    development:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
    test:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
    production:
        dataSources:
            dataSource:
                dbCreate: update
                url: jdbc:mysql://localhost:3306/db1?autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
                properties:
                    jmxEnabled: true
                    initialSize: 5
                    maxActive: 50
                    minIdle: 5
                    maxIdle: 25
                    maxWait: 10000
                    maxAge: 600000
                    timeBetweenEvictionRunsMillis: 5000
                    minEvictableIdleTimeMillis: 60000
                    validationQuery: SELECT 1
                    validationQueryTimeout: 3
                    validationInterval: 15000
                    testOnBorrow: true
                    testWhileIdle: true
                    testOnReturn: false
                    jdbcInterceptors: ConnectionState
                    defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

---

这对我有帮助,它设置了两个数据源。

不幸的是,问题依旧存在。我将尝试您的建议进行升级,看看是否有所帮助。 - Joshua S.
@JoshuaS。我有一种通用的升级方式 - 获取版本,创建一个该版本的应用程序,然后复制grails-app / [controllers / views / services / taglib / config / application.groovy],如果我已经放置了src / main / groovy文件夹,则也将它们复制过来。最后更新buildconfig以调用任何插件并审查配置文件 - 这就是简单快速的升级。 - V H
1
看起来升级没有帮助,但是通过Grails控制台运行而不是通过IntelliJ运行可以完美解决问题。如果您在Grails开发中使用IntelliJ,有任何想法为什么IntelliJ运行会导致异常,但是通过Grails控制台不会呢? - Joshua S.
@JoshuaS。我想这完全取决于Intelij返回的问题 - 我会从检查Intelij的设置开始 - 在设置中搜索sdk,确保它设置为正确的Grails版本SDK。然后我会检查JDK(ctrl shift alt + s)并确保其全部正确设置。最后,我进入所有使用控制台的IDE,并始终确保在进入之前运行相关版本的Grails和JDK。我认为前两点应该能回答你的问题。 - V H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接