如何使用grails配置mongodb

时间:2015-05-02 07:28:18

标签: mongodb grails

我是这样的新手。我试图使用milsodb和grails来制作登录应用程序时遇到错误。

grails 2.4.0 mongodb 3.0.3

BuildConfig.groovy。

grails.servlet.version = "3.0" // Change depending on target container  
compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.work.dir = "target/work"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

grails.project.fork = [
    // configure settings for compilation JVM, note that if you alter the   
Groovy version forked compilation is required
    //  compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, 
daemon:true],

    // configure settings for the test-app JVM, uses the daemon by default
    test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, 
daemon:true],
    // configure settings for the run-app JVM
    run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256,  
forkReserve:false],
    // configure settings for the run-war JVM
    war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, 
forkReserve:false],
    // configure settings for the Console UI JVM
    console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
   ]
grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
    // specify dependency exclusions here; for example, uncomment this to 
disable ehcache:
    // excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 
'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
legacyResolve false // whether to do a secondary resolve on plugin 
installation, not advised and here for backwards compatibility

repositories {
    inherits true // Whether to inherit repository definitions from plugins

    grailsPlugins()
    grailsHome()
    mavenLocal()
    grailsCentral()
    mavenCentral()
    // uncomment these (or add new ones) to enable remote dependency 
    resolution from public Maven repositories
    //mavenRepo "http://repository.codehaus.org"
    //mavenRepo "http://download.java.net/maven/2/"
    //mavenRepo "http://repository.jboss.com/maven2/"
}

dependencies {
    // specify dependencies here under either 'build', 'compile', 'runtime', 
    'test' or 'provided' scopes e.g.
    // runtime 'mysql:mysql-connector-java:5.1.29'
    // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'

    compile "org.springframework:spring-orm:$springVersion"
    compile 'org.grails:grails-datastore-gorm:3.1.4.RELEASE'
    compile 'org.grails:grails-datastore-core:3.1.4.RELEASE'
    test 'org.grails:grails-datastore-simple:3.1.4.RELEASE'
}

plugins {
    // plugins for the build system only
    build ":tomcat:7.0.53"

    // plugins for the compile step
    compile ":scaffolding:2.1.0"
    compile ':cache:1.1.6'
    compile ":asset-pipeline:1.8.7"

    // plugins needed at runtime but not for compilation
   // runtime ":hibernate4:4.3.5.3" // or ":hibernate:3.6.10.15"
    runtime ":database-migration:1.4.0"
    runtime ":jquery:1.11.1"

    // Uncomment these to enable additional asset-pipeline capabilities
    //compile ":sass-asset-pipeline:1.7.4"
    //compile ":less-asset-pipeline:1.7.0"
    //compile ":coffee-asset-pipeline:1.7.0"
    //compile ":handlebars-asset-pipeline:1.3.0.3"
    compile ":mongodb:3.0.3"

 }
}

DataSource.groovy中

dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}

grails {
 mongo {
    host = "localhost"
    port = 27017
    username = "admin"
    password = "admin"
    databaseName = "mydb"
 } 
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
 //    cache.region.factory_class =  
'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 
'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
}

// environment specific settings
environments {
 development {
    dataSource {
        dbCreate = "create-drop" // one of 'create', 'create-drop', 
         'update', 'validate', ''
        url =   
     "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }
  }
 test {
    dataSource {
        dbCreate = "update"
        url =   
    "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }
  }
  production {
    dataSource {
        dbCreate = "update"
        url = 
        "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        properties {
           // See http://grails.org/doc/latest/guide/conf.html#dataSource   
             //for documentation
           jmxEnabled = true
           initialSize = 5
           maxActive = 50
           minIdle = 5
           maxIdle = 25
           maxWait = 10000
           maxAge = 10 * 60000
           timeBetweenEvictionRunsMillis = 5000
           minEvictableIdleTimeMillis = 60000
           validationQuery = "SELECT 1"
           validationQueryTimeout = 3
           validationInterval = 15000
           testOnBorrow = true
           testWhileIdle = true
           testOnReturn = false
           jdbcInterceptors = "ConnectionState"
           defaultTransactionIsolation = 
                         java.sql.Connection.TRANSACTION_READ_COMMITTED
        }
      }
   }
}

运行此应用程序时出错我花了更多时间来解决它,但我认为这将是版本兼容性问题。

  Error | 2015-05-09 11:50:58,502 [localhost-startStop-1] ERROR    
  context.GrailsContextLoaderListener  - Error initializing the application:    
  Error creating bean with name 'transactionManagerPostProcessor':   
  Initialization of bean failed; nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'transactionManager': Cannot resolve reference to bean 
  'mongoTransactionManager' while setting constructor argument with key [1]; 
  nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongoTransactionManager': Cannot resolve reference to bean 
  'mongoDatastore' while setting bean property 'datastore'; nested exception 
  is org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongoDatastore': Cannot resolve reference to bean   
  'mongoBean' while setting bean property 'mongo'; nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongo': Invocation of init method failed; nested exception 
  is com.mongodb.MongoTimeoutException: Timed out while waiting for a server 
  that matches AnyServerSelector{} after 10000 ms

 Message: Error creating bean with name 'transactionManagerPostProcessor': 
 Initialization of bean failed; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'transactionManager': Cannot resolve reference to bean 
 'mongoTransactionManager' while setting constructor argument with key [1]; 
 nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'mongoTransactionManager': Cannot resolve reference to bean 
 'mongoDatastore' while setting bean property 'datastore'; nested exception 
 is org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'mongoDatastore': Cannot resolve reference to bean 
 'mongoBean' while setting bean property 'mongo'; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating  
 bean with name 'mongo': Invocation of init method failed; nested exception 
 is com.mongodb.MongoTimeoutException: Timed out while waiting for a server 
 that matches AnyServerSelector{} after 10000 ms

1 个答案:

答案 0 :(得分:0)

好像您的MongoDB服务器没有运行到本地计算机上。请确认并开始服务。

对于Ubuntu:

 sudo service mongod start

或者对于其他操作系统,请按照此http://docs.mongodb.org/manual/installation/

进行操作