MongoDB and ElasticSearch on Ubuntu 16.04 - Step by step instruction

Simple instructions about installing a MongoDB and ElasticSearch on Amazon's Ubuntu 16.04.

MongoDB

If you have already installed Mongo database with data you can ignore this step:

First we need to add the mongo-org repository list:

echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" > /etc/apt/sources.list.d/mongodb-org-3.4.list

We need to install the mongo database server and cli shell of course:

apt-get install mongo-org

Fill the Mongo with test documents:

mongo DATABASE_NAME

db.createCollection(COLLECTION_NAME)

for (var i = 1; i <= 25; i++) db.COLLECTION_NAME.insert( { x : i } )

Shutdown the database:

service mongodb stop

Now we need edit the Mongo configuration file:

nano /etc/mongod.conf

Here we need add folloving line under replication line:

replSetName: rs0

Then save (Ctrl+X, Y, Enter).

Now we need run test instance with emabled replSet, for this need execute:

mongo DATABASE_NAME

config = { "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "127.0.0.1:27017" } ] }

rs.initiate(config)

rs.slaveOk()

Our database have the replication support and we can begin to install the ElasticSearch program.

But first we need shutdown the temporary instance (Ctrl+C) and fix the permitions of Mongo database library:

chown mongodb:mongodb /var/lib/mongodb -R

Now we can run our database:

service mongod restart

ElasticSearch

First we need install the ElasticSearch engine through apt, for this need execute:

apt-get install elasticsearch

In file: /usr/share/elasticsearch/bin/plugin need change the end line from exec to echo, then run commad like:

/usr/share/elasticsearch/bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.9

You should saw something like this (should be very long, may be different from your):

/usr/bin/java -Xmx64m -Xms16m -Delasticsearch -Des.path.home="/usr/share/elasticsearch" -Des.default.path.conf="/usr/share/elasticsearch/config" -Des.default.config="/usr/share/elasticsearch/config/elasticsearch.yml" -cp ":/usr/share/java/lucene-codecs-4.10.4.jar:/usr/share/java/groovy-all-2.x.jar:/usr/share/java/lucene-analyzers-icu-4.10.4.jar:/usr/share/java/lucene-queries-4.10.4.jar:/usr/share/java/jna.jar:/usr/share/java/lucene-expressions-4.10.4.jar:/usr/share/java/apache-log4j-extras-1.2.17.jar:/usr/share/java/lucene-core-4.10.4.jar:/usr/share/java/lucene-analyzers-stempel-4.10.4.jar:/usr/share/java/lucene-analyzers-phonetic-4.10.4.jar:/usr/share/java/lucene-test-framework-4.10.4.jar:/usr/share/java/spatial4j-0.4.1.jar:/usr/share/java/lucene-classification-4.10.4.jar:/usr/share/java/lucene-analyzers-uima-4.10.4.jar:/usr/share/java/jts.jar:/usr/share/java/lucene-join-4.10.4.jar:/usr/share/java/sigar.jar:/usr/share/java/lucene-analyzers-common-4.10.4.jar:/usr/share/java/lucene-facet-4.10.4.jar:/usr/share/java/lucene-misc-4.10.4.jar:/usr/share/java/lucene-demo-4.10.4.jar:/usr/share/java/lucene-replicator-4.10.4.jar:/usr/share/java/lucene-highlighter-4.10.4.jar:/usr/share/java/lucene-benchmark-4.10.4.jar:/usr/share/java/lucene-analyzers-morfologik-4.10.4.jar:/usr/share/java/lucene-analyzers-smartcn-4.10.4.jar:/usr/share/java/lucene-spatial-4.10.4.jar:/usr/share/java/lucene-queryparser-4.10.4.jar:/usr/share/java/lucene-sandbox-4.10.4.jar:/usr/share/java/log4j-1.2-1.2.17.jar:/usr/share/java/lucene-analyzers-kuromoji-4.10.4.jar:/usr/share/java/elasticsearch-1.7.3.jar:/usr/share/java/lucene-memory-4.10.4.jar:/usr/share/java/lucene-grouping-4.10.4.jar:/usr/share/java/lucene-suggest-4.10.4.jar:" org.elasticsearch.plugins.PluginManager "--install" "com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.9"

Also we need another dependencie of river package (we also need repeat the long command execution):

/usr/share/elasticsearch/bin/plugin --install elasticsearch/elasticsearch-mapper-attachments/3.1.2

/usr/share/elasticsearch/bin/plugin --install mobz/elasticsearch-head

/usr/share/elasticsearch/bin/plugin --install lukas-vlcek/bigdesk

Now we need restart the ElasticSearch server:

service elasticsearch restart

For next we need make small test of our ElasticSearch engine, just in case:

curl -XGET http://localhost:9200/

On this step we need create the index of our collection:

curl -XPUT 'http://localhost:9200/_river/mongodb/_meta' -d '{ "type": "mongodb", "mongodb": { "db": "DATABASE_NAME", "collection": "COLLECTION_NAME" }, "index": { "name": "FIELD_FOR_INDEXATION", "type": "random" } }'

Please do not forget to replace the words written in large letters on your values.

Now we can run the test query:

curl -XPUT 'http://localhost:9200/_search?q=home'

I hope this simple guide will help you in your business! Good luck!