My name is Vasyl Khrystiuk‎ > ‎pub‎ > ‎java‎ > ‎libs‎ > ‎

java MongoDB

Annotation


Now I am writiing app, that will have persistance in MongoDB.
Why mongo:


Solutions overview

Getting started: 

So, what options is available in java :

    pros: low level control - maximal flexebility
    cons: many code need to write manyally - converting POJO to  DBObject, etc.
        * as a note here - for converting mongo to gson there is a library, in reverse way from json(gson) to mongo: this code

Morphia - excellent ORM for MongoDB
    pros: fast developnent 
    cons: this is ORM, so we have in practice work with strong typed objects - less flexible

Spring data: http://projects.spring.io/spring-data-mongodb/ - MongoDBORM for Spring
    pros: fast development and in-built integration with spring environment
    cons: hard to learn(if there is no expiriance with spring-data before), less flexible, work only with spring
        * tutorials:

hibernate ogm mongodb 
???


Securing service

Create db admin 

Securing working service:
>mongo
> show databases
> use mydb5
> db.system.users.find()
// пусто
> use admin
switched to db admin
>db.addUser( { user: "<username>", pwd: "<password>", roles: [ "userAdminAnyDatabase", "userAdmin" ] } )
{
        "user" : "<username>",
        "pwd" : "<password_hash>",
        "roles" : [
                "userAdminAnyDatabase",
                "userAdmin"
        ],
        "_id" : ObjectId("530c14468d296d217b9804c2")
}

Enable auth

For disable local bypass, add option to mongodb:
mongod --setParameter enableLocalhostAuthBypass=0
or in config file(manual):
setParameter=enableLocalhostAuthBypass=0

actually, need:
remove service
add this to service config:
auth=true
setParameter = enableLocalhostAuthBypass=0
install service
in case of problem with installing/reinstalling service:

now, after setting options
>mongo
MongoDB shell version: 2.4.1
connecting to: test
> use admin
switched to db admin
> db.system.users.find()
error: { "$err" : "not authorized for query on admin.system.users", "code" : 16550 }
> db.auth('<username>','<password>');
1
> db.system.users.find()
{ "_id" : ObjectId("530c14468d296d217b9804c2"), "user" : "<username>", "pwd" : "<password_hash>", "roles" : [ "userAdminAnyDatabase", "userAdmin" ] }
>

Add user to db


use mydb5
db.addUser( { user: "<username>", pwd: "<password>", roles: [ "readWrite", "dbAdmin", "userAdmin" ] } )

Configure libraries for work with auth

Spring mongo data

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    <constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>

<mongo:db-factory dbname="mydb5" id="mongoDbFactory" username="username" password="password"/>

Configure remote access

TODO


Dumping and restore

Create dump:
timestamp=$(date +_%s) # timestamp
timestamp=$(date +_%y.%m.%d_%H.%M)
mongodump  --username <username> --password <password> -db mydb5 --out mydbdump${timestamp}
mongodump  -db mydb5 --out mydbdump${timestamp}
tar -zcvf dump${timestamp}.tar.gz mydbdump${timestamp}
rm -rf mydbdump${timestamp}

Restore dump:
tar -xzvf dump_14.03.04_16.23.tar.gz
# mongorestore --drop --username <username> --password <password> -db mydb5 dump_14.03.04_16.23\mydb5
#mongorestore --host mongodb1.example.net --port 3017 --drop --username user --password pass /opt/backup/mongodump-2012-10-24
Comments