blob: 3ad73e6a26ded4ea2611f5cd349e0b7050324420 [file] [log] [blame]
h4. Mongo Database Connection Configuration
As mentioned the GORM for Mongo plugin will configure all the defaults for you, but if you wish to customize those defaults you can do so in the @grails-app/conf/DataSource.groovy@ file:
{code}
grails {
mongo {
host = "localhost"
port = 27017
username = "blah"
password = "blah"
databaseName = "foo"
}
}
{code}
The @databaseName@ setting configures the default database name. If not specified the @databaseName@ will default to the name of your application.
You can also customize the Mongo connection settings using an @options@ block:
{code}
grails {
mongo {
options {
autoConnectRetry = true
connectTimeout = 300
}
}
}
{code}
Available options and their descriptions are defined in the [MongoOptions|http://api.mongodb.org/java/current/com/mongodb/MongoOptions.html] javadoc.
In production scenarios you will typically use more than one MongoDB server in either [master/slave|http://docs.mongodb.org/manual/core/master-slave/] or [replication|http://docs.mongodb.org/manual/replication/] scenarios. The plugin allows you to configure [replica sets|http://docs.mongodb.org/manual/tutorial/deploy-replica-set/]:
{code}
grails {
mongo {
replicaSet = [ "localhost:27017", "localhost:27018"]
}
}
{code}
The replica sets are defined using a list of strings that conform to the MongoDB [DBAddress|http://api.mongodb.org/java/current/com/mongodb/DBAddress.html] specification.
h4. MongoDB Connection Strings
Since 2.0, you can also use MongoDB [connection strings|http://docs.mongodb.org/manual/reference/connection-string/] to configure the connection:
{code}
grails {
mongo {
connectionString = "mongodb://localhost/mydb"
}
}
{code}
Using MongoDB connection strings is currently the most flexible and recommended way to configure MongoDB connections.
h4. SSL Connections
You can configure SSL connections by setting @ssl@ to true in the @options@ block:
{code}
grails {
mongo {
...
options {
ssl = true
...
}
}
}
{code}
{note}
Note you can also configure the @SSLSocketFactory@ manually if necessary. All settings within the @options@ block are propagated to the underlying [MongoOptionsFactoryBean|http://docs.spring.io/spring-data/mongodb/docs/1.4.0.RELEASE/api/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.html]
{note}
h4. Configuration Options Guide
Below is a complete example showing all configuration options:
{code}
grails {
mongo {
databaseName = "myDb" // the default database name
host "localhost" // the host to connect to
port = 27017 // the port to connect to
username = ".." // the username to connect with
password = ".." // the password to connect with
stateless = false // whether to use stateless sessions by default
// Alternatively, using 'replicaSet' or 'connectionString'
// replicaSet = [ "localhost:27017", "localhost:27018"]
// connectionString = "mongodb://localhost/mydb"
options {
connectionsPerHost = 10 // The maximum number of connections allowed per host
threadsAllowedToBlockForConnectionMultiplier = 5
maxWaitTime = 120000 // Max wait time of a blocking thread for a connection.
connectTimeout = 0 // The connect timeout in milliseconds. 0 == infinite
socketTimeout = 0 // The socket timeout. 0 == infinite
socketKeepAlive = false // Whether or not to have socket keep alive turned on
writeNumber = 0 // This specifies the number of servers to wait for on the write operation
writeTimeout = 0 // The timeout for write operations in milliseconds
writeFsync = false // Whether or not to fsync
autoConnectRetry = false // Whether or not the system retries automatically on a failed connect
maxAutoConnectRetryTime = 0 // The maximum amount of time in millisecons to spend retrying
slaveOk = false // Specifies if the driver is allowed to read from secondaries or slaves
ssl = false // Specifies if the driver should use an SSL connection to Mongo
sslSocketFactory = ... // Specifies the SSLSocketFactory to use for creating SSL connections
}
}
}
{code}
h4. Global Mapping Configuration
Using the @grails.mongo.default.mapping@ setting in @Config.groovy@ you can configure global mapping options across your domain classes. This is useful if, for example, you want to disable optimistic locking globally or you wish to use @DBRefs@ in your association mappings. For example, the following configuration will disable optimistic locking globally and use @DBRefs@ for all properties:
{code}
grails.mongo.default.mapping = {
version false
'*'(reference:true)
}
{code}
The @*@ method is used to indicate that the setting applies to all properties.