Setup a Development Environment of a MongoDB Replica Set consisting of 3 mongod MongoDB Instances.
This is purely aimed for a testing or development environment, as one of the key points is that security is disabled, and that for this post, all 3 instances will be running on the same node.
Resources:
- https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
- https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/
Installation:
I am using Ubuntu 16.04, for other distributions, have a look at MongoDBs Installation Page
1 2 3 4 |
|
Prepare Directories:
Prepare the data directories, and as I am planning to use the --fork
option, I need to specify the the --logpath
, so therefore I will create the log directories as well:
1 2 |
|
Run 3 MongoDB Instances:
Create 3 MongoDB Instances, each instance listening on it’s unique port.
From MongoDB’s Documentation:
“The –smallfiles and –oplogSize settings reduce the disk space that each mongod instance uses”
1 2 3 |
|
Cofirm:
Confirm that the processes are listening on the ports that we defined:
1 2 3 4 5 6 |
|
Connect to the first MongoDB Instnace:
Connect to our first MongoDB Instance, where we will setup the replica set:
1 2 |
|
Create the Replica Set Configuration Object:
1 2 3 4 5 6 7 8 9 |
|
Initiate the replica set configuration:
1 2 |
|
Display the Replica Configuration with rs.conf()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Add the other two mongodb instances to the replica set using rs.add()
:
1 2 3 4 5 |
|
View the status of our MongoDB Replica Set with rs.status()
:
1
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
Write some Data to MongoDB:
Create a Database named mydb
:
1 2 |
|
Create a Collection, named mycol1
:
1 2 3 4 5 |
|
Write 2 documents with:
- Name: James, Home Address: Country => South Africa, City => Cape Town
- Name: Frank, Home Address: Country => Ireland, City => Dublin
1 2 3 4 5 |
|
Count all Documents in our Database:
1 2 |
|
Scan through all documents, and show the in pretty print
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Find Information about Frank
:
1 2 |
|
Delete the Database, but confirm which database your are logged on, the delete using dropDatabase()
:
1 2 3 4 5 6 7 8 |
|